B
bob
Is there any case-insensitive version of the String contains method?
Not that I'm aware of. An easy way to do the deed is to use Pattern;Is there any case-insensitive version of the String contains method?
Arved said:Not that I'm aware of. An easy way to do the deed is to use Pattern;
something like
boolean isContained =
Pattern.compile("isThisStringContained",
Pattern.CASE_INSENSITIVE)
.matcher("stringThatMayContainOther").find();
does the trick.
A possibly preferable alternative to doing case-insensitive string
operations is simply to uppercase (or lowercase) both Strings before
doing the operation. .toLowerCase() and .toUpperCase() are String
methods that are available for this purpose. If you plan to do this a
lot you can write up a small utility method.
That's a good point, but it's no mystery that uppercasing/lowercasingBeware of uppercasing and lowercasing - the results can be surprising.
String whatThe = "ß".toUpperCase().toLowerCase();
What should be the value of '"ß".equalsIgnoreCase("ss")'?
What should be the value of '"ß".toUpperCase().toLowerCase().equals("ß")'?
That's a good point, but it's no mystery that uppercasing/lowercasing
outside the standard 26-letter Latin alphabet has the odd pitfall here
and there, like Eszett rules.
So my second suggestion applies in particular to Strings that contain
Latin characters. For anything else you'd best be aware of the rules and
the nature of your text.
Lew said:On Thursday, September 8, 2011 7:33:34 AM UTC-7, Arved Sandstrom wrote:
Your post had trouble, being in "UTC-7" and dealing with 8-bit characters.
On Thursday, September 8, 2011 7:33:34 AM UTC-7, Arved Sandstrom wrote:
Your post had trouble, being in "UTC-7" and dealing with 8-bit characters.
Which means, for all practical purposes, always be aware of the rules and nature of your text. Even here in the U.S. of A., we use lots of letters that don't fall into your (apparent) definition of "Latin" characters, for example, Latin-American names and loan words. Never mind that in almost any context that a programmer cares about, you have to deal with locales. Advising a programmer to deal with just the restriction to ASCII is a ludicrous suggestion. You pretty much always have to be aware, at a minimum, of eight-bit characters, and really all of UTF-8. To do otherwise is very irresponsible.
On a go-forward basis starting with a clean slate, absolutely, I agree.Also, by definition "ß" is a Latin character, in the sense that it's in the Latin-1 (ISO8859-1) character set.
If you write for the 128
Then you deserve your horrible fate.
Andreas said:What does (your?) timezone UTC-7 have to do with Arved's ISO-8859-1 encoding?
Is there any case-insensitive version of the String contains method?
I'm not exactly advising any programmer to deal with just ASCII; what I
am saying here is that if you know that your text is ASCII text (*way*
more common than you make out) that lowercasing and uppercasing in this
particular situation is a potential approach. By ASCII text I still mean
Unicode; simply the ASCII subset thereof.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.