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. How to spliting a string

How to spliting a string

Scheduled Pinned Locked Moved C#
tutorial
11 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.
  • M mrkeivan

    Hey Guys, I have a string consisting of zeros and ones like 10101010 I want to read each letter then convert it to code and use it ... I've checked the function Split() but it requires a character, and as u see mine doesn't have any. Regards, K

    N Offline
    N Offline
    Neh C
    wrote on last edited by
    #2

    Just use - ToCharArray()

    string strTest = "1100110101";
    char[] cList = strTest.ToCharArray();

    modified on Friday, April 16, 2010 7:33 AM

    realJSOPR 1 Reply Last reply
    0
    • M mrkeivan

      Hey Guys, I have a string consisting of zeros and ones like 10101010 I want to read each letter then convert it to code and use it ... I've checked the function Split() but it requires a character, and as u see mine doesn't have any. Regards, K

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #3

      Try using the Substring method of a string.

      R 1 Reply Last reply
      0
      • A Abhinav S

        Try using the Substring method of a string.

        R Offline
        R Offline
        ramzg
        wrote on last edited by
        #4

        Hi Abhinav, Isn't string.ToCharArray() a better way than using string.SubString()?

        Thanks, Ram

        1 Reply Last reply
        0
        • N Neh C

          Just use - ToCharArray()

          string strTest = "1100110101";
          char[] cList = strTest.ToCharArray();

          modified on Friday, April 16, 2010 7:33 AM

          realJSOPR Offline
          realJSOPR Offline
          realJSOP
          wrote on last edited by
          #5

          This is okay unless you don't want to allocate more memory in the process. Using string.Substring() would be a viable alternative. (I still marked your response as a "good answer". :) )

          .45 ACP - because shooting twice is just silly
          -----
          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

          N 1 Reply Last reply
          0
          • M mrkeivan

            Hey Guys, I have a string consisting of zeros and ones like 10101010 I want to read each letter then convert it to code and use it ... I've checked the function Split() but it requires a character, and as u see mine doesn't have any. Regards, K

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #6

            I have to ask - what does the string represent? A series of flags? If so, you could convert it to an integer, and then access it with logical AND (&) and logical OR (|) operators. Math operations are always faster/more efficient than string operations. To convert the string to an int, do this:

            string myString = "10101010";
            int flags = Convert.ToInt32(myString, 2);

            At that point, you can do this:

            int myVal = 4;
            if ((myVal & flags) == myVal)
            {
            // do something if 4 is a set value
            }

            .45 ACP - because shooting twice is just silly
            -----
            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

            M N 2 Replies Last reply
            0
            • realJSOPR realJSOP

              I have to ask - what does the string represent? A series of flags? If so, you could convert it to an integer, and then access it with logical AND (&) and logical OR (|) operators. Math operations are always faster/more efficient than string operations. To convert the string to an int, do this:

              string myString = "10101010";
              int flags = Convert.ToInt32(myString, 2);

              At that point, you can do this:

              int myVal = 4;
              if ((myVal & flags) == myVal)
              {
              // do something if 4 is a set value
              }

              .45 ACP - because shooting twice is just silly
              -----
              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

              M Offline
              M Offline
              mrkeivan
              wrote on last edited by
              #7

              I have access to each character of my string ! :D each character is a code, I'm trying to store days for a class so for instance its 1000000, it means its only Saturday, or 1010100 means it's on Saturday, Monday and Wednesday. Regards, K

              I 1 Reply Last reply
              0
              • realJSOPR realJSOP

                I have to ask - what does the string represent? A series of flags? If so, you could convert it to an integer, and then access it with logical AND (&) and logical OR (|) operators. Math operations are always faster/more efficient than string operations. To convert the string to an int, do this:

                string myString = "10101010";
                int flags = Convert.ToInt32(myString, 2);

                At that point, you can do this:

                int myVal = 4;
                if ((myVal & flags) == myVal)
                {
                // do something if 4 is a set value
                }

                .45 ACP - because shooting twice is just silly
                -----
                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                N Offline
                N Offline
                Not Active
                wrote on last edited by
                #8

                John Simmons / outlaw programmer wrote:

                Math operations are always faster/more efficient than string operations.

                Unless you run out of fingers and toes, then have to resort to paper and pencil. ;P


                I know the language. I've read a book. - _Madmatt

                M 1 Reply Last reply
                0
                • realJSOPR realJSOP

                  This is okay unless you don't want to allocate more memory in the process. Using string.Substring() would be a viable alternative. (I still marked your response as a "good answer". :) )

                  .45 ACP - because shooting twice is just silly
                  -----
                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                  N Offline
                  N Offline
                  Neh C
                  wrote on last edited by
                  #9

                  Yes, thanks for pointing that out.. :)

                  Regards, Neh

                  1 Reply Last reply
                  0
                  • M mrkeivan

                    I have access to each character of my string ! :D each character is a code, I'm trying to store days for a class so for instance its 1000000, it means its only Saturday, or 1010100 means it's on Saturday, Monday and Wednesday. Regards, K

                    I Offline
                    I Offline
                    Ian Shlasko
                    wrote on last edited by
                    #10

                    Well, then you can do that more cleanly if you want...

                    [Flags]
                    public enum DayFlags
                    {
                    Saturday = 64,
                    Sunday = 32,
                    Monday = 16,
                    Tuesday = 8,
                    Wednesday = 4,
                    Thursday = 2,
                    Friday = 1
                    }

                    public static bool IsFlagSet(this DayFlags en, DayFlags flag)
                    {
                    return ((int)en & (int)flag) != 0;
                    }

                    DayFlags days = (DayFlags)Convert.ToInt32(str, 2);

                    if (days.IsFlagSet(DayFlags.Sunday)) { ... }

                    Proud to have finally moved to the A-Ark. Which one are you in?
                    Author of the Guardians Saga (Sci-Fi/Fantasy novels)

                    1 Reply Last reply
                    0
                    • N Not Active

                      John Simmons / outlaw programmer wrote:

                      Math operations are always faster/more efficient than string operations.

                      Unless you run out of fingers and toes, then have to resort to paper and pencil. ;P


                      I know the language. I've read a book. - _Madmatt

                      M Offline
                      M Offline
                      mrkeivan
                      wrote on last edited by
                      #11

                      lol true ... that works all the time but due to the fact that we are getting lazier everyday these things become vital in our lives ! :D :D :D don't u agree ?

                      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