Visual Studio:如何获取文件中“-”之前的名称

huangapple go评论77阅读模式
英文:

Visual Studio: How to get name before - of a file

问题

如何获取文件名中“-”之前的部分并将结果存入变量?
例如,文件名:test.adress-12.txt
但我只想要test.adresss

我已经尝试过搜索,但找不到任何信息,只有关于如何在Visual Code中查找和替换内容的信息。

英文:

How do I get the part before the - in the filename and put the outcome in a variable?
For example a filename: test.adress-12.txt
But I only want test.adresss

I have tried searching but couldn't find anything, only things how I can find and replace something within Visual Code itself.

答案1

得分: 0

C#

string fileName = "test.adress-12.txt";
string[] results = fileName.Split('-');
string result = results[0];

VB

Dim fileName As String = "test.adress-12.txt"
Dim results As String() = fileName.Split("-"c)
Dim result As String = results(0)
英文:

If you always know there will be a single - and you are using C#, then you can use string.Split('-') and take the 0th element.

C#

string fileName = "test.adress-12.txt";
string[] results = fileName.Split('-');
string result = results[0];

Here is the VB documentation for Strings.Split and here is an example of the same code in the C# example converted to VB.

VB

Dim fileName As String = "test.adress-12.txt"
Dim results As String() = fileName.Split("-"c)
Dim result As String = results(0)

huangapple
  • 本文由 发表于 2023年7月10日 23:48:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定