简体   繁体   中英

How do you split a string if you know the start and stop point but not the middle in asp.net C#

I'm attempting to remove a section of a string that can change at anytime. This makes it difficult to split the string to remove said section. There is a point before and after the section to remove that is constant.

Here is the only way I can think of to accurately show an example (please ignore the fact that it is html):

string text = 
"<ul>
<li>keep this text</li>
<li class=Known  unknown text  </li>
<li>keep this text</li>
</ul>";  

string [] splitPerams = {"","<li class=Known (im guessing a regex here) 
</li>"}

string [] results = 
text.Split(splitPerams,System.StringSplitOptions.RemoveEmptyEntries);

output:

"<ul>
<li>keep this text</li>

<li>keep this text</li>
</ul>";

I know there are a lot of similar questions regarding this subject, but all of them are in different languages, and I cant figure out how to implement the logic in c#.

EDIT:I guess I'm not allowed to delete this so I will do my best to just reword it completely for better understanding.

It sounds like what you need isn't a split so much as the front end of a string so using substring should do it. Since you know what characters the removing section starts with, using indexOf will be useful.

var str = "Hi My name is Mr. ???? from the usa.";
var newStr = str.Substring(0, str.IndexOf("Mr."));

Play around with that to get the exact length you want.

References:

https://docs.microsoft.com/en-us/dotnet/api/system.string.substring?view=netframework-4.7.2

https://docs.microsoft.com/en-us/dotnet/api/system.string.indexof?view=netframework-4.7.2

If by some chance this helps someone, here is the way to remove/pull an unknown section of text from a string when you know the part before and after the unknown section.

string originalText = "Hi my name is Mr. Smith from the USA.";

string[] topPull = { "", "Mr." };
string[] bottomPull = { "from", "" };
string result;

string[] topPage = originalText.Split(topPull,StringSplitOptions.RemoveEmptyEntries);
string[] bottomPage = 
originalText.Split(bottomPull,StringSplitOptions.RemoveEmptyEntries);


//topPage[0] gives all text above topPull, but not topPull it's self
//bottomPull[1] gives all text below bottomPull, but not bottomPull it's self
//now that we have grabbed all the text above and below our known sections we need to 
//add in the known sections themselves, ie topPull and bottomPull

result = topPage[0] + topPull[1] + " " + bottomPull[0] + bottomPage[1];

output: "Hi my name is Mr. from the USA."


If you want to keep only the middle text you can do it this way

string originalText = "Hi my name is Mr. Smith from the USA.";

string[] topPull = { "", "Mr." };
string[] bottomPull = { "from", "" };
string result;

string[] topPage = originalText.Split(topPull,StringSplitOptions.RemoveEmptyEntries);
string[] bottomPage = 
topPage[1].Split(bottomPull,StringSplitOptions.RemoveEmptyEntries);
result = bottomPage[0];

output: " Smith ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM