英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论