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. Splitting a string

Splitting a string

Scheduled Pinned Locked Moved C#
question
3 Posts 3 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
    kanchoette
    wrote on last edited by
    #1

    I was using the following code to split textbox text by a single space character:

    string words = notesTextBox.Text;
    string[] splitWords = words.Split(new Char[] { ' '});

    I now need to split the textbox text by either the space character or a carriage return. How can I do this please?

    M J 2 Replies Last reply
    0
    • K kanchoette

      I was using the following code to split textbox text by a single space character:

      string words = notesTextBox.Text;
      string[] splitWords = words.Split(new Char[] { ' '});

      I now need to split the textbox text by either the space character or a carriage return. How can I do this please?

      M Offline
      M Offline
      Moreno Airoldi
      wrote on last edited by
      #2

      You can add chars to your array:

      string words = notesTextBox.Text;
      string[] splitWords = words.Split(new Char[] { ' ', '\r' });

      2+2=5 for very large amounts of 2 (always loved that one hehe!)

      1 Reply Last reply
      0
      • K kanchoette

        I was using the following code to split textbox text by a single space character:

        string words = notesTextBox.Text;
        string[] splitWords = words.Split(new Char[] { ' '});

        I now need to split the textbox text by either the space character or a carriage return. How can I do this please?

        J Offline
        J Offline
        Jason Barry
        wrote on last edited by
        #3

        If you need to split the text by a carriage return and/or space character, you can use the following code sample:

        string words = notesTextBox.Text;
        words = words.Replace("\r\n", " ");
        string[] splitWords = words.Split(new char[] { ' ' });

        This will replace all the carriage returns with spaces, then split the text at each space. If you do what Moreno said and just add the '\r' and '\n' characters to the array, you will end up getting more split words in your array. You will have to try each way and see which best fits your needs.

        string words = notesTextBox.Text;
        string[] splitWords = words.Split(new char[] { ' ', '\r', '\n' });

        modified on Monday, August 10, 2009 11:55 AM

        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