Results 1 to 3 of 3

Thread: Replacing String - Case not sensitive

  1. #1

    Thread Starter
    Hyperactive Member Stiletto's Avatar
    Join Date
    Aug 2002
    Location
    Jerusalem, Israel
    Posts
    287

    Replacing String - Case not sensitive

    Hey,
    My code replaces a specific word chosen by the user by another word, also chosen by user. Lets say the words are And & &, and each time there "And", it will be replaced with &.
    The problem is that if the word is "and" (Lower Case), it wouldn't recognize it.
    How can I overcome this problem? What about the Compare Method?
    Code:
        sWords = Split(sData, vbNewLine)
        For i = LBound(sWords) To UBound(sWords) - 1
            'Retrive text to be replaced
            sText1 = Left(sWords(i), InStr(1, sWords(i), "=") - 1)
            If InStr(1, sFilename, sText1) Then
                'Replace sText1 with sText2
                sText2 = Right(sWords(i), (Len(sWords(i)) - InStr(1, sWords(i), "=")))
                sFilename = Replace(sFilename, sText1, sText2)
            End If
        Next i
        ConvertWords = sFilename
    Edit - totally forgot about LCase()...
    Last edited by Stiletto; Jul 19th, 2008 at 10:41 AM.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Replacing String - Case not sensitive

    you can also use text compare
    If InStr(1, sFilename, sText1, vbTextCompare)

    you could also use replace function to do this
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Replacing String - Case not sensitive

    Or you can convert the data that the user has entered, ie. "And", to lowercase at first using LCase(), and during the searching also, each word retieve from the data, convert that word also to lowercase. Then compare it. So that, no problem will occurs on replacing a particular word...

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width