1

I'm trying to use IndexOf as given in the example here:

https://msdn.microsoft.com/en-us/library/k8b1470s(v=vs.110).aspx

If you scroll down it shows the example:

  string s1 = "ani\u00ADmal";
  string s2 = "animal";

  // Find the index of the soft hyphen.
  Console.WriteLine(s1.IndexOf("\u00AD"));
  Console.WriteLine(s2.IndexOf("\u00AD"));

I am trying to execute that from Main(string[] args) and I get the compilation error, IndexOf is culture aware and missing a string comparison argument." I have imported using System; and using System.IO; and I'm using Visual Studio on a Mac. Am I missing something?

EDIT: Alright, the explanation of the problem can be found under "remarks":

This method performs a word (case-sensitive and culture-sensitive) search using the current culture. The search begins at the first character position of this instance and continues until the last character position.

Character sets include ignorable characters, which are characters that are not considered when performing a linguistic or culture-sensitive comparison. In a culture-sensitive search, if value contains an ignorable character, the result is equivalent to searching with that character removed. If value consists only of one or more ignorable characters, theIndexOf(String) method always returns 0 (zero) to indicate that the match is found at the beginning of the current instance. In the following example, the IndexOf(String) method is used to find three substrings (a soft hyphen (U+00AD), a soft hyphen followed by "n", and a soft hyphen followed by "m") in two strings. Only one of the strings contains a soft hyphen. If the example is run on the .NET Framework 4 or later, in each case, because the soft hyphen is an ignorable character, the result is the same as if the soft hyphen had not been included in value. When searching for a soft hyphen only, the method returns 0 (zero) to indicate that it has found a match at the beginning of the string.

Douglas
  • 53,759
  • 13
  • 140
  • 188
Lily Potter
  • 203
  • 2
  • 8
  • 2
    https://stackoverflow.com/questions/10941375/could-string-comparisons-really-differ-based-on-culture-when-the-string-is-guara This can help – TGKL May 29 '17 at 21:05
  • 1
    That's not a C# compilation error, in fact, according to Google you're the only person in the world having that error. Do you have StyleCop or ReSharper running and/or are you interpreting or translating that error message? – CodeCaster May 29 '17 at 21:40
  • I'm not familiar with StyleCop or ReSharper but I just tried in a Windows machine (".Net framework" project) and it works... hmm edit: I think I need this https://stackoverflow.com/questions/28280082/can-i-program-c-sharp-on-a-mac – Lily Potter May 29 '17 at 21:54
  • Just for reference, I was using an "other->.Net" c# project. Microsoft announced it had signed a definitive agreement to acquire Xamarin on February 24, 2016, and I don't think I will be able to write a Windows c# project on my machine (which is what I was trying to do) at all. – Lily Potter May 29 '17 at 22:15

1 Answers1

4

I assume that the implementation of the .NET Framework you're using does not have that overload. Try using the IndexOf(string, StringComparison) overload that it's suggesting by adding the string comparison:

string s1 = "ani\u00ADmal";
string s2 = "animal";

// Find the index of the soft hyphen.
Console.WriteLine(s1.IndexOf("\u00AD", StringComparison.CurrentCulture));
Console.WriteLine(s2.IndexOf("\u00AD", StringComparison.CurrentCulture));
Douglas
  • 53,759
  • 13
  • 140
  • 188