Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is string IndexOf() acting case-INsensitive?

I'm really stumped by this one. I'm learning LINQ and used Microsoft Visual C# Express Edition to connect to a SQL Server database containing information about my books. I created the LINQ to SQL classes as needed. That stuff obviously works. It all works except I cannot figure out for the life of me why, if I search for "SR" (uppercase), it finds two records, "SR-71 Revealed, The Inside Story", as expected, but it also finds "Faded Sun: Kesrith" where the "sr" is lowercase. I'm using the IndexOf() method of the string class, which is supposed to perform a case-SENSITIVE comparison, right? The output displays the second book title as shown above, with the "sr" in lowercase. Here's the pertinent part of the code:

// normal using directives here
namespace QueryBooksDB {
    class Program {
        static void Main() {
            var urgh = new BooksDataContext();

            Console.WriteLine("Enter a string to search for:");
            string str = Console.ReadLine();

            var list = from book in urgh.Books
                        where book.Title.IndexOf(str) > -1
                        orderby book.Title
                        select new { ID = book.BookId, book.Title, book.Location };

            foreach ( var b in list ) {
                Console.WriteLine(b.Title);
            }
        }
    }
}
like image 626
user1013210 Avatar asked Oct 25 '11 17:10

user1013210


People also ask

Is IndexOf case insensitive?

The indexOf() method returns the index number where the target string is first found or -1 if the target is not found. Like equals(), the indexOf() method is case-sensitive, so uppercase and lowercase chars are considered to be different.

Is array IndexOf case-sensitive?

The indexOf() method returns -1 if the value is not found. The indexOf() method is case sensitive.

Is string contain case-sensitive?

CONTAINSSTRING is not case-sensitive, but it is accent-sensitive. CONTAINSSTRINGEXACT: Returns TRUE if one text string contains another text string.

Is IndexOf case-sensitive C#?

IndexOf Method is by default case sensitive. But we can make it case insensitive if we use one of the overloads: IndexOf(String, StringComparison).


2 Answers

At the final step your query is translated into sql. In SQL server string alike fields (varchar, nvarchar) are case insensetive. So select * from tbl where col like '%foo%' will retrieve if the value is Foo or FOo

like image 174
Oybek Avatar answered Sep 26 '22 18:09

Oybek


I thought it was case-sensitive by default, but you can always use the StringComparison overload to specify case sensitivity:

test.IndexOf("foo", StringComparison.Ordinal);

StringComparison enumeration:

  1. CurrentCulture
  2. CurrentCultureIgnoreCase
  3. InvariantCulture
  4. InvariantCultureIgnoreCase
  5. Ordinal
  6. OrdinalIgnoreCase
like image 33
James Johnson Avatar answered Sep 24 '22 18:09

James Johnson