我需要提取文本字符串的一部分(在本例中是“Data Source =”之后的所有内容。
"数据源=xxxxx"
在VBA中,有一个函数调用Mid()
strText = "Data Source=xxxxx"
var = Mid(strText, 12)
C#中有类似的东西吗?
最佳答案
您可以使用 String.Substring(Int32)
overload ;
Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.
string strText = "Data Source=xxxxx";
string s = strText.Substring(12);
s
将是 xxxxx
这里是一个 demonstration
.
在你的情况下使用 IndexOf
方法或 Split
方法会更好 IMO..
string s = strText.Substring(strText.IndexOf('=') + 1);
或
string s = strText.Split(new []{'='}, StringSplitOptions.RemoveEmptyEntries)[1];
关于c# - 从字符串中提取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22855358/