-7

I am creating a project in which i want to group the words dynamically previously i Split and grouped them as static

if i need to insert (1 100000888888888 4949494949 17032 HYB DR 25-May-2000 Booked 05-May-2000)

OUTPUT 1) 1 2)100000888888888 3)4949494949 4)17032 5)HYB 6)DR 7)5-May-2000 8)Booked 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 user are inserting like

(1 1 000008 88888888 494949 4949 170 32 HYB DR 25-May-2000 Booked 05-May-2000) but the output must be same

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

Ibibo
  • 167
  • 1
  • 4
  • 21
  • what kind of data do you sort? and are those arrays the same? because you named them `words1` and `word1` – wake-0 Jun 25 '16 at 08:17
  • i edited the question @KevinWallis – Ibibo Jun 25 '16 at 08:21
  • What should the output of `153 81 2612GEN` be? – C4d Jun 25 '16 at 08:23
  • @C4u the output should be sr=153 81 and quota=GEN – Ibibo Jun 25 '16 at 08:25
  • @Ibibo And what happend to `2612`? – C4d Jun 25 '16 at 08:25
  • @C4u look its depend on user if user inserted 153 81 2612 then sr will be 153 81 2612 but if user inserted 153 81 then sr will be 153 81 its depend on user what he inserted (153 81 2612) this sr is consist of 3 words and (153 81) and this consist of 2 words so the grouping of string must be dynamic – Ibibo Jun 25 '16 at 08:28
  • 1
    @Ibibo Easy said you just want to split numbers and words? – C4d Jun 25 '16 at 08:29
  • @C4u i can't explain what i needed ya u r write i want to split words and its an example of 2 strings i have 14 strings i want to dynamically split the words – Ibibo Jun 25 '16 at 08:31
  • How do you want to GROUP the results? It is easy to split, but you have not explained what the groups should look like. – jdweng Jun 25 '16 at 08:33
  • actually i have a textbox in which a user will enter his detail **1 100000888888888 4949494949 17032 HYB DR 25-May-2000 Booked 05-May-2000** and the code is ` 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');` – Ibibo Jun 25 '16 at 08:35
  • `foreach (string s1 in words) { string text1 = s1; string[] words1 = text1.Split('\t'); int a = words1.Length; { sr = words1[1].ToString(); transid = words1[2].ToString(); pnr = words1[3].ToString(); trainno = words1[4].ToString(); fr = words1[5].ToString(); tt = words1[6].ToString(); doj = words1[7].ToString(); reservestat = words1[8].ToString(); dobook = words1[9].ToString(); }` – Ibibo Jun 25 '16 at 08:39
  • 2
    Update your question if you have more details - comments are for comments and not for adding more details – Sir Rufo Jun 25 '16 at 08:41
  • @SirRufo after 90 minutes will be completed I will Ask The Same Question Again – Ibibo Jun 25 '16 at 09:38
  • 2
    If you ask the same question again, please expect my duplicate close vote :o) – Sir Rufo Jun 25 '16 at 09:53
  • @ibibo Please edit the question showing actual inputs and expected outputs, covering all the important cases. – ClickRick Jun 25 '16 at 09:58
  • @Ibibo This is no commercial place where you can demand a special response time. All of here is free of charge and good will of the users answering questions in their private time. So please stay calm – Sir Rufo Jun 25 '16 at 10:19
  • @SirRufo point noted but i think this problem is nothing in front of many people as i am fresher in the feild – Ibibo Jun 25 '16 at 10:38
  • @Ibibo You should seriously reformat you question and maybe rethink the text. Cleary show what you've got and what you want to get. – C4d Jun 25 '16 at 11:14

4 Answers4

2

You can go with regex in here like this:

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

This will result in:

input = "153 81 2612GEN";

  • SR: 153 81 2612
  • Quota: GEN

input = "153 81 1 1 1 1 1 1 ABCDE";

  • SR: 153 81 1 1 1 1 1 1
  • Quota: ABCDE

input = "123 AB";

  • SR: 123
  • Quota: AB
C4d
  • 3,183
  • 4
  • 29
  • 50
  • but i have total 10 strings `string text; string sr = ""; string Quota= ""; string pnr = ""; string trainno = ""; string fr = ""; string tt = ""; string doj = ""; string reservestat = ""; string dobook = "";` – Ibibo Jun 25 '16 at 08:41
  • According to comments to the question `"153 81 2612GEN"` should be splitted into `"153 81"` and `"GEN"`, please notice the abscence of `"2612"` – Dmitry Bychenko Jun 25 '16 at 08:43
  • @DmitryBychenko Check the comments below the line you noticed. There is a contradiction in his explanation. I assume he really wants to split numbers by words. – C4d Jun 25 '16 at 08:51
  • @Ibibo repeat the step of write a method for it. – C4d Jun 25 '16 at 08:51
  • @C4u sorry sir i am unable to explain – Ibibo Jun 25 '16 at 08:53
  • look i dont want to split numbers by word i want to split the string For Eg I Will Take For String Now `string sr **46853 3653 6387** string Quota **GEN** string Name **Ibibo c#** string Add **India Mumbai**` It should be edited Like this – Ibibo Jun 25 '16 at 08:54
0
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;
    }
}
denchu
  • 839
  • 7
  • 10
0
foreach (string line in words)
{
    var split = line.Split('\t');
    sr = string.Join('\t', split.Take(split.Count() - 1));
    Quota = split.Last();
}
Artyom
  • 3,507
  • 2
  • 34
  • 67
0

using linq

var counts = words.GroupBy(x => x)
                 .Select(g => new { Text = g.Key, Count = g.Count() });
MMM
  • 3,132
  • 3
  • 20
  • 32