Word Separation Dynamically in C #

I am creating a project in which I want to group words dynamically before I divided and grouped them as static

if I need to insert (1 100000888888888 4949494949 17032 HYB DR 25-May-2000 booked May 05, 2000)

OUTPUT 1) 1 2) 100000888888888 3) 4949494949 4) 17032 5) HYB 6) DR 7) 5-May-2000 8) order 9) 05-May-2000

my cs code for this was

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

} but now the values ​​from the user are inserted as

(1 1 000008 88888888 494949 4949 170 32 HYB DR May 25, 2000 booked May 05, 2000) but the output should be the same

1) 1 2) 1000008888888888 3) 4949494949 4) 17032 5) HYB 6) DR 7) 5-May-2000 8) order 9) 05-May-2000

-5
4

:

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
+2
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;
    }
}
0
foreach (string line in words)
{
    var split = line.Split('\t');
    sr = string.Join('\t', split.Take(split.Count() - 1));
    Quota = split.Last();
}
0
source

using linq

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

All Articles