use IndexOf() Method to modify string in <upcase> tags as uppercase

Viewed 29

I have some text, there are some tags like <upcase>something</upcase>" I want to use IndexOf() method to modify my text and change tags with real uppercase strings but unfortunately my code just modifies sentence in tags and cannot extract and append text in the middle can you advice me?

using System.Text;

string text = "We are living in a <upcase>yellow submarine</upcase>. We don't have <upcase>anything</upcase> else.";

Console.WriteLine(TextTagToUpper(text, "<upcase>", "</upcase>"));
Console.WriteLine();


string TextTagToUpper(string str, string sub1, string sub2){
    StringBuilder sb = new();
    int indexStart = str.IndexOf(sub1);
    int indexEnd = str.IndexOf(sub2);
    sb.Append(str[..(indexStart-1)]);
    while(indexEnd != -1){
        sb.Append(" ");
        sb.Append(str[(indexStart+sub1.Length)..(indexEnd)].ToUpper());
        sb.Append(" ");
        indexStart = str.IndexOf(sub1, indexEnd + 1);
        indexEnd = str.IndexOf(sub2, indexEnd + 1);
    }
    return sb.ToString();
}
0 Answers
Related