Get second position of last character from string

I have a dynamically generated string like - part1.abc.part2.abc.part3.abc

On this line, I want to know the position of the second, to continue "." so that I can split the string as part1.abc.part2.abc and part3.abc

let me know if there is any direct method to get this?

+6
source share
10 answers
string str = "part1.abc.part2.abc.part3.abc"; int ix1 = str.LastIndexOf('.'); int ix2 = ix1 > 0 ? str.LastIndexOf('.', ix1 - 1) : -1; 

There are always fans of Regexes (and jQuery), so I will give a Regex solution (for a jQuery solution you have to wait :-)):

 var match = Regex.Match(str, @"\.[^\.]*\.", RegexOptions.RightToLeft); int ix = match.Success ? match.Index : -1; 

(note that I am a hater for regular expressions, I give it to you so that you have enough rope to hang yourself if you so wish).

Keep in mind that I'm using the RegexOptions.RightToLeft parameter RegexOptions.RightToLeft that the RegexOptions.RightToLeft with the last character.

+14
source

You can use the String.LastIndexOf('.') Method to get the position of the last full cycle / period, and then use this position per second call LastIndexOf('.') To get the last but one, for example:

 string aString = "part1.abc.part2.abc.part3.abc"; int lastPos = aString.LastIndexOf('.'); int lastPosButOne = aString.LastIndexOf('.', lastPos - 1); 

But I would recommend using String.Split('.') , Which will give you an array of strings, then you can take the last but one, for example

 string aString = "part1.abc.part2.abc.part3.abc"; string[] parts = aString.Split('.'); string lastPartButOne = parts[parts.Length - 1]; 
+6
source

Here is another solution:

  string aString = "part1.abc.part2.abc.part3.abc"; // Getting string until last dot var untilLastDot = aString.Substring(0, aString.LastIndexOf(".")); // Now we have string until last dot and last dot here will be last but one // and getting text from last but one dot to end string lastWordButOne = aString.Substring(untilLastDot.LastIndexOf(".") + 1); // Result: part3.abc 

hope thanks, thanks!

+4
source

As far as I know, there is no ready-made solution. One approach is to find the last one. With the string LastIndexOf, and then again look for the last point, this time using an overload that allows you to specify startindex and count, using the index of the first call as a parameter for counting.

+2
source

You can use the String.Split() method, which returns an array of separated elements. Then you can combine the first 2 and leave the last.

+1
source

Try the LastIndexOf method of the string class.

+1
source

How about "part1.abc.part2.abc.part3.abc".Split('.') , In this case you get an array of all the substrings

Hope this helps.

+1
source

LastIndexOf should do what you want. Just do it twice.

+1
source

Based on @bitbonk answer. I used the code below, which is a replica of the RAT() function of VFP .

  public static int RightIndexAt(this string expressionToSearch, char charToSearch, int occurence) { //Validate parameter if (occurence < 1) return -1; int index = -1; int numfound = 0; for (int count = expressionToSearch.Length - 1; count >= 0; count--) { if (expressionToSearch[count].Equals(charToSearch)) { index = count; numfound++; } if (numfound.Equals(occurence)) break; } return numfound < occurence ? -1 : index; } 
+1
source

This would be the solution with the best performance (perhaps it will not be much faster and will have less memory than this if you do not want to follow an unsafe route):

 public static int LastIndexOf(this string str, char charToSearch, int repeatCound) { int index = -1; for(int i = str.Length - 1; i >= 0, numfound < repeatCound) { if(str[i] == charToSearch) { index = i; numfound++; } } return index; } 
0
source

Source: https://habr.com/ru/post/899204/


All Articles