Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. SplitString

SplitString

Scheduled Pinned Locked Moved C#
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Are there any possibilities to split string like this : "hello world" "aaa bbb" "ccc" 10.5 20.1 30.4 it must be split like this : hello world aaa bbb ccc 10.5 20.1 30.4 Thx...

    A A W M 4 Replies Last reply
    0
    • L Lost User

      Are there any possibilities to split string like this : "hello world" "aaa bbb" "ccc" 10.5 20.1 30.4 it must be split like this : hello world aaa bbb ccc 10.5 20.1 30.4 Thx...

      A Offline
      A Offline
      Ash20
      wrote on last edited by
      #2

      Tokenized the string with delimiter as space ' '. There will be couple of API which does this. String.Split Method Thanks, Ashish

      1 Reply Last reply
      0
      • L Lost User

        Are there any possibilities to split string like this : "hello world" "aaa bbb" "ccc" 10.5 20.1 30.4 it must be split like this : hello world aaa bbb ccc 10.5 20.1 30.4 Thx...

        A Offline
        A Offline
        Abhijit Jana
        wrote on last edited by
        #3

        namespace ConsoleApplication2 { class Program { static void Main(string[] args) { string str = string.Empty; str="bbb ccc ddd 10.5 20.1 30.4"; string[] sstring = str.Split(' '); foreach (string s in sstring) { Console.WriteLine(s); } } } }

        Best Regards ----------------- Abhijit Jana Check Out My Latest Article Java.NET : Integration of Java and .NET[^] "Success is Journey it's not a destination"

        L 1 Reply Last reply
        0
        • A Abhijit Jana

          namespace ConsoleApplication2 { class Program { static void Main(string[] args) { string str = string.Empty; str="bbb ccc ddd 10.5 20.1 30.4"; string[] sstring = str.Split(' '); foreach (string s in sstring) { Console.WriteLine(s); } } } }

          Best Regards ----------------- Abhijit Jana Check Out My Latest Article Java.NET : Integration of Java and .NET[^] "Success is Journey it's not a destination"

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Please see my question again. Note the quote "aaa bbb" "ccc dddd" 10.1 20.2 You cannot just split using space.

          A 1 Reply Last reply
          0
          • L Lost User

            Are there any possibilities to split string like this : "hello world" "aaa bbb" "ccc" 10.5 20.1 30.4 it must be split like this : hello world aaa bbb ccc 10.5 20.1 30.4 Thx...

            W Offline
            W Offline
            wasimsharp
            wrote on last edited by
            #5

            by coding u can do this like that

            string[] mainstring;
            string[] s = string.Empty;
            string[] t = string.Empty;
            s = yourstring.spilt('"');

            for(int i=1;i<s.length;i++)>
            {
            if(i%2 != 0)
            {
            mianstring[i-1] = s[i]
            }
            int i = mainstring.length;
            t = mainstring[i].split(' ');
            i = i-1'
            foreach(string s in t)
            {
            mainstring[i+1] = s;

            }
            }

            wasim khan

            1 Reply Last reply
            0
            • L Lost User

              Please see my question again. Note the quote "aaa bbb" "ccc dddd" 10.1 20.2 You cannot just split using space.

              A Offline
              A Offline
              Abhijit Jana
              wrote on last edited by
              #6

              stancrm wrote:

              "aaa bbb" "ccc dddd" 10.1 20.2

              let me know how did you creting a string in this way ???:confused:

              Best Regards ----------------- Abhijit Jana Check Out My Latest Article Java.NET : Integration of Java and .NET[^] "Success is Journey it's not a destination"

              L 1 Reply Last reply
              0
              • A Abhijit Jana

                stancrm wrote:

                "aaa bbb" "ccc dddd" 10.1 20.2

                let me know how did you creting a string in this way ???:confused:

                Best Regards ----------------- Abhijit Jana Check Out My Latest Article Java.NET : Integration of Java and .NET[^] "Success is Journey it's not a destination"

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                string str = "\"aaa bbb\" \"ccc dddd\" 10.1 20.2";

                M 1 Reply Last reply
                0
                • L Lost User

                  string str = "\"aaa bbb\" \"ccc dddd\" 10.1 20.2";

                  M Offline
                  M Offline
                  Mbah Dhaim
                  wrote on last edited by
                  #8

                  1. split the string use space character 2. trim each returned string use " character i hope this usefull

                  dhaim program is hobby that make some money as side effect :)

                  1 Reply Last reply
                  0
                  • L Lost User

                    Are there any possibilities to split string like this : "hello world" "aaa bbb" "ccc" 10.5 20.1 30.4 it must be split like this : hello world aaa bbb ccc 10.5 20.1 30.4 Thx...

                    M Offline
                    M Offline
                    mabo42
                    wrote on last edited by
                    #9

                    Not tested:

                    public List<string> SplitLine(string line)
                    {
                    List<string> items = new List<string>();

                    MatchCollection matchCollection = Regex.Matches(line, "\"[^\"]*\"|[^\"]*");
                    foreach (Match match in matchCollection)
                    {
                    string group = match.Value.Trim();
                    if (group != string.Empty)
                    {
                    if (group[0] == '"')
                    {
                    items.Add(group.Trim(new char[] {'"'}));
                    }
                    else
                    {
                    //items.AddRange(group.Split(new char[] {'"'}, StringSplitOptions.RemoveEmptyEntries)); sorry, should be
                    items.AddRange(group.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries));
                    }
                    }
                    }
                    return items;
                    }

                    Hope this helps.

                    modified on Thursday, June 26, 2008 7:48 AM

                    L 1 Reply Last reply
                    0
                    • M mabo42

                      Not tested:

                      public List<string> SplitLine(string line)
                      {
                      List<string> items = new List<string>();

                      MatchCollection matchCollection = Regex.Matches(line, "\"[^\"]*\"|[^\"]*");
                      foreach (Match match in matchCollection)
                      {
                      string group = match.Value.Trim();
                      if (group != string.Empty)
                      {
                      if (group[0] == '"')
                      {
                      items.Add(group.Trim(new char[] {'"'}));
                      }
                      else
                      {
                      //items.AddRange(group.Split(new char[] {'"'}, StringSplitOptions.RemoveEmptyEntries)); sorry, should be
                      items.AddRange(group.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries));
                      }
                      }
                      }
                      return items;
                      }

                      Hope this helps.

                      modified on Thursday, June 26, 2008 7:48 AM

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      I just found the answer.

                      static string[] ParseStringUsingRegex(string input)
                      {
                      Regex regex = new Regex("\"([^\"]+?)\" ?|([^ ]+) ?| ");
                      MatchCollection matches = regex.Matches(input);

                      string[] result = new string[matches.Count];
                      for(int i = 0; i < matches.Count; i++)
                      {
                      Match match = matches[i];
                      result[i] = match.ToString().Replace("\"", "").Trim();
                      }
                      return result;
                      }

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups