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

Manipulating a string

Scheduled Pinned Locked Moved C#
help
6 Posts 4 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
    Ryno Burger
    wrote on last edited by
    #1

    Hi guys, I needs some help with this challenge I'm facing please. This is my challenge: I have a string with "parent object" and item in the format of pipe delimited between "parent objects" and comma delimited between items. Like this string sample = "Object1**,**Item1**,**Item2**-**Object2**,**Item1**,**Item2**-**Object3**,**Item1**,**Item2"; How will I go about manipulating the string to use the "objects" and items in this format. I basically need to get the "object" and the items and in some cases I will need to work with one entire entry at a time, lets say Object1,Item1,Item2. I am not to familiar with the System.Stringfunctions yet and would love if anybody around here could show me how this could be done or possibly some references. Thanks. R

    H G R K 5 Replies Last reply
    0
    • R Ryno Burger

      Hi guys, I needs some help with this challenge I'm facing please. This is my challenge: I have a string with "parent object" and item in the format of pipe delimited between "parent objects" and comma delimited between items. Like this string sample = "Object1**,**Item1**,**Item2**-**Object2**,**Item1**,**Item2**-**Object3**,**Item1**,**Item2"; How will I go about manipulating the string to use the "objects" and items in this format. I basically need to get the "object" and the items and in some cases I will need to work with one entire entry at a time, lets say Object1,Item1,Item2. I am not to familiar with the System.Stringfunctions yet and would love if anybody around here could show me how this could be done or possibly some references. Thanks. R

      H Offline
      H Offline
      Harini N K
      wrote on last edited by
      #2

      Hi I have added comments for each line. Hope it helps you.

      string StrObjectItems = "";
      StrObjectItems = "Object1,Item1,Item2-Object2,Item1,Item2-Object3,Item1,Item2";

      //You need two string arrays - one for each pipeline and another is that each comma within pipeline strings

      string[] StrObjectsArray = StrObjectItems.Split(new char[] { '-' });

      //loop thro' each objects as you have splitted into array of strings.
      for (int i = 0; i <= StrObjectsArray.GetUpperBound(0); i++)
      {
      //this string will have first object that is 'Object1,Item1,Item2'
      string StrObjects = StrObjectsArray[i].ToString();

      //again split the above string as an array
      string[] StrItemsArray = StrObjects.Split(new char[] { ',' });

      //loop thro' each objects' items as you have splitted into array of strings.
      for (int j = 0; j <= StrItemsArray.GetUpperBound(0); j++)
      {
      string StrItems = StrItemsArray[j].ToString();
      }
      }

      -- modified at 5:23 Tuesday 6th March, 2007

      Rate this message. Thank you. Harini :)

      1 Reply Last reply
      0
      • R Ryno Burger

        Hi guys, I needs some help with this challenge I'm facing please. This is my challenge: I have a string with "parent object" and item in the format of pipe delimited between "parent objects" and comma delimited between items. Like this string sample = "Object1**,**Item1**,**Item2**-**Object2**,**Item1**,**Item2**-**Object3**,**Item1**,**Item2"; How will I go about manipulating the string to use the "objects" and items in this format. I basically need to get the "object" and the items and in some cases I will need to work with one entire entry at a time, lets say Object1,Item1,Item2. I am not to familiar with the System.Stringfunctions yet and would love if anybody around here could show me how this could be done or possibly some references. Thanks. R

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

        I don't see any pipe (|) characters in the string. I suppose that you mean that the parent strings are delimited by a dash (-). To get the parent string in an array, split on a dash: string[] parents = sample.Split('-'); The property parents.Length will give you the number of strings. To get the child strings of a parent, split it on a comma: string[] children = parents[0].Split(',');

        --- single minded; short sighted; long gone;

        1 Reply Last reply
        0
        • R Ryno Burger

          Hi guys, I needs some help with this challenge I'm facing please. This is my challenge: I have a string with "parent object" and item in the format of pipe delimited between "parent objects" and comma delimited between items. Like this string sample = "Object1**,**Item1**,**Item2**-**Object2**,**Item1**,**Item2**-**Object3**,**Item1**,**Item2"; How will I go about manipulating the string to use the "objects" and items in this format. I basically need to get the "object" and the items and in some cases I will need to work with one entire entry at a time, lets say Object1,Item1,Item2. I am not to familiar with the System.Stringfunctions yet and would love if anybody around here could show me how this could be done or possibly some references. Thanks. R

          R Offline
          R Offline
          Ryno Burger
          wrote on last edited by
          #4

          Thanks to both of you that replied. It all makes sense now. Thank you once again! R

          1 Reply Last reply
          0
          • R Ryno Burger

            Hi guys, I needs some help with this challenge I'm facing please. This is my challenge: I have a string with "parent object" and item in the format of pipe delimited between "parent objects" and comma delimited between items. Like this string sample = "Object1**,**Item1**,**Item2**-**Object2**,**Item1**,**Item2**-**Object3**,**Item1**,**Item2"; How will I go about manipulating the string to use the "objects" and items in this format. I basically need to get the "object" and the items and in some cases I will need to work with one entire entry at a time, lets say Object1,Item1,Item2. I am not to familiar with the System.Stringfunctions yet and would love if anybody around here could show me how this could be done or possibly some references. Thanks. R

            K Offline
            K Offline
            Keshav V Kamat 0
            wrote on last edited by
            #5

            Hi. the following line looks for the character '-' in the string. the flag checks whether it is true or not. then it checks where the character is found, and the substrings are taken into other strings. bool flag=false; flag=sample.Contains("-"); if(flag==true) { int m=sample.IndexOf("-"); string s1 = sample.Substring(0, m); // s1 will contain Object1,Item1,Item2 sample = sample.Remove(0, m+1); // now the sample will contain //Object2,Item1,Item2-Object3,Item1,Item2 } Hence you can repeat the procedure to store the other substrings, maybe in a string arraylist. i hope this helps. keshav kamat siemens india

            1 Reply Last reply
            0
            • R Ryno Burger

              Hi guys, I needs some help with this challenge I'm facing please. This is my challenge: I have a string with "parent object" and item in the format of pipe delimited between "parent objects" and comma delimited between items. Like this string sample = "Object1**,**Item1**,**Item2**-**Object2**,**Item1**,**Item2**-**Object3**,**Item1**,**Item2"; How will I go about manipulating the string to use the "objects" and items in this format. I basically need to get the "object" and the items and in some cases I will need to work with one entire entry at a time, lets say Object1,Item1,Item2. I am not to familiar with the System.Stringfunctions yet and would love if anybody around here could show me how this could be done or possibly some references. Thanks. R

              K Offline
              K Offline
              Keshav V Kamat 0
              wrote on last edited by
              #6

              Alternatively, one can use the Split funtion depending upon a special character. string[] string1=sample.Split("-"); the number of members are identified by the string1.Length property, which will give you the count of the members in the new string after the split. Keshav Kamat India.

              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