[英]C# split string of first character occurrence
I thought this was simple but this is just kicking my butt. 我以为这很简单,但这只是在踢我的屁股。
I have this string 21. A.Person
I simply want to get A.Person
out of this. 我有这个字符串21. A.Person
我只是想从中获得A.Person
。
I try the following but I only get 21
我尝试以下方法,但我只得到21
string[] pName = values[i, j].ToString().Split(new char[] { '.' }, 2);
pName[1] ???
values[i, j].ToString() = 21. A.Person
and yes I've verified this. values[i, j].ToString() = 21. A.Person
,是的,我已经对此进行了验证。
Try this: 尝试这个:
var substr="";
var indedx = yourString.IndexOf('.');
if(index>-1)
substr = yourString.Substring(index);
substr=substr.Trim();
For string "21. A.Person" should return "A.Person" 对于字符串“21。A.Person”应返回“ A.Person”
Everyone is giving you alternate solutions when yours should work. 每个人都在给你备用解决方案时,你应该工作。 The problem is that values[i, j] must not equal 21. A.Person
问题是值[i,j] 不能等于21. A.Person
I plugged it into a simple test.. 我将其插入一个简单的测试中。
[Test]
public void junk()
{
string[] pName = "21. A.Person".Split(new char[] { '.' }, 2);
Console.WriteLine(pName[1]);
}
What does it print? 它打印什么? A.Person
(With the space in the front, because you didn't trim the space) A.Person
(前面有空格,因为您没有修剪空格)
I would use substring() with the position of the first '.' 我会在第一个'。'的位置使用substring()。 as your start point: 作为起点:
var name = sourceString.Substring(sourceString.IndexOf('.'));
string pName = values[i, j].ToString().Substring(values[i, j].ToString().IndexOf('.')+1);
Try something like that: 尝试这样的事情:
var str = "21. A.Person";
var index = str.IndexOf('.') +1;
var substr = str.Substring(index, str.Length - index);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.