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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. string to string array

string to string array

Scheduled Pinned Locked Moved C#
csharpdata-structuresquestionlearning
17 Posts 7 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.
  • K Offline
    K Offline
    keroed_edmond
    wrote on last edited by
    #1

    is it possible for me to convert a strig to a string array in c# and if so how?? beginner in C#

    J 1 Reply Last reply
    0
    • K keroed_edmond

      is it possible for me to convert a strig to a string array in c# and if so how?? beginner in C#

      J Offline
      J Offline
      Josh Smith
      wrote on last edited by
      #2

      String has the Split method, which should be what you are looking for. josh

      K 1 Reply Last reply
      0
      • J Josh Smith

        String has the Split method, which should be what you are looking for. josh

        K Offline
        K Offline
        keroed_edmond
        wrote on last edited by
        #3

        yes andi normally use the split function for example if i hava a string="walk,keny,go" i will get all off the values like walk or kenny or go by setting the delimeter to "," but supposei have a string="lets walk to the suppermarket" is there anyway for me to access each character by themself for example i know if i was puting this in an array. array[o] would be l and array[1] would be e ???? u have any idea how i could do this? or any link to any example if it might be too tediuos to show me how to do it????? beginner c#

        S E W 3 Replies Last reply
        0
        • K keroed_edmond

          yes andi normally use the split function for example if i hava a string="walk,keny,go" i will get all off the values like walk or kenny or go by setting the delimeter to "," but supposei have a string="lets walk to the suppermarket" is there anyway for me to access each character by themself for example i know if i was puting this in an array. array[o] would be l and array[1] would be e ???? u have any idea how i could do this? or any link to any example if it might be too tediuos to show me how to do it????? beginner c#

          S Offline
          S Offline
          Stefan Troschuetz
          wrote on last edited by
          #4

          You could use String.ToCharArray() or maybe simply call String.SubString(index, index+1).


          "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

          www.troschuetz.de

          K 1 Reply Last reply
          0
          • K keroed_edmond

            yes andi normally use the split function for example if i hava a string="walk,keny,go" i will get all off the values like walk or kenny or go by setting the delimeter to "," but supposei have a string="lets walk to the suppermarket" is there anyway for me to access each character by themself for example i know if i was puting this in an array. array[o] would be l and array[1] would be e ???? u have any idea how i could do this? or any link to any example if it might be too tediuos to show me how to do it????? beginner c#

            E Offline
            E Offline
            Ed Poore
            wrote on last edited by
            #5

            Does it have to be a string array, if you're only using single characters then the string.ToCharArray() will do it exactly.  But if you want to use strings then I think you will have to write a loop which walks through, adding them to a list as it goes.


            You know you're a Land Rover owner when the best route from point A to point B is through the mud. Ed

            K V 2 Replies Last reply
            0
            • S Stefan Troschuetz

              You could use String.ToCharArray() or maybe simply call String.SubString(index, index+1).


              "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

              www.troschuetz.de

              K Offline
              K Offline
              keroed_edmond
              wrote on last edited by
              #6

              what would index and index+1 be for example string.substring(0,1) and this will return the first and second element??

              S 1 Reply Last reply
              0
              • E Ed Poore

                Does it have to be a string array, if you're only using single characters then the string.ToCharArray() will do it exactly.  But if you want to use strings then I think you will have to write a loop which walks through, adding them to a list as it goes.


                You know you're a Land Rover owner when the best route from point A to point B is through the mud. Ed

                K Offline
                K Offline
                keroed_edmond
                wrote on last edited by
                #7

                i would prefer it to prob be a string because as i go alopngiwant to tets my data to see what is bieng saved but everytime i try to set a textboxt.text=charArray[0] i get an error saying char cannot be converted to string hence if it was stored in a string array i cold prob display it in the textbox or i dont know if is the way i am trying to display it is wrong. is there a way display char in your textbox do u have any samples onlinei could look on that shows u how to loop through a string character by character kenny

                E G 2 Replies Last reply
                0
                • K keroed_edmond

                  what would index and index+1 be for example string.substring(0,1) and this will return the first and second element??

                  S Offline
                  S Offline
                  Stefan Troschuetz
                  wrote on last edited by
                  #8

                  Ups, I didn't look hard enough at the docs. First parameter is the startIndex, so if you want the "l" from your example string it should be 0. Second paramter is length, so it has to be 1.


                  "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                  www.troschuetz.de

                  K 1 Reply Last reply
                  0
                  • K keroed_edmond

                    i would prefer it to prob be a string because as i go alopngiwant to tets my data to see what is bieng saved but everytime i try to set a textboxt.text=charArray[0] i get an error saying char cannot be converted to string hence if it was stored in a string array i cold prob display it in the textbox or i dont know if is the way i am trying to display it is wrong. is there a way display char in your textbox do u have any samples onlinei could look on that shows u how to loop through a string character by character kenny

                    E Offline
                    E Offline
                    Ed Poore
                    wrote on last edited by
                    #9

                    keroed_edmond wrote:

                    textboxt.text=charArray[0]

                    Try textboxt.text=charArray[0].ToString(); :doh:


                    You know you're a Land Rover owner when the best route from point A to point B is through the mud. Ed

                    1 Reply Last reply
                    0
                    • K keroed_edmond

                      yes andi normally use the split function for example if i hava a string="walk,keny,go" i will get all off the values like walk or kenny or go by setting the delimeter to "," but supposei have a string="lets walk to the suppermarket" is there anyway for me to access each character by themself for example i know if i was puting this in an array. array[o] would be l and array[1] would be e ???? u have any idea how i could do this? or any link to any example if it might be too tediuos to show me how to do it????? beginner c#

                      W Offline
                      W Offline
                      Wjousts
                      wrote on last edited by
                      #10

                      You can treat a regular string like a char array and just index it:string mystring = "hello"; char h = mystring[0]; // h now contains the char 'h'

                      1 Reply Last reply
                      0
                      • K keroed_edmond

                        i would prefer it to prob be a string because as i go alopngiwant to tets my data to see what is bieng saved but everytime i try to set a textboxt.text=charArray[0] i get an error saying char cannot be converted to string hence if it was stored in a string array i cold prob display it in the textbox or i dont know if is the way i am trying to display it is wrong. is there a way display char in your textbox do u have any samples onlinei could look on that shows u how to loop through a string character by character kenny

                        G Offline
                        G Offline
                        Guffa
                        wrote on last edited by
                        #11

                        You don't have to convert the string at all to access the separate characters. Just use an index to access the characters: string message = "So long and thanks for all the fish."; char c1 = message[0]; // get the 'S' character char c2 = message[35]; // get the '.' character You can easily convert a char to a string: string s1 = new string(c1); // creates the "S" string string s2 = new string(c2); // creates the "." string There are many ways to get a single character from a string into a new string. Here are some: textbox.Text = new string(message[0]); textbox.Text = message.SubString(0, 1); textbox.Text = new string(message.ToCharArray()[0]); textbox.Text = new string(message.ToCharArray(), 0, 1); textbox.Text = new string(message.ToCharArray(0, 1)); textbox.Text = new string((char)(new StringReader(message).Read())); --- b { font-weight: normal; }

                        E 1 Reply Last reply
                        0
                        • S Stefan Troschuetz

                          Ups, I didn't look hard enough at the docs. First parameter is the startIndex, so if you want the "l" from your example string it should be 0. Second paramter is length, so it has to be 1.


                          "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

                          www.troschuetz.de

                          K Offline
                          K Offline
                          keroed_edmond
                          wrote on last edited by
                          #12

                          Thanks it works perfectly kenny

                          1 Reply Last reply
                          0
                          • G Guffa

                            You don't have to convert the string at all to access the separate characters. Just use an index to access the characters: string message = "So long and thanks for all the fish."; char c1 = message[0]; // get the 'S' character char c2 = message[35]; // get the '.' character You can easily convert a char to a string: string s1 = new string(c1); // creates the "S" string string s2 = new string(c2); // creates the "." string There are many ways to get a single character from a string into a new string. Here are some: textbox.Text = new string(message[0]); textbox.Text = message.SubString(0, 1); textbox.Text = new string(message.ToCharArray()[0]); textbox.Text = new string(message.ToCharArray(), 0, 1); textbox.Text = new string(message.ToCharArray(0, 1)); textbox.Text = new string((char)(new StringReader(message).Read())); --- b { font-weight: normal; }

                            E Offline
                            E Offline
                            Ed Poore
                            wrote on last edited by
                            #13

                            Bit of an overkill for what he wants.


                            You know you're a Land Rover owner when the best route from point A to point B is through the mud. Ed

                            G 1 Reply Last reply
                            0
                            • E Ed Poore

                              Bit of an overkill for what he wants.


                              You know you're a Land Rover owner when the best route from point A to point B is through the mud. Ed

                              G Offline
                              G Offline
                              Guffa
                              wrote on last edited by
                              #14

                              Yes, of course some of the alternatives are. I just thought that I'd show that there is more than one way to skin a cat. Anyway, I'm not really sure what he wants, and frankly I don't think he is either... ;) --- b { font-weight: normal; }

                              E 1 Reply Last reply
                              0
                              • G Guffa

                                Yes, of course some of the alternatives are. I just thought that I'd show that there is more than one way to skin a cat. Anyway, I'm not really sure what he wants, and frankly I don't think he is either... ;) --- b { font-weight: normal; }

                                E Offline
                                E Offline
                                Ed Poore
                                wrote on last edited by
                                #15

                                keroed_edmond wrote:

                                textboxt.text=charArray[0]

                                Based on that, I think he just wants to display the first character in the array.


                                You know you're a Land Rover owner when the best route from point A to point B is through the mud. Ed

                                1 Reply Last reply
                                0
                                • E Ed Poore

                                  Does it have to be a string array, if you're only using single characters then the string.ToCharArray() will do it exactly.  But if you want to use strings then I think you will have to write a loop which walks through, adding them to a list as it goes.


                                  You know you're a Land Rover owner when the best route from point A to point B is through the mud. Ed

                                  V Offline
                                  V Offline
                                  vernchen
                                  wrote on last edited by
                                  #16

                                  you can use split(',');

                                  E 1 Reply Last reply
                                  0
                                  • V vernchen

                                    you can use split(',');

                                    E Offline
                                    E Offline
                                    Ed Poore
                                    wrote on last edited by
                                    #17

                                    vernchen wrote:

                                    you can use split(',');

                                    Not unless there are comma's seperating all characters ;P  If you look at the above thread you'll see that what he needed was the ToString() method :doh:


                                    You know you're a Land Rover owner when the best route from point A to point B is through the mud. Ed

                                    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