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. Split function in c#?

Split function in c#?

Scheduled Pinned Locked Moved C#
csharpquestion
8 Posts 4 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.
  • D Offline
    D Offline
    dynamic
    wrote on last edited by
    #1

    hi does anyone know if there's an equivalent to the Split function in c# ?

    D 1 Reply Last reply
    0
    • D dynamic

      hi does anyone know if there's an equivalent to the Split function in c# ?

      D Offline
      D Offline
      Daniel Turini
      wrote on last edited by
      #2

      See String.Split and Regex.Split. Kant wrote: Actually she replied back to me "You shouldn't fix the bug. You should kill it"

      D 1 Reply Last reply
      0
      • D Daniel Turini

        See String.Split and Regex.Split. Kant wrote: Actually she replied back to me "You shouldn't fix the bug. You should kill it"

        D Offline
        D Offline
        dynamic
        wrote on last edited by
        #3

        cheers , although i'm struggling with it a bit, if i want to seperate the first space in a row of text, how would it be done? all i can find is info on arrays of stuff ( eg: i want to split this line "hello i'm a test" , so that all i get is "i'm" ) but all i've managed to find is some really complex stuff on msdn about splitting whole lines:~ in vb6 / vb.net i've always been able to do this... Dim text as string text = Split("hello i'm a test",chr(32))(1) /// bringing back the "i'm" bit any ideas?

        J D I 3 Replies Last reply
        0
        • D dynamic

          cheers , although i'm struggling with it a bit, if i want to seperate the first space in a row of text, how would it be done? all i can find is info on arrays of stuff ( eg: i want to split this line "hello i'm a test" , so that all i get is "i'm" ) but all i've managed to find is some really complex stuff on msdn about splitting whole lines:~ in vb6 / vb.net i've always been able to do this... Dim text as string text = Split("hello i'm a test",chr(32))(1) /// bringing back the "i'm" bit any ideas?

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          Both the C# version and the VB6 version return an array, you are just accessing a specific element of that array in your example. This should work: string word = "hello i'm a test".Split(' ')[1]; To be more verbose, you are setting the variable word to the first element in the array returned by calling the Split method on the string "hello i'm a test". The large downfall with the .NET version of Split is that it only splits on characters not strings. I don't think this is a problem with Regex's but I don't know for sure. James "I despise the city and much prefer being where a traffic jam means a line-up at McDonald's" Me when telling a friend why I wouldn't want to live with him

          1 Reply Last reply
          0
          • D dynamic

            cheers , although i'm struggling with it a bit, if i want to seperate the first space in a row of text, how would it be done? all i can find is info on arrays of stuff ( eg: i want to split this line "hello i'm a test" , so that all i get is "i'm" ) but all i've managed to find is some really complex stuff on msdn about splitting whole lines:~ in vb6 / vb.net i've always been able to do this... Dim text as string text = Split("hello i'm a test",chr(32))(1) /// bringing back the "i'm" bit any ideas?

            D Offline
            D Offline
            Daniel Turini
            wrote on last edited by
            #5

            dynamic wrote: Dim text as string text = Split("hello i'm a test",chr(32))(1) Code this on C# as:

            string text = string.Split("hello i'm a test", ' ')[1];

            Kant wrote: Actually she replied back to me "You shouldn't fix the bug. You should kill it"

            D 1 Reply Last reply
            0
            • D Daniel Turini

              dynamic wrote: Dim text as string text = Split("hello i'm a test",chr(32))(1) Code this on C# as:

              string text = string.Split("hello i'm a test", ' ')[1];

              Kant wrote: Actually she replied back to me "You shouldn't fix the bug. You should kill it"

              D Offline
              D Offline
              dynamic
              wrote on last edited by
              #6

              cheers guys that worked great:-D i've only decided this week to make a move over from vb6 / vb.net to c# so it's all new , although i'm beginning to grasp the concepts a bit now. thanks a lot.

              1 Reply Last reply
              0
              • D dynamic

                cheers , although i'm struggling with it a bit, if i want to seperate the first space in a row of text, how would it be done? all i can find is info on arrays of stuff ( eg: i want to split this line "hello i'm a test" , so that all i get is "i'm" ) but all i've managed to find is some really complex stuff on msdn about splitting whole lines:~ in vb6 / vb.net i've always been able to do this... Dim text as string text = Split("hello i'm a test",chr(32))(1) /// bringing back the "i'm" bit any ideas?

                I Offline
                I Offline
                imoz
                wrote on last edited by
                #7

                using System; namespace testString { class testString { public static void Main(string[] args) { string secondword = ""; string text = "hello i'm a test"; string[] fragments = text.Split(new char[] {' '}); if( fragments.Length > 2 ) { secondword = fragments[1]; } Console.WriteLine(text + "\n" + secondword); } } }
                This will return i'm.

                I 1 Reply Last reply
                0
                • I imoz

                  using System; namespace testString { class testString { public static void Main(string[] args) { string secondword = ""; string text = "hello i'm a test"; string[] fragments = text.Split(new char[] {' '}); if( fragments.Length > 2 ) { secondword = fragments[1]; } Console.WriteLine(text + "\n" + secondword); } } }
                  This will return i'm.

                  I Offline
                  I Offline
                  imoz
                  wrote on last edited by
                  #8

                  You can use the code sample I posted above just to test and see it write out the second word to the command line. Alternatively, here is a method you can add to your class or a utility class to just get the second word. This will return the 2nd word, or a zero-length string if there is no 2nd word. Yes I was bored :-) Time to sleep now.private string GetSecondWord(string TextIn) { string secondword = ""; string[] fragments = TextIn.Split(new char[] {' '}); if( fragments.Length > 2 ) { secondword = fragments[1]; } return secondword; }
                  Happy To Help, Joe Mozelesky imoz technologies, llc :: powered by imoz

                  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