如何获取字符串中第二个逗号的索引 [英] How to get the Index of second comma in a string
问题描述
我在一个包含两个逗号以及制表符和空格的数组中有一个字符串。我想在该字符串中切两个字,两个都放在逗号前,我真的不在乎标签和空格。
我的字符串看起来像这样:
String s = Address1芝加哥,伊利诺伊州Address2底特律,密歇根州
我得到第一个逗号的索引
int x = s.IndexOf(',');
然后从那里,我在第一个逗号的索引前切下字符串。
firstCity = s.Substring(x-10,x).Trim()//修剪字母C之前的空格;
那么,如何获取第二个逗号的索引,以便获取第二个字符串? / p>
非常感谢您的帮助!
您必须使用代码
int index = s.IndexOf(',',s.IndexOf(',')+ 1);
您可能需要确保不要超出字符串的范围。我会把那部分留给你。
I have a string in an Array that contains two commas as well as tabs and white spaces. I'm trying to cut two words in that string, both of them before the commas, I really don't care about the tabs and white spaces.
My String looks similar to this:
String s = "Address1 Chicago, IL Address2 Detroit, MI"
I get the index of the first comma
int x = s.IndexOf(',');
And from there, I cut the string before the index of the first comma.
firstCity = s.Substring(x-10, x).Trim() //trim white spaces before the letter C;
So, how do I get the index of the second comma so I can get my second string?
I really appreciate your help!
You have to use code like this.
int index = s.IndexOf(',', s.IndexOf(',') + 1);
You may need to make sure you do not go outside the bounds of the string though. I will leave that part up to you.
这篇关于如何获取字符串中第二个逗号的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!