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. question about split method c#

question about split method c#

Scheduled Pinned Locked Moved C#
questioncsharp
7 Posts 5 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.
  • F Offline
    F Offline
    fady_sayegh
    wrote on last edited by
    #1

    hi to all, how can i split a string value with a seperator of many character. like this : string MyString = "abc|@def|@hij|@klm|@" after the split i want the following result : abc def hij klm best regrads and thanks in advance fady

    I L M 3 Replies Last reply
    0
    • F fady_sayegh

      hi to all, how can i split a string value with a seperator of many character. like this : string MyString = "abc|@def|@hij|@klm|@" after the split i want the following result : abc def hij klm best regrads and thanks in advance fady

      I Offline
      I Offline
      Ingo
      wrote on last edited by
      #2

      Try: string test = "abc|@def|@hij|@klm|@"; char[] seperators = {'|'}; string[] splitted = test.Replace("@","").Split(seperators); Then you get an array of 5! strings, the last is empty. Greetings, Ingo ------------------------------ An bug in a Microsoft Product? No! It's not a bug it's an undocumented feature!

      1 Reply Last reply
      0
      • F fady_sayegh

        hi to all, how can i split a string value with a seperator of many character. like this : string MyString = "abc|@def|@hij|@klm|@" after the split i want the following result : abc def hij klm best regrads and thanks in advance fady

        L Offline
        L Offline
        Le centriste
        wrote on last edited by
        #3

        If it is the same string sequence, String.Split will do the job.

        string[] myStrings = myString.Split(new char[] { '|', '@' }, StringSplitOptions.RemoveEmptyEntries);

        Not that the StringSplitOptions is new to 2.0. If you are still in 1.1, you have 2 choices: 1- filter the empty strings when reading the array. 2- Use Regex.Split instead:

        string[] myStrings = Regex.Split(myString, "[\|@]+");

        The regular expression means "split on | or @, which may appear one or more times. Unfortunately, the Regex one leaves an empty entry at the end, because your string finishes with the "|@" sequence. -------- "I say no to drugs, but they don't listen." - Marilyn Manson

        1 Reply Last reply
        0
        • F fady_sayegh

          hi to all, how can i split a string value with a seperator of many character. like this : string MyString = "abc|@def|@hij|@klm|@" after the split i want the following result : abc def hij klm best regrads and thanks in advance fady

          M Offline
          M Offline
          mav northwind
          wrote on last edited by
          #4

          Hi! What I usually do is to replace the separator strings with single characters that do not otherwise occurr inside the string and then perform a regular Split. Something like this:

          string MyString = "abc|@def|@hij|@klm|@";
          string MyNewString = MyString.Replace("|@", "\0");
          string[] Parts = MyNewString.Split('\0');

          You should get the expected result. You have to be careful, though: Since your string ends with a separator, you'll get an additional empty string as last entry in your result array! Regards, mav

          L 1 Reply Last reply
          0
          • M mav northwind

            Hi! What I usually do is to replace the separator strings with single characters that do not otherwise occurr inside the string and then perform a regular Split. Something like this:

            string MyString = "abc|@def|@hij|@klm|@";
            string MyNewString = MyString.Replace("|@", "\0");
            string[] Parts = MyNewString.Split('\0');

            You should get the expected result. You have to be careful, though: Since your string ends with a separator, you'll get an additional empty string as last entry in your result array! Regards, mav

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            mav.northwind wrote:

            string MyNewString = MyString.Replace("|@", "\0");

            Funny, I think I mistakenly did that the other day and it blew up in my face! :confused:

            xacc.ide-0.1.2.3
            Now with full keyboard customization

            M 1 Reply Last reply
            0
            • L leppie

              mav.northwind wrote:

              string MyNewString = MyString.Replace("|@", "\0");

              Funny, I think I mistakenly did that the other day and it blew up in my face! :confused:

              xacc.ide-0.1.2.3
              Now with full keyboard customization

              M Offline
              M Offline
              mav northwind
              wrote on last edited by
              #6

              I must admit that I wasn't sure that '\0' would behave nicely until I tried. But now that I actually tried it there's no real reason why it shouldn't work - .NET strings aren't \0-terminated, so \0 should be a character like every other. Only difference is that you'll have a hard time trying to insert a \0 manually in your text :) Regards, mav

              L 1 Reply Last reply
              0
              • M mav northwind

                I must admit that I wasn't sure that '\0' would behave nicely until I tried. But now that I actually tried it there's no real reason why it shouldn't work - .NET strings aren't \0-terminated, so \0 should be a character like every other. Only difference is that you'll have a hard time trying to insert a \0 manually in your text :) Regards, mav

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                mav.northwind wrote:

                .NET strings aren't \0-terminated

                Try tell that to Console.WriteLine() ;P

                xacc.ide-0.1.2.3
                Now with full keyboard customization

                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