Desutoroiya
Desutoroiya

Reputation: 544

Substring IndexOf in c#

I have a string that looks like this: "texthere^D123456_02". But I want my result to be D123456.

this is what i do so far:

if (name.Contains("_"))
{
name = name.Substring(0, name.LastIndexOf('_'));
}

With this I remove at least the _02, however if I try the same way for ^ then I always get back texthere, even when I use name.IndexOf("^")

I also tried only to check for ^, to get at least the result:D123456_02 but still the same result.

I even tried to name.Replace("^" and then use the substring way I used before. But again the result stays the same.

texthere is not always the same length, so .Remove() is out of the question.

What am I doing wrong?

Thanks

Upvotes: 11

Views: 25542

Answers (13)

DL Narasimhan
DL Narasimhan

Reputation: 733

Try this and let me know how it goes

  string inputtext = "texthere^D123456_02";
  string pattern = @".+\^([A-Z]+[0-9]+)\_[0-9]+";

  string result = Regex.Match(inputtext, pattern).Groups[1].Value;

Upvotes: 3

venkat
venkat

Reputation: 259

use regex.

Regex regex = new Regex(@"\^(.*)_");
Match match = regex.Match(name);

if(match.Success)
{
    name= match.Groups[1].Value
}

Upvotes: 4

Gabe
Gabe

Reputation: 670

If you are just wanting the "d123456" it's simple with just String.Split() there is no need for anything else. Just define the index you want afterwards. There are overloads on Split() for this very reason.

//...

var source = "texthere^D123456_02";
var result = source.Split(new char[] {'^', '_'}, StringSplitOptions.RemoveEmptyEntries)[1];
Console.WriteLine(result);
//Outputs: "D123456"

Hope this helps.

Upvotes: 2

Gediminas Masaitis
Gediminas Masaitis

Reputation: 3212

An alternative to what's already been suggested is to use regex:

string result = Regex.Match("texthere^D123456_02", @"\^(.*)_").Groups[1].Value; // D123456

Upvotes: 4

vahid kargar
vahid kargar

Reputation: 794

With Regular Expression

 string s = "texthere^D123456_02";
 Regex r1 = new Regex(@"\^(.*)_");
 MatchCollection mc = r1.Matches(s);
 Console.WriteLine("Result is " + mc[0].Groups[1].Value);

Upvotes: 4

Apoorva Srivastava
Apoorva Srivastava

Reputation: 388

String name = "texthere^D123456_02"
    print name.split('_', '^')[1]

This splits your string at all occurrences of _ and ^ and returns the list of strings after the split. Since the string you need D123456 would be at the 1st index, (i.e. the 2nd position), I have printed out that.

Upvotes: 2

Pedro G. Dias
Pedro G. Dias

Reputation: 3222

Substring takes a position and a length, so you need to actually figure out where your caret position is and where the underscore is to calculate the length

var name = "texthere^D123456_02";

if(name.Contains('_'))
{
    var caretPos = name.IndexOf('^') + 1; // skip ahead
    var underscorePos = name.IndexOf('_');
    var length = underscorePos - caretPos;
    var substring = name.Substring(caretPos, length);
    Debug.Print(substring);
};  

Upvotes: 3

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

When call Substring you should not start from 0, but from the index found:

  String name = "texthere^D123456_02";

  int indexTo = name.LastIndexOf('_');

  if (indexTo < 0)
    indexTo = name.Length;

  int indexFrom = name.LastIndexOf('^', indexTo - 1);

  if (indexFrom >= 0)
    name = name.Substring(indexFrom + 1, indexTo - indexFrom - 1);

Upvotes: 13

Gusman
Gusman

Reputation: 15151

Well, if you use the same code you posted, it's doing the right thing, you start to retrieve characters from the char 0 and stop when it finds "^", so what you will get is "texthere".

If you want only the value, then use this:

name = name.Substring(0, name.LastIndexOf('_')).Substring(name.IndexOf("^") + 1);

It will first remove whatever is after the "_" and whatever is before "^".

Upvotes: 3

Ghassen
Ghassen

Reputation: 1206

string s = "texthere^D123456_02";
            string result= s.Substring(s.IndexOf("^") + 1);//Remove all before
            result = result.Remove(result.IndexOf("_"));//Remove all after

Upvotes: 7

kall2sollies
kall2sollies

Reputation: 1549

Use the String.Split method :

    var split1 = name.Split('^')[1];
    var yourText = split1.Split('_')[0];

Or you could use RegExp to achieve basically the same.

Upvotes: 5

Matthijs
Matthijs

Reputation: 3170

Your easiest solution would be to split the string first, and then use your original solution for the second part.

string name = "texthere^D123456_02";
string secondPart = name.Split('^')[1]; // This will be D123456_02

Afterwards you can use the Substring as before.

Upvotes: 4

Serve Laurijssen
Serve Laurijssen

Reputation: 9733

An easier way would be to use Split

string s = "texthere^D123456_02";

string[] a = s.Split('^', '_');

if (a.Length == 3) // correct
{
}

Upvotes: 3

Related Questions