首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

求字符串解析?解决方法

2012-05-05 
求字符串解析?字符串如下:ConnectString{Persist Security InfoTrueProviderSQLOLEDB.1User IDsaPa

求字符串解析?
字符串如下:
ConnectString={Persist Security Info=True;Provider=SQLOLEDB.1;User ID=sa;Password=123456;Data Source=XXXXX;Initial Catalog=DB8888};UserName=XX;UserID=XX;

如何得到以下字符串
Persist Security Info=True;User ID=sa;Password=123456;Data Source=XXXXX;Initial Catalog=DB8888

注意:Provider=SQLOLEDB.1;也要去掉哈

[解决办法]
正则表达式不会写
通俗点的,可以用截取实现

C# code
string str = "ConnectString={Persist Security Info=True;Provider=SQLOLEDB.1;User ID=sa;Password=123456;Data Source=XXXXX;Initial Catalog=DB8888};UserName=XX;UserID=XX;";            string str1 = str.Substring(str.IndexOf('{'));            string str2 = str1.Substring(0, str1.IndexOf('}')).Replace("Provider=SQLOLEDB.1", "");
[解决办法]
探讨
正则表达式不会写
通俗点的,可以用截取实现

C# code

string str = "ConnectString={Persist Security Info=True;Provider=SQLOLEDB.1;User ID=sa;Password=123456;Data Source=XXXXX;Initial Catalog=DB8888};UserName=XX;UserID……

[解决办法]
C# code
void Main(){    string str=@"ConnectString={Persist Security Info=True;Provider=SQLOLEDB.1;User ID=sa;Password=123456;Data Source=XXXXX;Initial Catalog=DB8888};UserName=XX;UserID=XX;";    string result=Regex.Replace(Regex.Match(str,@"(?<={)[^}]+(?=})").Value,@"(?i)Provider[^;]+;","");    Console.WriteLine(result);    //Persist Security Info=True;User ID=sa;Password=123456;Data Source=XXXXX;Initial Catalog=DB8888} 

热点排行