Share via


Removing part of a string with starts with and ends with

Question

Tuesday, August 17, 2010 3:10 PM

hello, i need to be able to remove a part of a string that can be different every time i remove it.

like say i have this string:

@"MappedImage 0123456789ABCDEF
Texture = HAPPYBRIAN.tga
TextureWidth = 512
TextureHeight = 512
Coords = Left:184 Top:306 Right:330 Bottom:389
Status = NONE
End

MappedImage 0123456789ABCDEFXX
Texture = HAPPYBRIAN.tga
TextureWidth = 512
TextureHeight = 512
Coords = Left:232 Top:144 Right:438 Bottom:331
Status = NONE
End"

i need to find the part of the string that begins with ("MappedImage " + NameOf) and ends with ("End") and then replace that part of the string with "". how could i do this?

-StumpyINC

All replies (14)

Tuesday, August 17, 2010 8:05 PM ✅Answered | 1 vote

okay, im sorta new to C# and have never used regular expressions before so im still confused. where would i add the MappedImage's name? the string that has the name of the mapped image is called entryName, so as in the example of the string i posted, entryName would be = to "0123456789ABCDEF". what does the ".*" do? sorry for all the questions

-StumpyINC

In that case it would simply be changed to:

System.Text.RegularExpressions.Regex.Replace(inputString, "MappedImage " + entryName + ".*End", "");

