Re: case sensitivity in strings...
From: Grant Wagner (gwagner_at_agricoreunited.com)
Date: 11/29/04
- Previous message: Stefan Schulz: "Re: strange problem"
- In reply to: Frances Del Rio: "Re: case sensitivity in strings..."
- Next in thread: Hal Rosser: "Re: case sensitivity in strings..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 29 Nov 2004 20:18:54 GMT
Frances Del Rio wrote:
> Mike B wrote:
> > Frances Del Rio <fdr58@yahoo.com> wrote:
> >
> >>this seems like somthing so simple, but I can't figure it out... I
> >>have a little app that consists basically of a string being displayed
> >>and app tests whether or not user typed in a substring of that
> >>string... (complete code at www.francesdelrio.com/hamlet.html)
> >>
> >>but: I would like this to be non-case-sensitive.. the way it is now
> >>if user types 'hamlet' instead of 'Hamlet' it returns that that
> >>substring is not found in the string... how do I say:
> >>if(Str.indexOf(useript) REGARDLESS OF CASE...... like you can do
> >>with CompareTo() method... I don't see the possibility of doing this
> >>w/indexOf in API.. thanks for any help.. Frances
> >
> >
> > Translate both strings to either upper- or lower-case before you search in
> > the string.
>
> of course.. thank you... I turned them both to lowercase, but if user
> types in "Hamlet" I would like app to return that same Upper-lower
> scheme, well, this might be a small detail... I'm too picky... ;)
> thank you for your help... (now I have to do same for JavaScript for
> the form validation.. I tried same approach in JS but so far it's not
> working..) thank you again... Frances
Only convert them to lowercase for the comparison, retain the original strings
to get the original value out:
String search = "Hamlet";
String source = "A small town is a hamlet";
if (source.toLowerCase().indexOf(search.toLowerCase()) != -1) {
System.out.println("Found '" + search + "' in '" + source + "'.");
} else {
System.out.println("Did not find '" + search + "' in '" + source + "'.");
}
or if you need the lowercase versions for other tests:
String search = "Hamlet";
String source = "A small town is a hamlet";
String searchLc = search.toLowerCase();
String sourceLc = source.toLowerCase();
if (sourceLc.indexOf(searchLc) != -1) { ...
-- Grant Wagner <gwagner@agricoreunited.com> comp.lang.javascript FAQ - http://jibbering.com/faq
- Previous message: Stefan Schulz: "Re: strange problem"
- In reply to: Frances Del Rio: "Re: case sensitivity in strings..."
- Next in thread: Hal Rosser: "Re: case sensitivity in strings..."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|