Reputation: 311
got a little Stringcutting problem. I got a string like this:
@VAR;Variable=Deteministic;Value=mostly;Note=Unless Slave is already in use;Op==;@ENDVAR;
the Note is not neccessary for me, so I want to cut out everything that begins with Note until the next Semicolon. The Replace-Method would be nice, but I dont know how to get the Chars after Note away.
I tried something like this:
int index1 = rs.IndexOf("Note=");
int index2 = rs.IndexOf(';', index1+1);
rs = rs.Remove(index1, index2);
I thought this should be sufficient, but it fails if there is no note and my program pops out an error. Regex would be an option, but I cant think of one the fits in here.
Please note, the example was just a sample. I dont know how the inputstring looks like. One line can contain two Notes and another line can have none. Please consider this in your answers.
Upvotes: 1
Views: 1087
Reputation: 1407
Your code should look like this
int index1 = rs.IndexOf(";Note=");
while(index1 > -1)
{
int index2 = rs.IndexOf(';', index1 + 1);
rs = rs.Remove(index1, index2);
index1 = rs.IndexOf(";Note=");
}
Upvotes: 0
Reputation: 35944
Most of the other regex answers to this post will indeed work, but it might be appropriate with a non-regex answer just to show that there are other tools available than the hammer :)
I'm assuming you just want to ignore all entries starting with Note=
string input = "@VAR;Variable=Deteministic;Value=mostly;Note=Unless Slave is already in use;Op==;@ENDVAR;";
// Entries will contain all entries except those starting with 'Note='
string[] entries = input.Split(';').Where(s => !s.StartsWith("Note=")).ToArray();
// If you want to, you can put it all back together without the Note entries
string output = String.Join(";", entries);
// Ouput: @VAR;Variable=Deteministic;Value=mostly;Op==;@ENDVAR;
Console.WriteLine(output);
// Output:
// @VAR
// Variable=Deteministic
// Value=mostly
// Op==
// @ENDVAR
foreach (var entry in entries)
Console.WriteLine(entry);
Upvotes: 2
Reputation: 172468
A regex can indeed help here:
rs = Regex.Replace(s, "(?<=;)Note=.*?;", "");
Let me explain the more obscure parts of it:
(?<=;)
makes sure Note
is preceded by a semicolon. That semicolon is, however, not part of the replacement. (That's a positive look-behind assertion).
.*?;
matches all characters until the semicolon, but non-greedy. This ensures that Note=A;x=B;
is only matched until the first semicolon and x=B
is retained.
Upvotes: 7
Reputation: 71598
You can use the regex:
Note[^;]+;?
Something a bit like:
rs = Regex.Replace(rs, @"\bNote[^;]+;?", "");
\b
matches a word boundary.
[^;]
matches any character which is not a semicolon.
Upvotes: 1
Reputation: 10486
First use StringBuilder
. It is much more efficient that string
. Then if you want to preserve you original idea:
int index1 = -1;
while( (index1 = sb.IndexOf("Note=")) >=0)
{
int index2 = rs.IndexOf(';', index1+1);
rs = rs.Remove(index1, index2);
}
Upvotes: 1
Reputation: 73502
You mean this ?
var regex = new Regex(@"Note.*?;");
var ouput = regex.Replace(input, "");
Upvotes: 1