Re: case sensitivity in strings...

From: Grant Wagner (gwagner_at_agricoreunited.com)
Date: 11/29/04

  • Next message: Krzysiek K.: "Library program"
    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
    

  • Next message: Krzysiek K.: "Library program"

    Relevant Pages

    • CodeDom Security / Permissions issues
      ... ASP.net web site and a Windows Service. ... I realize that CodeDom has to create temporary files even when doing ... Boolean useAsync, String msgPath, Boolean bFromProxy)at ... options, String source) at ...
      (microsoft.public.dotnet.framework.aspnet)
    • CodeDom Security/Permission Issues
      ... The same code is used by both an ASP.net web site and a ... I realize that CodeDom has to create temporary files even when doing ... access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, ... options, String source) ...
      (microsoft.public.dotnet.framework)
    • System.IO.__Error.WinIOError in XmlSerializer constructor
      ... access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, ... options, String source) ...
      (microsoft.public.dotnet.xml)
    • Re: how can i optimize the given below code
      ... String source = this.source; ... Note that the assignment to the local is important for the logic, which is correct independently of the memory model, i.e. the method will always return the correct result. ... Do you think that filling an array char by char and then creating a string is more optimal than just creating a substring of the already filled string? ...
      (comp.lang.java.programmer)
    • RE: Bad Data using TripleDES Encryption
      ... Hi Troy, ... > public static string EncryptString(string toEncrypt) ... > public static string DecryptString(string toDecrypt) ... >public string EncString(string Source) ...
      (microsoft.public.dotnet.framework.aspnet.security)