假設我有:使用長度功能如何從一個字符串複製到另一個字符串從所需的位置到最後?
string abc="Your name = Hello World";
我匹配=
操作者的位置的存在,但我怎麼能複製所有的=
之後到來的話,比如「Hello字」,從這個字符串到另一個?
假設我有:使用長度功能如何從一個字符串複製到另一個字符串從所需的位置到最後?
string abc="Your name = Hello World";
我匹配=
操作者的位置的存在,但我怎麼能複製所有的=
之後到來的話,比如「Hello字」,從這個字符串到另一個?
有幾種方法可以做到這一點。這裏有幾個例子...
使用Split
:
string[] parts = abc.Split(new char[]{'='}, 2);
if (parts.Length != 2) { /* Error */ }
string result = parts[1].TrimStart();
使用IndexOf
和Substring
:
int i = abc.IndexOf('=');
if (i == -1) { /* Error */ }
string s = abc.Substring(abc, i).TrimStart();
使用正則表達式(可能是矯枉過正此):
Match match = Regex.Match(abc, @"=\s*(.*)");
if (!match.Success) { /* Error */ }
string result = match.Groups[1].Value;
string abc="Your name = Hello World";
abc.Substring(abc.IndexOf("=")+1); //returns " Hello World"
string newstring = abc.Substring(abc.IndexOf("=") + 2);
string abc="Your name = Hello World";
string[] newString = abc.Split('=');
/*
newString[0] is 'Your name '
newString[1] is ' Hello World'
*/