c# - 从字符串中提取文本

标签 c#

我需要提取文本字符串的一部分(在本例中是“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/

相关文章:

c# - MonoTouch - 字段与自动属性

c# - 内部 System.Linq.Set<T> 与公共(public) System.Collections.Generic.HashSet<T>

c# - 如何在 asp.net webforms 中激活当前菜单项

c# - 复选框内容内的列表框 - WPF

c# - 如何编写一个函数来确定一个键是否按下并经过延迟?

c# - String.Contains() : Multiple character - C#

c# - WPF:获取堆栈面板中触摸的项目索引

c# - WPF:将列表动态绑定(bind)到(某些)对象的属性

c# - 以编程方式格式化生成的 CodeDom 代码

c# - 流利的 NHibernate、varbinary(max) 和 SQLite