在字符串中拆分和定位字符串的出现 [英] Split and locate occurrences of character strings within string
本文介绍了在字符串中拆分和定位字符串的出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在字符串和拆分中找到第二个出现的-"
示例:
myFileName-12345678-Inbound-Test_ND.txt
我只需要获取:myFileName-12345678
How do I find second occurrences "-" in string and split
Example:
myFileName-12345678-Inbound-Test_ND.txt
I need to get only:myFileName-12345678
推荐答案
使用
Use
string[] tokens = originalFileName.Split(new char[] {''-'', ''.''});
之后,根据您的喜好重新组成输出字符串片段.优先使用string.Format
而不是字符串连接,例如:
After that, re-compose the output string fragment at your liking. Prefer string.Format
over string concatenation, for example:
int tokenLength = tokens.Length;
if (tokenLength > 2)
myNewFileName = string.Format("{0}-{1}.{2}",
tokens[0], tokens[1], tokens[tokenLength - 1]);
您可以使用Substring
方法获取子字符串,并根据IndexOf(''-'')
返回的值定义起点.
You can take the substring usingSubstring
method and define the starting point based on the value returned byIndexOf(''-'')
.
这篇关于在字符串中拆分和定位字符串的出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文