string – how to do substring in reverse mode in c#

string – how to do substring in reverse mode in c#

You should put the length in the second argument (instead of passing another index) of the Substring you want to grab. Given that you know the two indexes, the translation to the length is pretty straight forward:

string sub = input.Substring(lastindexofplusminus + 1, lastindexofattherate - lastindexofplusminus - 1);

Note, +1 is needed to get the char after your lastindexofplusminus.
-1 is needed to get the Substring between them minus the lastindexofattherate itself.

You can simple reverse the string, apply substring based on position and length, than reverse again.

string result = string.Join(, string.Join(, teste.Reverse()).Substring(1, 10).Reverse());

Or create a function:

public static string SubstringReverse(string str, int reverseIndex, int length) {
    return string.Join(, str.Reverse().Skip(reverseIndex - 1).Take(length));
}

View function working here!!

string – how to do substring in reverse mode in c#

You can use LINQ:

string input = csm15+abc-indiaurban@v2;

string result = String.Join(, input.Reverse()
                                     .SkipWhile(c => c != @)
                                     .Skip(1)
                                     .TakeWhile(c => c != + && c != -)
                                     .Reverse());

Console.WriteLine(result); // indiaurban

Leave a Reply

Your email address will not be published. Required fields are marked *