locked
how to split string. RRS feed

  • Question

  • User-649116597 posted

    I want to split my input string according to digit. this is all passenger name. It can be change, may be 2 or 10,

    input string:-  1.CHIA/CHER LING(ADT)   2.KUA/LILY(ADT)   3.KWA/ENG ENG(YCD)                 
      4.TAN/YONG PHONG(ADT)   5.YEO/CHIN GEE BERNARD(YCD)

    o/p:- 1.CHIA/CHER LING(ADT)

             2.KUA/LILY(ADT)

             3.KWA/ENG ENG(YCD)

             4.TAN/YONG PHONG(ADT)

             5.YEO/CHIN GEE BERNARD(YCD)

            

    Friday, May 10, 2013 6:52 AM

Answers

  • User79986525 posted

    Hi ,

    check this

     string newval = "";
              Int32 newstring =0;
              string s = " 1.CHIA/CHER LING(ADT)   2.KUA/LILY(ADT)   3.KWA/ENG ENG(YCD) 4.TAN/YONG PHONG(ADT)   5.YEO/CHIN GEE BERNARD(YCD)";
       string[] newarray=new string[10];
        Int32 count = 0;
            for (int j = 0; j < 10; j++)
            
            {
                //string result = "";
                newstring = s.IndexOf(j.ToString());
                if (newstring > 0)
                {  
                     newval = s.Substring(0, newstring);
                    if (newval.Trim() != "")
                    {
                        newarray[count] = newval.Trim().ToString();
                        //result += ","+ newval;
                        count++;
                    }
    
                    if (newval != "")
                    {
                        s = s.Substring(newstring);
                       int  next = s.IndexOf((j+1).ToString());
                       if (next <= 0)
                        {
                            newarray[count] = s.ToString();
                        }
                    }
                }
            }
            for (int k = 0; k < newarray.Length; k++)
            {
                if (newarray[k] != null)
                {
                    Console.WriteLine(newarray[k].ToString());
                }
            }
    

    In NewArray you get what you want .........

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 13, 2013 1:40 AM

