IndexOf substring in C #

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

this is what i am doing so far:

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

With this, I delete at least _02 , however, if I try to do the same for ^ , then I always return texthere , even when I use name.IndexOf("^")

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

I even tried name.Replace("^" , and then used the substring method that I used earlier. But again, the result remains the same.

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

What am I doing wrong?

thanks

+8
substring c #
source share
13 answers

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

  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); 
+8
source share

Use String.Split method:

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

Or you can use RegExp to achieve basically the same thing.

+3
source share

Well, if you use the same code that you posted, it does the right thing, you start to extract characters from char 0 and stop when it finds "^", so you get "texthere".

If you want only a value, use this:

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

First, he will delete everything that is after "_", and everything that is before "^".

+3
source share

The substring takes position and length, so you need to determine where your caret position is, and where the underline should 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); }; 
+3
source share

With regex

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

use regex.

 Regex regex = new Regex(@"\^(.*)_"); Match match = regex.Match(name); if(match.Success) { name= match.Groups[1].Value } 
+3
source share

An alternative to what has already been suggested is to use a regular expression:

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

Try this and let me know how this happens.

  string inputtext = "texthere^D123456_02"; string pattern = @".+\^([AZ]+[0-9]+)\_[0-9]+"; string result = Regex.Match(inputtext, pattern).Groups[1].Value; 
+3
source share

An easier way is to use Split.

 string s = "texthere^D123456_02"; string[] a = s.Split('^', '_'); if (a.Length == 3) // correct { } 
+2
source share

The simplest solution is to split the string first and then use the original solution for the second part.

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

Subsequently, you can use the substring as before.

+2
source share
 string s = "texthere^D123456_02"; string result= s.Substring(s.IndexOf("^") + 1);//Remove all before result = result.Remove(result.IndexOf("_"));//Remove all after 
+2
source share
 String name = "texthere^D123456_02" print name.split('_', '^')[1] 

This splits your string in all occurrences of _ and ^ and returns a list of strings after splitting. Since the row you need, D123456 will be in the first index (i.e., in the 2nd position), I printed this.

+2
source share

If you just want "d123456" to be just with just String.Split() , nothing is needed. Just determine the index you want afterwards. For this reason, there are overloads on Split() .

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

Hope this helps.

+2
source share

All Articles