5

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.

3
  • 2
    "Index and length" - you are passing two indexes. Commented Apr 15, 2016 at 4:03
  • then how to achieve only "indiaurban". Can you please give me the right substring. Commented Apr 15, 2016 at 4:06
  • 1
    Did you understand my comment? If so, you should be able to work it out yourself. Commented Apr 15, 2016 at 4:17

6 Answers 6

9

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

In the SubstringReverse function you are missing a Reverse. It should be str.Reverse().Skip(reverseIndex - 1).Take(length).Reverse()
Index's are usually Zero-based. It would be less confusing if it is Skip(reverseIndex).
1

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

1 Comment

I like the idea of Reverse().
0

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);

Comments

0

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());
    }

Comments

-3

Assuming that your string is always in that format

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.