-1

I am trying to find the position of a substring in another string with IgnoreCase.
example:

String str ="aabbssddaa", str2="ab";
str.indexOf(str2);

I want to find the position of str2 that is not case sensitive. means str2= any of them "ab","Ab","aB","AB"
And I want to traverse the string only once.

Eric Brown
  • 13,308
  • 7
  • 28
  • 67
gifpif
  • 4,087
  • 3
  • 28
  • 44

1 Answers1

5

The easiest way is to just use toLowerCase() or toUpperCase() on both strings.

chrylis -cautiouslyoptimistic-
  • 67,584
  • 19
  • 106
  • 140
  • thanx good logic.. :) – gifpif Oct 26 '13 at 12:13
  • Except that does not always work. http://www.joelonsoftware.com/articles/Unicode.html – Kevin Panko Oct 26 '13 at 17:45
  • @KevinPanko Any explanation of why? I'm not bit-twiddling ASCII, I'm using Java's `String` manipulations, which are explicitly defined in terms of Unicode characters. In particular, how would `toUpperCase()` and `CASE_INSENSITIVE_ORDER` differ from each other? – chrylis -cautiouslyoptimistic- Oct 27 '13 at 00:23
  • Check out the Turkish rules for the letter I. Uppercase `i` and the result is `İ` which means if you do `foo.toUpperCase().equals("EXIT")` it will be true in US English and false in Turkey when `foo` is the string `"exit"`. – Kevin Panko Oct 27 '13 at 21:20
  • @KevinPanko The bug is using the wrong charset. And you still haven't said how that would differ from `CASE_INSENSITIVE_ORDER`, which is the next-best option. – chrylis -cautiouslyoptimistic- Oct 28 '13 at 03:44