N2J
N2J

Reputation: 175

how to do substring in reverse mode in c#

My string is "csm15+abc-indiaurban@v2". I want only "indiaurban" from my string. what I am doing right now is :

var lastindexofplusminus = input.LastIndexOfAny(new char[]{'+','-'});

var lastindexofattherate = input.LastIndexOf('@');

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

but getting error "Index and length must refer to a location within the string."

Thanks in Advance.

Upvotes: 5

Views: 16466

Answers (6)

AlanC
AlanC

Reputation: 584

2 extension methods:

    public static string SubstrReverse(this string str, int reverseIndex, int length)
    {
        var startPos = str.Length -reverseIndex;
        return string.Join("", 
            str.Reverse()
            .Skip(startPos)
            .Take(length).Reverse());
    }

    public static string SubstrReverse(this string str, int reverseIndex)
    {
        var startPos = str.Length - reverseIndex;
        return string.Join("", 
            str.Reverse()
            .Skip(startPos)
            .Reverse());
    }

Upvotes: 0

Ian
Ian

Reputation: 30813

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.

Upvotes: 9

Daniel Mendes
Daniel Mendes

Reputation: 31

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!!

Upvotes: 2

w.b
w.b

Reputation: 11228

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

Upvotes: 1

Sam Ford
Sam Ford

Reputation: 21

I don't know what is identify your break point but here is sample which is working

you can learn more about this at String.Substring Method (Int32, Int32)

String s = "csm15+abc-indiaurban@v2";
        Char charRange = '-';
        Char charEnd = '@';
        int startIndex = s.IndexOf(charRange);
        int endIndex = s.LastIndexOf(charEnd);
        int length = endIndex - startIndex - 1;
        Label1.Text = s.Substring(startIndex+1, length);

Upvotes: 0

JC Borlagdan
JC Borlagdan

Reputation: 3618

Assuming that your string is always in that format

string str = "csm15+abc-indiaurban@v2";
string subSTr = str.Substring(10).Substring(0,10);

Upvotes: -3

Related Questions