-1

I have the following piece of code:

string FullNote = "aaa bbb ccc";
string ExistingAdminNote = "bbb";

string[] NoteDifference = FullNote.Split(new string[] { ExistingAdminNote }, StringSplitOptions.None);

for (int ii = 0; ii < NoteDifference.Length; ii++)
    Response.Write("INCREMENT: " + ii + "--" + NoteDifference[ii] +"<br>");

Which outputs this:

INCREMENT: 0--aaa 
INCREMENT: 1-- ccc

But what I want it to output is this:

INCREMENT: 0--aaa 
INCREMENT: 1--bbb 
INCREMENT: 2-- ccc

eg to also add the bbb into the array, at the correct index.

I know I can write some code to compare the strings etc, but is there a function that can achieve this without me having to write some custom code?

Basically I want to find the changes to the original string. It does not need to be done in this way, as long as I can find the differences between the 2 strings.

PS I do not know what the delimiter will be - it could be anything

Alex
  • 3,730
  • 9
  • 43
  • 94
  • See http://stackoverflow.com/q/521146/87698. Remember to Regex.Escape your separator first. – Heinzi Sep 30 '16 at 14:15
  • Per you edit about the differing delimiter, so you're saying the input could be "aaa%bbb%ccc"? – rory.ap Sep 30 '16 at 14:18
  • 1
    If you don't know what the delimiter is going to be, and you don't know what the values are going to be, then what you're trying to do is just not possible. – rory.ap Sep 30 '16 at 14:19
  • @rory.ap yes it could be anything, I am trying to find the changes to the original string, and then add the initials of the person who made the change – Alex Sep 30 '16 at 14:19
  • What is wrong with this question that warrants a downvote? Anonymous downvoters please explain – Alex Sep 30 '16 at 14:22
  • If the original string is `bbb` and whoever edits it writes `bbb` before and/or after it, then how do you know which bit the original string is? I'm not sure you've given enough information on the expected inputs and outputs - perhaps provide a few examples. – Charles Mager Sep 30 '16 at 14:23
  • Could be "unclear what you're asking" because you didn't clarify the delimiters. I down voted because it's "not useful" given my prior comment. – rory.ap Sep 30 '16 at 14:23

3 Answers3

2

You're splitting on bbb when you should be splitting on the space character. Your delimiter string will not show up on your result array. The delimiter needs to be the common string that separates the values that you want.

rory.ap
  • 34,009
  • 10
  • 83
  • 174
0

Couldn't you just do something fairly simple as this:

      string FullNote = "aaa bbb ccc bbb ddd bbb eee";
      string ExistingAdminNote = "bbb";

      string[] NoteDifference = FullNote.Split(new string[] { ExistingAdminNote }, StringSplitOptions.None);

      int index = 0;
      for (int ii = 0; ii < NoteDifference.Length; ii++)
      {
        ResponseWriteLine("INCREMENT: " + index++ + "--" + NoteDifference[ii] + "<br>");
        if (ii < NoteDifference.Length - 1)
          Response.WriteLine("INCREMENT: " + index++ + "--" + ExistingAdminNote + "<br>");
      }
-1

Use ' ' (space) charater to split your string then your NoteDifference array will have 3 elements

Maddy
  • 774
  • 5
  • 14