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 a string into parts by losing no character in c#

split a string into parts by losing no character in c#

Scheduled Pinned Locked Moved C#
csharpdata-structureshelp
5 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.
  • R Offline
    R Offline
    rahuladya
    wrote on last edited by
    #1

    hello forum i am spliting a string into parts on the occurance of characther a by using split method like

    string a = "advanced";
    string[] b = new string[10];
    b=a.Split('a');

    but by this method i m losing character a from the string array it is returning

    b[0]=
    b[1]=dv
    b[2]=nced

    i want to get

    b[0]=a
    b[1]=dv
    b[2]=a
    b[3]=nced

    can any body help me regards rahul adya

    A G 2 Replies Last reply
    0
    • R rahuladya

      hello forum i am spliting a string into parts on the occurance of characther a by using split method like

      string a = "advanced";
      string[] b = new string[10];
      b=a.Split('a');

      but by this method i m losing character a from the string array it is returning

      b[0]=
      b[1]=dv
      b[2]=nced

      i want to get

      b[0]=a
      b[1]=dv
      b[2]=a
      b[3]=nced

      can any body help me regards rahul adya

      A Offline
      A Offline
      Ashfield
      wrote on last edited by
      #2

      Unfortunaltely thats the way split works, you need to write some looping code to do what you require. Have a look at string.IndexOf Hope this helps

      Bob Ashfield Consultants Ltd

      1 Reply Last reply
      0
      • R rahuladya

        hello forum i am spliting a string into parts on the occurance of characther a by using split method like

        string a = "advanced";
        string[] b = new string[10];
        b=a.Split('a');

        but by this method i m losing character a from the string array it is returning

        b[0]=
        b[1]=dv
        b[2]=nced

        i want to get

        b[0]=a
        b[1]=dv
        b[2]=a
        b[3]=nced

        can any body help me regards rahul adya

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

        rahuladya wrote:

        string[] b = new string[10]; b=a.Split('a');

        Why do you create an array and assign to the b variable, when you are assigning a new variable to it on the next line?

        rahuladya wrote:

        i want to get b[0]=a b[1]=dv b[2]=a b[3]=nced

        You can use a regular expression to split the string:

        string a = "advanced";
        string[] b = Regex.Split(a, "(a)");

        Or for any string you like:

        string a = "advanced";
        string splitter = "a";
        string[] b = Regex.Split(a, "(" + Regex.Escape(splitter) + ")");

        Despite everything, the person most likely to be fooling you next is yourself.

        A R 2 Replies Last reply
        0
        • G Guffa

          rahuladya wrote:

          string[] b = new string[10]; b=a.Split('a');

          Why do you create an array and assign to the b variable, when you are assigning a new variable to it on the next line?

          rahuladya wrote:

          i want to get b[0]=a b[1]=dv b[2]=a b[3]=nced

          You can use a regular expression to split the string:

          string a = "advanced";
          string[] b = Regex.Split(a, "(a)");

          Or for any string you like:

          string a = "advanced";
          string splitter = "a";
          string[] b = Regex.Split(a, "(" + Regex.Escape(splitter) + ")");

          Despite everything, the person most likely to be fooling you next is yourself.

          A Offline
          A Offline
          Ashfield
          wrote on last edited by
          #4

          Nice one. Never thought of regex. :)

          Bob Ashfield Consultants Ltd

          1 Reply Last reply
          0
          • G Guffa

            rahuladya wrote:

            string[] b = new string[10]; b=a.Split('a');

            Why do you create an array and assign to the b variable, when you are assigning a new variable to it on the next line?

            rahuladya wrote:

            i want to get b[0]=a b[1]=dv b[2]=a b[3]=nced

            You can use a regular expression to split the string:

            string a = "advanced";
            string[] b = Regex.Split(a, "(a)");

            Or for any string you like:

            string a = "advanced";
            string splitter = "a";
            string[] b = Regex.Split(a, "(" + Regex.Escape(splitter) + ")");

            Despite everything, the person most likely to be fooling you next is yourself.

            R Offline
            R Offline
            rahuladya
            wrote on last edited by
            #5

            Thanx Guffa it works very well... regards Rahul adya

            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