Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the end of a substring match in .NET

I am trying to find the index of a substring in a string that matches another string under a specific culture (provided from a System.CultureInfo).

For example the string "ass" matches the substring "aß" in "straße" under a German culture.

I can find the index of the start of the match using

culture.CompareInfo.IndexOf(value, substring);

but without resorting to brute force, is there an easy way of identifying that 2 characters were matched, and not 3?

like image 717
Oliver Hallam Avatar asked Oct 08 '08 11:10

Oliver Hallam


People also ask

How do you find the substring at the end of a string?

You call the Substring(Int32) method to extract a substring from a string that begins at a specified character position and ends at the end of the string. The starting character position is a zero-based; in other words, the first character in the string is at index 0, not index 1.

How to find the position of a substring in a string in C#?

In C#, IndexOf() method is a string method. This method is used to find the zero-based index of the first occurrence of a specified character or string within the current instance of the string. The method returns -1 if the character or string is not found.

How do you match a string in C#?

In C#, Equals(String, String) is a String method. It is used to determine whether two String objects have the same value or not. Basically, it checks for equality. If both strings have the same value, it returns true otherwise returns false.


1 Answers

If you use a capture group, you can capture the exact match that was found, and from that you can determine how many characters were matched.

I'm a bit timestressed right now to give an example, so I hope you can figure it out from my description.

Perhaps I'll ammend my answer later.

Dave

like image 56
Dave Van den Eynde Avatar answered Oct 14 '22 07:10

Dave Van den Eynde