'.' means any character, and '*' means zero or more of the preceeding value, so .* is a string containing anything and of any length (including nothing).  Fortunetly the regular expression is smart enough to know the the .* ends when it reaches the next token (in this case "End" so you don't need to do something weird like defining a string of any length that doesn't contain the string "End".  It'll take care of that for you.

Oh, and you probably need to specify that this is a multi-line replace.  It's just another parameter for the options, and multi-line is one of them.  See the documentation.


Tuesday, August 17, 2010 8:13 PM ✅Answered | 1 vote

Off the top of my head...

private void button1_Click(object sender, RoutedEventArgs e)
    {
      string mystring = @"MappedImage 0123456789ABCDEF
Texture = HAPPYBRIAN.tga
TextureWidth = 512
TextureHeight = 512
Coords = Left:184 Top:306 Right:330 Bottom:389
Status = NONE
End

MappedImage 0123456789ABCDEFXX
Texture = HAPPYBRIAN.tga
TextureWidth = 512
TextureHeight = 512
Coords = Left:232 Top:144 Right:438 Bottom:331
Status = NONE
End";
      string myNewString;
      if (mystring.Contains("MappedImage")) //this is the begining of this message
      {
        myNewString = GetValueBetween(mystring, "MappedImage", "End");
        mystring = myNewString.Replace(myNewString, "");
      }
      MessageBox.Show(mystring);
}
public static string GetValueBetween(string theline, string startString, string endString)
    {
      string value;
      try
      {
        int startAt = theline.IndexOf(startString);
        string substr = theline.Substring(startAt + startString.Length);
        int endAt = substr.IndexOf(endString);
        if (endAt < 0) //there is no end!
        {
          endAt = substr.Length;
        }
        value = substr.Substring(0, endAt);
      }
      catch (Exception)
      {
        throw;
      }
      return value;
    }

I couldn't figure out where NameOf was, or what you wanted to do exactly, but the gist of the question I think is answered in my GetValueBetween routine.


Tuesday, August 17, 2010 3:16 PM

I recommend you add a "Begin" in front of each one to make this more clean.

Also, is this in memory or from a file?

If from a file, you should read each line until you get to a Begin, then, start saving each line until you get to an End. Then search for the next Begin, rinse repeat.

If from memory, you could split the string by the newline delimiter. Then, you'll have an array of the entire thing. Foreach through each index , look for Begin, start saving each one after that until you get to End. Rinse, repeat.

 


Tuesday, August 17, 2010 3:19 PM

I'd recommend to use Regular Expressions.

For more information: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx

The other way would be to find where is the MappedImage string (someStr.IndexOf("MappedImage")), find where is the End (someStr.IndexOf("End")) and then remove what's in the middle. Just be careful not to find another END string inside it!

Zaiden http://zPod.com.ar


Tuesday, August 17, 2010 3:19 PM

System.Text.RegularExpressions.Regex.Replace(inputString, "MappedImage.*End", "");

You can use the static method above, or you can make an instance of the Regex class if you're going to be using it repeatedly.  (See documentation for both how to do this, and when it is/is not better.)


Tuesday, August 17, 2010 7:52 PM

okay, im sorta new to C# and have never used regular expressions before so im still confused. where would i add the MappedImage's name? the string that has the name of the mapped image is called entryName, so as in the example of the string i posted, entryName would be = to "0123456789ABCDEF". what does the ".*" do? sorry for all the questions

-StumpyINC


Tuesday, August 17, 2010 7:55 PM

im trying to not to change the code and how it looks, or else the program that uses this code wont work right. the program im trying to write will map ab image in a .TGA file and save the list of mapped images to an INI file. 

-StumpyINC


Tuesday, August 17, 2010 8:12 PM

okay, im sorta new to C# and have never used regular expressions before so im still confused. where would i add the MappedImage's name? the string that has the name of the mapped image is called entryName, so as in the example of the string i posted, entryName would be = to "0123456789ABCDEF". what does the ".*" do? sorry for all the questions

-StumpyINC

Hmm, although regex is efficient, is also hard to read, hard to use and very easy to do wrong, if you are new to this gig, I recommend that you read a few book on regular expresion before trying to use it a lot.

If you are not confortable with regex (a lot of people are) you can use regular string manipulation functions, you can achieve the same although you will have to write more code.

Regards


Tuesday, August 17, 2010 8:16 PM

yes! thanks! that did it :D

-StumpyINC


Tuesday, August 17, 2010 8:25 PM

okay, im sorta new to C# and have never used regular expressions before so im still confused. where would i add the MappedImage's name? the string that has the name of the mapped image is called entryName, so as in the example of the string i posted, entryName would be = to "0123456789ABCDEF". what does the ".*" do? sorry for all the questions

-StumpyINC

Hmm, although regex is efficient, is also hard to read, hard to use and very easy to do wrong, if you are new to this gig, I recommend that you read a few book on regular expresion before trying to use it a lot.

If you are not confortable with regex (a lot of people are) you can use regular string manipulation functions, you can achieve the same although you will have to write more code.

Regards

This isn't a particularly hard Regex to do, since you only need to use two metacharacters, everything else is literals, and the two metacharacters are two of the most common.  What it would take to do that without regexes would probably be more confusing.  Even assuming you needed to look up the two regex characters used it would be faster than the time it would take to understand something like what IAmLouis just wrote.  On top of that, the regex will be substantially more efficent than that code, for a number of reasons.  First, it can be done with one pass, whereas that code requires two extra passes each time it finds a match of the start string.  It also is copying the string after each match which the regex won't (it will only do one copy at the very end) which will make it a lot faster.


Tuesday, August 17, 2010 8:31 PM

on IamLouis post, whatever make the OP happy is fine by me, it can be done better than that and regex can be more complex than that

So is matter of perspective

I need to go now, I'll post more if you like tomorrow

Rgards


Tuesday, August 17, 2010 8:38 PM

While RegEx is the way to go, he was uncomfortable using regex, so I suggested a way that was easier to understand.


Tuesday, August 17, 2010 8:41 PM

and i liked that, thank you (:


Wednesday, August 18, 2010 1:16 AM

This isn't a particularly hard Regex to do

Looks like it was sufficiently hard that you didn't get it right.

.* will get all the characters it can. That means that it will go until the last 'End' of the string.

If you want to have a so-called lazy match, that is, take the least possible number of characters, you need to add a question mark: .*?

Once that first thing is done, you'll see that both MappedImage lines match the condition, because the regex didn't tell to look for an end of line after the entryName.