c#中动态拆分单词
本文关键字:单词 拆分 动态 | 更新日期: 2023-09-27 17:49:24
我正在创建一个项目,我想动态分组的词之前我将它们拆分并分组为静态
如果我需要插入(1 100000888888888 4949494949 17032 HYB DR 25-May-2000预订05-May-2000)
我的cs代码是1) 12) 1000008888888883) 49494949494) 170325) HYB6)博士7) 5 - 5 - 20008)预订9) 05 - 2000年5月——
string text;
string sr = "";
string transid = "";
string pnr = "";
string trainno = "";
string fr = "";
string tt = "";
string doj = "";
string reservestat = "";
string dobook = "";
text = txtarea.Text;
string[] words = text.Split(''n');
foreach (string s1 in words)
{
string text1 = s1;
string[] words1 = text1.Split(''t');
int a = words1.Length;
if (a == 9 || a == 10)
{
if (a == 9)
{
sr = words1[1].ToString();
transid = words1[2].ToString();
pnr = words1[3].ToString();
trainno = words1[4].ToString();
fr = words1[5].ToString();
SqlCommand cmd1 = new SqlCommand("SpLocZonedata");// select location from zonedata
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Connection = con;
SqlParameter param1;
param1 = new SqlParameter("@location_code", fr);
param1.Direction = ParameterDirection.Input;
param1.DbType = DbType.String;
cmd1.Parameters.Add(param1);
con.Open();
SqlDataReader da0 = cmd1.ExecuteReader();
if (da0.Read())
{
Label5.Text = da0["location_name"].ToString();
}
con.Close();
tt = words1[6].ToString();
SqlCommand cmd2 = new SqlCommand("SpLocZonedata");//sellect location from zonedata
cmd2.CommandType = CommandType.StoredProcedure;
SqlParameter param ;
param = new SqlParameter("@location_code", tt);
param.Direction = ParameterDirection.Input;
param.DbType = DbType.String;
cmd2.Parameters.Add(param);
cmd2.Connection = con;
con.Open();
SqlDataReader tt1 = cmd2.ExecuteReader();
if (tt1.Read())
{
Label6.Text = tt1["location_name"].ToString();
}
con.Close();
doj = words1[7].ToString();
reservestat = words1[8].ToString();
dobook = words1[9].ToString();
}但是现在来自user的值插入了
(1 1 000008 88888888 494949 4949 170 32 HYB DR 25-May-2000预订05-May-2000)但输出必须相同
1) 12) 1000008888888883) 49494949494) 170325) HYB6)博士7) 5 - 5 - 20008)预订9) 05 - 2000年5月——
你可以在这里像这样使用正则表达式:
Regex rgxData = new Regex("([0-9 ]+)([a-zA-Z]+)");
Match mData = rgxData.Match(input);
string sr = mData.Groups[1].Value.Trim();
string quota = mData.Groups[2].Value.Trim();
这将导致:
input = "153 81 2612GEN";
- sr:
153 81 2612
- 配额:
GEN
input = "153 81 1 1 1 1 1 1 ABCDE";
- sr:
153 81 1 1 1 1 1 1
- 配额:
ABCDE
input = "123 AB";
- sr:
123
- 配额:
AB
foreach (string line in words)
{
var split = line.Split(''t');
sr = string.Join(''t', split.Take(split.Count() - 1));
Quota = split.Last();
}
foreach (string s1 in words)
{
string text1 = s1;
string[] words1 = text1.Split(''t');
int a = words1.length;
Regex regex = new Regex(@"/^[0-9]*$/");
foreach(string compare in words1)
{
if (regex.IsMatch(compare))
sr = sr + " " + compare;
else
quota = quota + " " + compare;
}
}
using linq
var counts = words.GroupBy(x => x)
.Select(g => new { Text = g.Key, Count = g.Count() });