embryo
embryo

Reputation: 157

String contains (second instance of substring)

I'm trying to manipulate a string using C#/razor.

What I want to do is show only the part of the string to the right of the second appearance of the word "Summary ". So, for example, if the string is:

Summary Symptoms Read More Summary A tarsal coalition is a bridge of bone that forms in the foot in late adolescence. As the tarsal coalition progresses from a fibrous...

I want to display it as:

A tarsal coalition is a bridge of bone that forms in the foot in late adolescence. As the tarsal coalition progresses from a fibrous...

So, I guess what I need to know is how do you use "contains" to find the second instance of a substring within a string?

OK...so Soner has put me in the right direction, but when I try this, it errors:

@{

string s = @Html.Raw(item.ShortBody);

int firstindex = s.IndexOf("Summary ");
s = s.Remove(0, 8);
int secondindex = s.IndexOf("Summary ");

var strbody = s.Substring(secondindex + 8);
}

@strbody

How do I output the manipulated string to the screen on my view? the @s isn't working..

Upvotes: 1

Views: 727

Answers (2)

Soner Gönül
Soner Gönül

Reputation: 98750

If there is no Summary word after your second Summary, you can use String.Split method like;

string s = "Summary Symptoms Read More Summary A tarsal coalition is a bridge of bone that forms in the foot in late adolescence. As the tarsal coalition progresses from a fibrous...";
var array = s.Split(new string[] {"Summary "}, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine(array[1]);

Output will be;

A tarsal coalition is a bridge of bone that forms in the foot in late adolescenc
e. As the tarsal coalition progresses from a fibrous...

Here a demonstration.

If you have a Summary word after your second Summary, you can use String.IndexOf and String.SubString methods like;

string s = "Summary Symptoms Read More Summary A tarsal coalition is a bridge of bone that forms in the foot in late adolescence. As the tarsal coalition progresses from a fibrous...";
int firstindex = s.IndexOf("Summary ");
s = s.Remove(firstindex, 8);
int secondindex = s.IndexOf("Summary ");
Console.WriteLine(s.Substring(secondindex + 8));

Output will be;

A tarsal coalition is a bridge of bone that forms in the foot in late adolescenc
e. As the tarsal coalition progresses from a fibrous...

Here a demonstration.

Upvotes: 1

tvanfosson
tvanfosson

Reputation: 532495

If you know that the string always starts with the first summary, you can use the IndexOf signature that includes an offset before starting the search.

 var second = str.IndexOf("Summary", 7);
 var description = str.Substring(second + 8).TrimStart();

Alternatively, you could find the first, then use it's position to find the correct offset

 var second = str.IndexOf("Summary", str.IndexOf("Summary") + 7);
 var description = str.Substring(second + 8).TrimStart();

Obviously, these both rely on the fact that the string does contain at least two instances of the word Summary. If that's not the case then you'll need to check that the results of IndexOf is equal to or greater than zero before attempting to find the substring.

Another alternative, if you know that there are at most 2 occurrences of the word is to use LastIndexOf instead of IndexOf, then take the substring after that.

 var second = str.LastIndexOf("Summary");
 var description = str.Substring(second + 8).TrimStart();

Upvotes: 2

Related Questions