1

I get this error

    string v = "aeiou";
    foreach(int i in lokacija.Naziv) {
      if(v.indexOf(lokacija.Naziv[i], StringComparison.CurrentCultureIgnoreCase) = -1)
        s+=lokacija.Naziv[i];
    }

The error says "cannot convert from System.StringComparison to int". But I know there is an overload of the method indexOf(string) which accepts arguments of the type StringComparison. So how can I resolve this?

djomles
  • 11
  • 1
  • 1
    what version of .net? – Hogan Oct 01 '15 at 01:24
  • 3
    I'd guess that `lokacija.Naziv[i]` likely provides a `Char`. [`.IndexOf()`](https://msdn.microsoft.com/en-us/library/system.string.indexof.aspx) only accepts `StringComparison`s when the 1st argument is a `String`. – Jonathan Lonowski Oct 01 '15 at 01:25
  • @djomles, did you have the chance to try what I suggested? – Andrew Oct 02 '15 at 00:55
  • 1
    @JonathanLonowski it depends of the .NET Framework used. There **is** such a function in .NET Core 3.1: https://learn.microsoft.com/en-us/dotnet/api/system.string.indexof?view=netcore-3.1#System_String_IndexOf_System_Char_System_StringComparison_ – serge Oct 25 '20 at 14:25

3 Answers3

2

First of all, you should be using == for comparison.

Second, all IndexOf overloads whose first parameter is a char, their second parameter is an int. That's why you get that error. In order to use the overload that receives a StringComparison, make that first parameter a string, like this:

if (v.indexOf(lokacija.Naziv[i].ToString(), StringComparison.CurrentCultureIgnoreCase) == -1)

BTW, are you trying to remove vowels from a string? I recommend you try this.

Community
  • 1
  • 1
Andrew
  • 7,602
  • 2
  • 34
  • 42
  • not quite true your second point. In .NET Core we have the overload `char, StringComparision` https://learn.microsoft.com/en-us/dotnet/api/system.string.indexof?view=netcore-3.1#System_String_IndexOf_System_Char_System_StringComparison_ – serge Oct 25 '20 at 14:23
  • Well, .NET Core Initial release: June 27, 2016, my bad for not knowing the future. – Andrew Oct 26 '20 at 12:03
0

you loop seems strange... did you mean this?

foreach(string ssub in lokacija.Naziv) {
  if(v.indexOf(ssub, StringComparison.CurrentCultureIgnoreCase) = -1)
    s+=ssub;
Hogan
  • 69,564
  • 10
  • 76
  • 117
0

@Johnathan-Lonowski hit the nail on the head- you are getting this version of String.IndexOf, not this one, because you are looking for a character in a string, not an occurrence of one string in another.

Chris Shain
  • 50,833
  • 6
  • 93
  • 125