using System.Text.RegularExpressions; string str = "顧客・VIP(Top)"; string patter = @"(\w+)・(\w+)[((](\w+)[))]"; Match match = Regex.Match(str, patter); if (match != null) { text1.Text = match.Groups[1].Value; text2.Text = match.Groups[2].Value; text3.Text = match.Groups[3].Value; }
string str = @"顧客・VIP(Top)"; Regex reg = new Regex(@"(.*?)・(.*?)(.*?)"); MatchCollection mc = reg.Matches(str); string a = mc[0].Groups[1].ToString(); string b = mc[0].Groups[2].ToString(); string c = mc[0].Groups[3].ToString();
string str = @"顧客・VIP(Top)"; Regex reg = new Regex(@"(.*?)・(.*?)((.*?))"); Match match = Regex.Match(str, @"(.*?)・(.*?)((.*?))"); if (match != null) { string a = match.Groups[1].ToString(); string b = match.Groups[2].ToString(); string c = match.Groups[3].ToString(); }
string testString = "顧客・VIP(Top)"; //[\u4e00-\u9fa5]是汉字 //\u30fb是・ //\(\uff08是(或( //\)\uff09是)或) Regex r = new Regex(@"(?<Name>[\u4e00-\u9fa5]+)\s*\u30fb\s*(?<Level>\w+)\s*[\(\uff08](?<Site>\w+)[\)\uff09]"); Match m = r.Match(testString); if (m.Success) { string name = m.Result("${Name}"); string level = m.Result("${Level}"); string site = m.Result("${Site}"); }
using System.Text.RegularExpressions;
string str = "顧客・VIP(Top)";
string patter = @"(\w+)・(\w+)[((](\w+)[))]";
Match match = Regex.Match(str, patter);
if (match != null)
{
text1.Text = match.Groups[1].Value;
text2.Text = match.Groups[2].Value;
text3.Text = match.Groups[3].Value;
}
Regex reg = new Regex(@"(.*?)・(.*?)(.*?)");
MatchCollection mc = reg.Matches(str);
string a = mc[0].Groups[1].ToString();
string b = mc[0].Groups[2].ToString();
string c = mc[0].Groups[3].ToString();
text2.text=str.Substring(起始位置, 终止位置);
text3.text=str.Substring(起始位置, 终止位置);
Regex reg = new Regex(@"(.*?)・(.*?)((.*?))");
Match match = Regex.Match(str, @"(.*?)・(.*?)((.*?))");
if (match != null)
{
string a = match.Groups[1].ToString();
string b = match.Groups[2].ToString();
string c = match.Groups[3].ToString(); }
string s1= str.Substring(0, 2);
textBox1.Text = s1;
//略
string[] strs=str.split(ch);text1.text=strs[0];
text2.text=strs[1];
text3.text=strs[2] ;
8楼的有点太粗糙,如果字符串长度变了咋办。
可以用String.IndexOf来获取位置,这个办法应该效率不错。
Regex reg = new Regex(@"(.*)・(.*)((.*))");
Match m = reg.Match(str);
MessageBox.Show(m.Groups[1].Value);
MessageBox.Show(m.Groups[2].Value);
MessageBox.Show(m.Groups[3].Value);
private void SubString()
{
string str = "顧客・VIP(Top)"; text1.Text = str.Substring(0, 2);
text1.Text = str.Substring(3, 3);
text1.Text = str.Substring(7, 3); }应该是这样写,5楼的答案是错误的,应该是text1.text=str.Substring(起始位置, 取值长度);
private void SubString()
{
string str = "顧客・VIP(Top)"; text1.Text = str.Substring(0, 2);
text2.Text = str.Substring(3, 3);
text3.Text = str.Substring(7, 3); }
修改了下 楼主可以直接拿去用了
//[\u4e00-\u9fa5]是汉字
//\u30fb是・
//\(\uff08是(或(
//\)\uff09是)或)
Regex r = new Regex(@"(?<Name>[\u4e00-\u9fa5]+)\s*\u30fb\s*(?<Level>\w+)\s*[\(\uff08](?<Site>\w+)[\)\uff09]");
Match m = r.Match(testString);
if (m.Success)
{
string name = m.Result("${Name}");
string level = m.Result("${Level}");
string site = m.Result("${Site}");
}
根据长度用substring 截取字符串
text2.text=kw[1];
text3.text=kw[2];http://www.mybuffet.cn