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. problems splitting string

problems splitting string

Scheduled Pinned Locked Moved C#
helptutorialquestion
7 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.
  • S Offline
    S Offline
    stephan_007
    wrote on last edited by
    #1

    hy everyone! i do have a problem. I have to split strings they are like

    "variable1", "variable_1", "variable"

    i do only have to parse the first line, but sometimes the first line looks like

    "variable11", "variable_11", "variable1"

    in this line the last "1" represents a color information. I do only have to parse the first line, in both cases the result should be variable1, variable_1, variable example: my input is

    variable1, variable_1, variable
    variable11, variable_11, variable1

    => result of first line: variable1, variable_1, variable if the input is

    variable11, variable_11, variable1
    variable1, variable_1, variable

    => result of first line: variable1, variable_1, variable does anyone have an idea of how to code this? It's quit tricky. I tried myself for some hours but found no solution which works propperly. Because I do not know if the first element has two "11" or just one! The string could also be in this order:

    variable, variable1, variable_1

    Maybe someone of you could help me please. thanks. Stephan.

    L G W 3 Replies Last reply
    0
    • S stephan_007

      hy everyone! i do have a problem. I have to split strings they are like

      "variable1", "variable_1", "variable"

      i do only have to parse the first line, but sometimes the first line looks like

      "variable11", "variable_11", "variable1"

      in this line the last "1" represents a color information. I do only have to parse the first line, in both cases the result should be variable1, variable_1, variable example: my input is

      variable1, variable_1, variable
      variable11, variable_11, variable1

      => result of first line: variable1, variable_1, variable if the input is

      variable11, variable_11, variable1
      variable1, variable_1, variable

      => result of first line: variable1, variable_1, variable does anyone have an idea of how to code this? It's quit tricky. I tried myself for some hours but found no solution which works propperly. Because I do not know if the first element has two "11" or just one! The string could also be in this order:

      variable, variable1, variable_1

      Maybe someone of you could help me please. thanks. Stephan.

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      stephan_007 wrote:

      does anyone have an idea of how to code this?

      Yes. Use the keyboard to type syntactically correct code that represents the logic needed to satisfy your requirements.

      led mike

      1 Reply Last reply
      0
      • S stephan_007

        hy everyone! i do have a problem. I have to split strings they are like

        "variable1", "variable_1", "variable"

        i do only have to parse the first line, but sometimes the first line looks like

        "variable11", "variable_11", "variable1"

        in this line the last "1" represents a color information. I do only have to parse the first line, in both cases the result should be variable1, variable_1, variable example: my input is

        variable1, variable_1, variable
        variable11, variable_11, variable1

        => result of first line: variable1, variable_1, variable if the input is

        variable11, variable_11, variable1
        variable1, variable_1, variable

        => result of first line: variable1, variable_1, variable does anyone have an idea of how to code this? It's quit tricky. I tried myself for some hours but found no solution which works propperly. Because I do not know if the first element has two "11" or just one! The string could also be in this order:

        variable, variable1, variable_1

        Maybe someone of you could help me please. thanks. Stephan.

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

        You have to specify which parts of the string that can vary, and how. For example, the repeated part "variable" in your example, is that always the specific word "variable", or could it be anything? If you have an actual example of some real data, that could help when trying to understand what you are trying to do.

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

        S 1 Reply Last reply
        0
        • S stephan_007

          hy everyone! i do have a problem. I have to split strings they are like

          "variable1", "variable_1", "variable"

          i do only have to parse the first line, but sometimes the first line looks like

          "variable11", "variable_11", "variable1"

          in this line the last "1" represents a color information. I do only have to parse the first line, in both cases the result should be variable1, variable_1, variable example: my input is

          variable1, variable_1, variable
          variable11, variable_11, variable1

          => result of first line: variable1, variable_1, variable if the input is

          variable11, variable_11, variable1
          variable1, variable_1, variable

          => result of first line: variable1, variable_1, variable does anyone have an idea of how to code this? It's quit tricky. I tried myself for some hours but found no solution which works propperly. Because I do not know if the first element has two "11" or just one! The string could also be in this order:

          variable, variable1, variable_1

          Maybe someone of you could help me please. thanks. Stephan.

          W Offline
          W Offline
          WBurgMo
          wrote on last edited by
          #4

          Assuming you parse the variable names into a string array, then for each element of the array examine the last character. If is numeric then examine the next to last character. If that character is also numeric then strip the last character. Of course this assumes that your color info is only a single digit.

          S 1 Reply Last reply
          0
          • G Guffa

            You have to specify which parts of the string that can vary, and how. For example, the repeated part "variable" in your example, is that always the specific word "variable", or could it be anything? If you have an actual example of some real data, that could help when trying to understand what you are trying to do.

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

            S Offline
            S Offline
            stephan_007
            wrote on last edited by
            #5

            well the input could be anything. the only difference between the two lines is, one has an additional "1" at the end of every fieldname. so if you remove the last "1" from the second line, then you will get the other. e.g. name, address1, address2, postalcode, city, phone_1, phone_2 name1, address11, addrress21, postalcode1, city1, phone_11, phone_21 So as you might have realized the second line is the first one with an additional "1" at the end of each entry. i guess meanwhile i found a solution to this problem: first check if every entry ends with 1, then it's the of the second type. if it is of the second type, then remove the last "1" by using Substring with the whole string without the last character (length-1). The last operation will change name1 to name and so on. But if the first operation returns, not every item has a "1" at it's end, then its of type line 1 and nothing has to be done. in code its something likestring[] Checkforcolorsign (string[] input) { int counter = 0; int i = 0; // check for "1" at the end (colorinfo) while (i < input.length - 1) { if (input[i].EndsWith("1")) { counter++; } } // if all elements had a "1" then it is a colorinfo => remove it if (counter == input.length) //as many hits as elements { for(i=0; i < input.length - 1; i++) { // remove the last character input[i] = input[i].SubString(0, input[i].Length - 1); } } return input; }
            Maybe this code will illustrate what I originally wanted to ask. I hope this code really does what I expect it to. so if the input is e.g. name1, address11, addrress21, postalcode1, city1, phone_11, phone_21 then the final result should be name, address1, address2, postalcode, city, phone_1, phone_2

            G 1 Reply Last reply
            0
            • W WBurgMo

              Assuming you parse the variable names into a string array, then for each element of the array examine the last character. If is numeric then examine the next to last character. If that character is also numeric then strip the last character. Of course this assumes that your color info is only a single digit.

              S Offline
              S Offline
              stephan_007
              wrote on last edited by
              #6

              that was an idea i already had, but it did not work. e.g. if the input is of this kind name1, address11, address21, postalcode1, city1 and the other line (without the colorsign "1" added) looks like name, address1, address2, postalcode, city it would work for the address elements but not for the other ones. i already posted some code which came to my mind when thinking about it without sitting in front of the comp. i have to check the syntax and if it works, meaning if it changes the line with the "1"s and doesn't change the line without. because that's what I want to do. the "task" is to read fieldvalues because i do have something like name|address1|address2|postalcode|city Mr. X, nowhere, here and there, 1234, noname city name1|address11|address21|postalcode1|city ..... but the two lines could be the otherway round as well, meaning name1|address11|address21|postalcode1|city Mr. X, nowhere, here and there, 1234, noname city name|address1|address2|postalcode|city and because every datafieldline is the same like the associated valuelines (the position of the fields not the values!), i do only read the first fieldline and use the indexes for every dataline. the fieldvalues are used for further operations (the ones without the "1" at the end). that's why i have to remove it.

              1 Reply Last reply
              0
              • S stephan_007

                well the input could be anything. the only difference between the two lines is, one has an additional "1" at the end of every fieldname. so if you remove the last "1" from the second line, then you will get the other. e.g. name, address1, address2, postalcode, city, phone_1, phone_2 name1, address11, addrress21, postalcode1, city1, phone_11, phone_21 So as you might have realized the second line is the first one with an additional "1" at the end of each entry. i guess meanwhile i found a solution to this problem: first check if every entry ends with 1, then it's the of the second type. if it is of the second type, then remove the last "1" by using Substring with the whole string without the last character (length-1). The last operation will change name1 to name and so on. But if the first operation returns, not every item has a "1" at it's end, then its of type line 1 and nothing has to be done. in code its something likestring[] Checkforcolorsign (string[] input) { int counter = 0; int i = 0; // check for "1" at the end (colorinfo) while (i < input.length - 1) { if (input[i].EndsWith("1")) { counter++; } } // if all elements had a "1" then it is a colorinfo => remove it if (counter == input.length) //as many hits as elements { for(i=0; i < input.length - 1; i++) { // remove the last character input[i] = input[i].SubString(0, input[i].Length - 1); } } return input; }
                Maybe this code will illustrate what I originally wanted to ask. I hope this code really does what I expect it to. so if the input is e.g. name1, address11, addrress21, postalcode1, city1, phone_11, phone_21 then the final result should be name, address1, address2, postalcode, city, phone_1, phone_2

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

                As far as I can tell, the code will work just fine. You don't have to return the array, though. You are changing the items in the array.

                void Checkforcolorsign (string[] input) {
                int counter = 0;
                // check for "1" at the end (colorinfo)
                foreach (string s in input) {
                if (s.EndsWith("1")) {
                counter++;
                }
                }
                // if all elements had a "1" then it is a colorinfo => remove it
                if (counter == input.length) //as many hits as elements {
                for (int i = 0; i < input.length - 1; i++) {
                // remove the last character
                input[i] = input[i].SubString(0, input[i].Length - 1);
                }
                }
                }

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

                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