如何从字符串中提取指定的字符?如何从字符串中提取指定的字符?
Provider=SQLOLEDB.1;Data Source=127.1.1.1;Initial Catalog=CD_SQL;User ID=sa;Password=123456;Persist Security Info=True;如何分别提取到
127.0.0.1
CD_SQL
SQ
123456
--------------------编程问答--------------------
string connString = "Provider=SQLOLEDB.1;Data Source=127.1.1.1;Initial Catalog=CD_SQL;User ID=sa;Password=123456;Persist Security Info=True;";--------------------编程问答--------------------
string[] buffer = connString.Split(new char[]{';'});
foreach (string key in buffer)
{
int index = key.IndexOf('=');
if(index>=0)
{
//key.Substring(index+1)即为要求值
}
}
--------------------编程问答--------------------
string str = "Provider=SQLOLEDB.1;Data Source=127.1.1.1;Initial Catalog=CD_SQL;User ID=sa;Password=123456;Persist Security Info=True;";
string[] strs = str.Split(';');
foreach (var item in strs)
{
if (item.Length == 0)
break;
int index = item.IndexOf('=');
string s = item.Substring(index + 1, item.Length - index - 1);
MessageBox.Show(s);
}
--------------------编程问答-------------------- 都是=号后面的内容。以;号结尾。用indexOF和substring就可以做到了。
private static Dictionary<string, string> GetStartUpParams(string[] args)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
if (args != null && args.Length > 0)
{
string arg = args[0];
string[] paramslist = arg.Split(';');
foreach (string str in paramslist)
{
string[] strs = str.Split(':');
if (strs.Length > 1)
{
dict.Add(strs[0], strs[1]);
}
}
}
return dict;
}
补充:.NET技术 , C#