在c#中动态分割单词

时间:2016-06-25 08:12:52

标签: c# split

我正在创建一个项目,我想在其中动态分组单词 以前我拆分并将它们分组为静态

如果我需要插入(1 100000888888888 4949494949 17032 HYB DR 25-May-2000 Booked 05-May-2000)

  

输出   1)1   2)100000888888888   3)4949494949   4)17032   5)HYB   6)DR   7)5-五月-2000   8)被预订   9)05-五月-2000

我的cs代码是

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();

} 但现在来自用户的值正在插入

(1 1 000008 88888888 494949 4949 170 32 HYB DR 25-May-2000 Booked 05-May-2000) 但输出必须相同

  

1)1   2)100000888888888   3)4949494949   4)17032   5)HYB   6)DR   7)5-五月-2000   8)被预订   9)05-五月-2000

4 个答案:

答案 0 :(得分:2)

你可以像这样使用正则表达式:

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

答案 1 :(得分:0)

[3,null,null,"This API project is not authorized to use this API. Please ensure that this API is activated in the APIs Console: https://console.developers.google.com/apis/library?project=_ Please ensure this API is activated in the Google Developers Console: https://console.developers.google.com/apis/api/places_backend?project=_ For more information on authentication and Google Maps Javascript API services please see: https://developers.google.com/maps/documentation/javascript/get-api-key"] )

答案 2 :(得分:0)

foreach (string line in words)
{
    var split = line.Split('\t');
    sr = string.Join('\t', split.Take(split.Count() - 1));
    Quota = split.Last();
}

答案 3 :(得分:0)

使用linq

var counts = words.GroupBy(x => x)
                 .Select(g => new { Text = g.Key, Count = g.Count() });