All replies

  • User-1800438376 posted

    input string:-  1.CHIA/CHER LING(ADT)   2.KUA/LILY(ADT)   3.KWA/ENG ENG(YCD)                 
      4.TAN/YONG PHONG(ADT)   5.YEO/CHIN GEE BERNARD(YCD)

    string strData = "1.CHIA/CHER LING(ADT)|2.KUA/LILY(ADT)|3.KWA/ENG ENG(YCD)|4.TAN/YONG PHONG(ADT)|5.YEO/CHIN GEE BERNARD(YCD)"
    
    string [] arr = strData.split('|').toArray();
    
    for(int intCtr =0 ; intCtr < arr.Len; intCtr++)
    {
       Console.WriteLine(arr[intCtr].toString());
    }
    
    

    Friday, May 10, 2013 7:12 AM
  • User-649116597 posted

    Hey why you put this symbol ( | ) here. this is not part of the input string.

    Friday, May 10, 2013 7:15 AM
  • User-1800438376 posted

    replace '|' with ' ' but make sure you will not get space in the string itself

    Friday, May 10, 2013 7:34 AM
  • User1083584480 posted

    use below code

    string abc = " 1.CHIA/CHER LING(ADT)   2.KUA/LILY(ADT)   3.KWA/ENG ENG(YCD) 4.TAN/YONG PHONG(ADT)   115.YEO/CHIN GEE BERNARD(YCD)";
                int length = abc.Length;
                int counter = 0;
                int lastindex = 0, currentIndex = 0;
                List<string> objstr = new List<string>();
                string str = "";
                while (counter < length)
                {
                    currentIndex = abc.IndexOfAny("1234567890".ToCharArray());
                    if (currentIndex < 0) break;
                    lastindex = currentIndex + 1;
                    while (lastindex >= 0)
                    {
                        lastindex = abc.IndexOfAny("1234567890".ToCharArray(), lastindex);
                        if (lastindex - currentIndex > 1 || lastindex < 0) break;
                        currentIndex = lastindex++;

                    }
                    str = abc.Substring(0, lastindex < 0 ? abc.Length  : lastindex);
                    abc = abc.Replace(str, "");
                    objstr.Add(str);
                    Console.WriteLine(str);
                }

    Friday, May 10, 2013 8:00 AM
  • User79986525 posted

    Hi,

    try somthing like this

    string s = " 1.CHIA/CHER LING(ADT)   2.KUA/LILY(ADT)   3.KWA/ENG ENG(YCD) 4.TAN/YONG PHONG(ADT)   5.YEO/CHIN GEE BERNARD(YCD)";
            for (int j = 0; j < 10; j++)
            {
                var newstring = s.IndexOf(j.ToString());
                if (newstring > 0)
                {
                    //var nextstr=s.IndexOf((j+1).ToString());
                    string newval = s.Substring(0,newstring);
                    if (newval != "")
                    {
                        s = s.Substring(newstring);
                    }
                }
            }

    Hope this will help you .......

    Friday, May 10, 2013 8:26 AM
  • User-1716253493 posted
    if the number less than 10, you can use str.Replace("2","-2") loop through 9. then str.split('-')
    Friday, May 10, 2013 9:14 AM
  • User-649116597 posted

    @Sanjay verma... 

     In this statment :- var newstring = output.IndexOf(j.ToString());

    when I debug this code. I get var newstring index value is -1.

    Friday, May 10, 2013 9:35 AM
  • User-649116597 posted

    @lucky..

    I tried this statment :- string[] arr = fix.Split(' ').ToArray();

    but I got tomuch extra indexes. I don't want extra indexes. When I got two passenger name in input string then I want only two indexes.

    Friday, May 10, 2013 9:46 AM
  • User-1916342745 posted

    using System.Text.RegularExpressions;

    ....

    var items2 = Regex.Matches(inp, @"\d{1,2}[^\d]*");

    foreach (var i in items2)
    {
    Console.WriteLine(i);

    }

    Friday, May 10, 2013 9:52 AM
  • User79986525 posted

    Hi abamboria,

    Yes it's because if it's not get the value we looking for the it gives -1

    var newstring = output.IndexOf(j.ToString());

    so i use the condition

    if(newstring >0)

    if you run the whole code then you get value in

     string newval = s.Substring(0,newstring);
    one by one as per your requirement
    the last value you get in
     s = s.Substring(newstring);

    try to run the whole code and check it step by step

    if still have any problem let me know

    Thanks
    Friday, May 10, 2013 9:53 AM
  • User-1916342745 posted
    using System;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string inp = "1. item one 2. Item two 3. Item three 4. Item 55. last item";
                var items = Regex.Matches(inp, @"\d{1,2}[^\d]*");
                foreach (var i in items)
                {
                    Console.WriteLine(i);
                }
                Console.Read();
            }
        }
    }

    Hope this helps. 

    Friday, May 10, 2013 9:55 AM
  • User-649116597 posted

    I debug the whole code but it give only one name out of 2. It's enter only 2 times in this condition if (newstring > 0){}

    first time it give both 2 names together then next time only starting name.

    Friday, May 10, 2013 10:13 AM
  • User-434868552 posted

    @ abamboria              TIMTOWTDI  =.  there is more than one way to do it

    Example:

    String passengers = "1.CHIA/CHER LING(ADT)   2.KUA/LILY(ADT)   3.KWA/ENG ENG(YCD) 4.TAN/YONG PHONG(ADT)   5.YEO/CHIN GEE BERNARD(YCD)";
    
    // String.Replace Method (String, String) http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx
    String passengersToSplit = passengers.Replace(")", ")$");
    
    // String.Split Method:  String.Split Method (Char[])  http://msdn.microsoft.com/en-us/library/b873y76a.aspx
    String[] passengerList = passengersToSplit.Split(new Char[] {'$'});
    
    foreach (String passenger in passengerList)
    {
        Console.WriteLine (passenger.Trim());    
    }

    output:

    1.CHIA/CHER LING(ADT)
    2.KUA/LILY(ADT)
    3.KWA/ENG ENG(YCD)
    4.TAN/YONG PHONG(ADT)
    5.YEO/CHIN GEE BERNARD(YCD)

    g.

    Friday, May 10, 2013 11:42 AM
  • User-649116597 posted

    Thanks for reply.

    If we don't have (ADT) after passenger name like the below sample

    1.WROBEL/TERESA   2.WROBEL/MAREK   

    then how to perform function.

    Friday, May 10, 2013 11:50 AM
  • User-434868552 posted

    @ abamboria                You're welcome.

    abamboria, you're changing the rules in the middle of the game, so to speak.

    BTW, is this a homework assignment?

    (a) in your O.P. (original post), your examples all had an airport:
         ADT - Ada Municipal Airport, OK, USA 
         YCD - Nanaimo, BC, Canada

    (b) in your O.P., some of the names also had embedded spaces:
          3.KWA/ENG ENG(YCD)  

    (c) your O.P. likely had no leading or trailing spaces.

    So, before your peers here at forums.asp.net can help you properly, we really need, as do you, to understand your data.

    Questions include:

    -- in addition to cases like
        1.WROBEL/TERESA   2.WROBEL/MAREK 
        1.CHIA/CHER LING(ADT)   2.KUA/LILY(ADT)   3.KWA/ENG ENG(YCD)  4.TAN/YONG PHONG(ADT)   5.YEO/CHIN GEE BERNARD(YCD)
       what other cases can we expect?:
    ?? 1.CHIA/CHER LING(ADT)   2.WROBEL/MAREK   3.KWA/ENG ENG(YCD)

    -- will there always be THREE spaces between each entry?

    NOTE:  if your data is NOT consistent in some way, your problem becomes substantially more difficult.

    If you will always have three spaces between your items, you could modify my previous example to be like this:

    String passengersToSplit = passengers.Replace("   ", "$");

    However, your data is inconsistent, hence:

    1.CHIA/CHER LING(ADT)
    2.KUA/LILY(ADT)
    3.KWA/ENG ENG(YCD) 4.TAN/YONG PHONG(ADT) <=== not three spaces
    5.YEO/CHIN GEE BERNARD(YCD)

    abamboria, please give us a better description of your data.

    This article might be useful to you:  "clarity is important both in question and in answer" http://weblogs.asp.net/gerrylowry/archive/2012/11/10/clarity-is-important-both-in-question-and-in-answer.aspx

    g.

    Edit:

    perhaps you could tell us also the story behind your data ... why are you getting data in this format in the first place?

    end Edit.

    Friday, May 10, 2013 12:32 PM
  • User1833159707 posted

    Good question, it made me think a bit. Here's a solution that will work for the most part. It's another RegEx solution that uses non-capturing positive lookaheads. The only issue is that it will split the begining of the string before the first number. Hope this is helpful for you.

     

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string subjectString = "1.CHIA/CHER LING(ADT)   2.KUA/LILY(ADT)   3.KWA/ENG ENG(YCD)   4.TAN/YONG PHONG(ADT)   5.YEO/CHIN GEE BERNARD(YCD)";
                List<string> split = Regex.Split(subjectString, @"(?=\d)").ToList();
    
                foreach (string str in split)
                {
                    Console.WriteLine(str);
                }
    
            }
        }
    }

    Saturday, May 11, 2013 1:41 AM
  • User-158764254 posted

    bholtzman

    The only issue is that it will split the begining of the string before the first number.

    it also has extra splits if the text happened to contain a digit and it would not split correctly if the number prefixing the text is a multi digit number like 10 - something i think the OP indicated as a possibility in his thread starter post.  The regex match patterns previously posted also have these issues too.

    The split pattern could be made more specific if you look for a space, followed by one or more digits followed by a period.

    (?= \d+\.)

    caveats to this regex split pattern:

    1. if the original string has a leading space, then you get an empty element 0. avoid this by pre trimming the input string
    2. since we use look ahead to preserve the split characters and we're including the space character in the pattern, your array results will have that leading space character.  simple enough to trim each of the results though.

    Saturday, May 11, 2013 2:31 PM
  • User1833159707 posted

    That's true there would be extra splits with multi digit numbers. I see that I didn't understand the question correctly. I was providing an answer for the given string. Thanks for the help mbanavige!

    Saturday, May 11, 2013 5:11 PM
  • User-649116597 posted

    @mbanavige :- you are correct. When I execute this code. it's include space so I got three indexes in the list array.

    string fix = Regex.Replace(output, @"^\s*$\n", string.Empty, RegexOptions.Multiline).TrimEnd();
                    //string[] output1 = Regex.Split(fix, @"(?<=\d\.)([^\s]*)(?=\s)");
                    List<string> split = Regex.Split(fix, @"(?=\d)").ToList();
    
    
                    foreach (string str in split)
                    {
                        dr = dt.NewRow();
                        dr["Passenger Name"] = str;
                    }



    Sunday, May 12, 2013 4:02 AM
  • User79986525 posted

    Hi ,

    check this

     string newval = "";
              Int32 newstring =0;
              string s = " 1.CHIA/CHER LING(ADT)   2.KUA/LILY(ADT)   3.KWA/ENG ENG(YCD) 4.TAN/YONG PHONG(ADT)   5.YEO/CHIN GEE BERNARD(YCD)";
       string[] newarray=new string[10];
        Int32 count = 0;
            for (int j = 0; j < 10; j++)
            
            {
                //string result = "";
                newstring = s.IndexOf(j.ToString());
                if (newstring > 0)
                {  
                     newval = s.Substring(0, newstring);
                    if (newval.Trim() != "")
                    {
                        newarray[count] = newval.Trim().ToString();
                        //result += ","+ newval;
                        count++;
                    }
    
                    if (newval != "")
                    {
                        s = s.Substring(newstring);
                       int  next = s.IndexOf((j+1).ToString());
                       if (next <= 0)
                        {
                            newarray[count] = s.ToString();
                        }
                    }
                }
            }
            for (int k = 0; k < newarray.Length; k++)
            {
                if (newarray[k] != null)
                {
                    Console.WriteLine(newarray[k].ToString());
                }
            }
    

    In NewArray you get what you want .........

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 13, 2013 1:40 AM
  • User1083584480 posted

    have you tried my below code it will work even when you have n number of digit in string.

    string abc = " 1.CHIA/CHER LING(ADT)   2.KUA/LILY(ADT)   3.KWA/ENG ENG(YCD) 4.TAN/YONG PHONG(ADT)   115.YEO/CHIN GEE BERNARD(YCD)";
                int length = abc.Length;
                int counter = 0;
                int lastindex = 0, currentIndex = 0;
                List<string> objstr = new List<string>();
                string str = "";
                while (counter < length)
                {
                    currentIndex = abc.IndexOfAny("1234567890".ToCharArray());
                    if (currentIndex < 0) break;
                    lastindex = currentIndex + 1;
                    while (lastindex >= 0)
                    {
                        lastindex = abc.IndexOfAny("1234567890".ToCharArray(), lastindex);
                        if (lastindex - currentIndex > 1 || lastindex < 0) break;
                        currentIndex = lastindex++;

                    }
                    str = abc.Substring(0, lastindex < 0 ? abc.Length  : lastindex);
                    abc = abc.Replace(str, "");
                    objstr.Add(str);
                    Console.WriteLine(str);
                }

    Monday, May 13, 2013 3:24 AM
  • User-649116597 posted

    Thanks Sanjay,

    It's work.

    Monday, May 13, 2013 3:59 AM