James Blackburn
James Blackburn

Reputation: 616

Removing certain amount of characters before index found in string

I have a string which I need to remove certain characters from.

string note = "TextEntry_Slide_7|Notepad one I am going to enter the text all over the place :)|250887|0^TextEntry_Slide_10|Notepad two: wrilun3q 4p9834m ggddi :(|996052|2^TextEntry_Slide_14||774159|4^TextEntry_Slide_16|tnoinrgb rt trn n|805585|5"

I want to remove the ^ characters and also the 9 characters behind the ^ character. So the string will look like:

string note = "TextEntry_Slide_7|Notepad one I am going to enter the text all over the place :)TextEntry_Slide_10|Notepad two: wrilun3q 4p9834m ggddi :(TextEntry_Slide_14|TextEntry_Slide_16|tnoinrgb rt trn n|805585|5"

Also after that I need to remove the last 9 characters off the end of the string:

string note = "TextEntry_Slide_7|Notepad one I am going to enter the text all over the place :)TextEntry_Slide_10|Notepad two: wrilun3q 4p9834m ggddi :(TextEntry_Slide_14|TextEntry_Slide_16|tnoinrgb rt trn n"

I've already removed a ton of other stuff that was originally in the string note but I am stumped on how to do the above.

I found the index of the ^ character like note.IndexOf("^") but I am unsure what to do next to remove the 9 characters before it.

Any help will be greatly appreciated :)

Upvotes: 0

Views: 1261

Answers (5)

user613326
user613326

Reputation: 2180

I am not sure what your language is, but in vb.net I used instr() function a lot for this. instr tells you at what position it finds the first match of a string inside another string, and if it didn't find a string it returns 0 or negative.

Next if you want to strip strings in vb.net you can easily do this with the mid() function in combination with len() function, len tells of the length together with instr you can calculate what you want from a string.

If you like to do this in C# check out this url: http://www.dotnetcurry.com/ShowArticle.aspx?ID=189

Upvotes: 0

Daniel Brückner
Daniel Brückner

Reputation: 59645

First we strip of the carets and the nine preceding characters using a regular expression.

 var stepOne = Regex.Replace(input, @".{9}\^", String.Empty);

Then we just throw away the last nine characters.

 var stepTwo = stepOne.Remove(stepOne.Length - 9);

And you should probably add some error handling - for example if the string is shorter than nine characters after step one.

Upvotes: 1

Joshua
Joshua

Reputation: 4139

Sure, all you need is:

string output = Regex.Replace(note, @".{9}\^", string.Empty);
// remove last 9
output = output.Remove(output.Length - 9);

Upvotes: 1

newfurniturey
newfurniturey

Reputation: 38426

If you're using .IndexOf("^"), you can store that result/position into a temp-variable and then use a few .Substring() calls to rebuild your string.

Try something like:

int carotPos = note.IndexOf("^");
while (carotPos > -1) {
    if (carotPos <= 9) {
        note = note.Substring(carotPos);
    } else {
        note = note.Substring(0, (carotPos - 9)) + note.Substring(carotPos);
    }
    carotPos = note.IndexOf("^");
}

This will find the first ^ in the string and remove the leading 9 characters before it (including the ^). Then, it will find the next ^ in the string and repeat until there are none remaining.

To then drop the last 9 characters from the string, you do one more .Substring():

note = note.Substring(0, (note.Length - 9));

Upvotes: 0

Thymine
Thymine

Reputation: 9195

One simple way is Regex.Replace(note, ".{9,9}\\^", "");

And the obvious way to remove the last 9 characters would be note.Substring(0, note.length - 9);

Upvotes: 4

Related Questions