Compare two strings and combine into a string.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Janice Hui
    New Member
    • Feb 2010
    • 16

    Compare two strings and combine into a string.

    Hi,

    I have two strings as below:

    Code:
    string a = "A, 0001; B, 0002; C, 0003;";
    string b = "0001, From, FlowA; 0002, To, FlowB; 0003, From, FlowC; 0003, To, FlowD;";
    I would like to produce a new string c with the output:

    A, From, FlowA; B, To, FlowB; C, From, FlowC; C, To, FlowD;

    Is that anyway for me to do it?
    Thank.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    You can use String.Split to split each source string into string arrays
    Then merge together whatever parts you want.

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      Looks like after you use String.Split twice, just use String.Replace with the items of the string arrays as arguments.

      Comment

      • harrisunderwork
        New Member
        • Feb 2010
        • 7

        #4
        You would have to apply a combination of string.split and string.replace to accomplish that.

        Comment

        • Janice Hui
          New Member
          • Feb 2010
          • 16

          #5
          Hi harrisunderwork ,

          Thank for your answer. Based on your answer, i should apply the string.replace in combination of string. However, string.replace is used to replace certain word in a sentences. In my case, i would like to combine this two string and at the same the similar word is needed to eliminate. Does string.replace can make it?

          Now, i'am using the foreach and array method together in order to get the output. Thank.

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            Yes, you would do something like:

            b = b.Replace("A", "0001");
            except "A" and "0001" would be variables which are set when you tokenize the string.

            Comment

            Working...