[英]How can I copy from one string to another string from the desired location to the end?
假设我有:
string abc="Your name = Hello World";
使用长度函数我匹配=
运算符的位置的存在,但是如何将=
之后的所有单词(例如“Hello Word”)从此字符串复制到另一个字符串?
string abc="Your name = Hello World";
abc.Substring(abc.IndexOf("=")+1); //returns " Hello World"
有几种方法可以做到这一点。 这里有一些例子...
使用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 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'
*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.