locked
Lowering case of TCHAR for case insensitive strstr() RRS feed

  • Question

  • I need to find some string in TCHAR array but i need to do it in case-insensitive way. Is there a function to achieve that? Also, i cant determine the size of * TCHAR[] correctly.

    TCHAR *extens[] = { L".*" };int extCount = sizeof(extens) / sizeof(TCHAR);

    How can i achieve it right?

    Monday, July 27, 2015 8:20 AM

Answers

  • I need to find some string in TCHAR array but i need to do it in case-insensitive way. Is there a function to achieve that? Also, i cant determine the size of * TCHAR[] correctly.

    TCHAR *extens[] = { L".*" };int extCount = sizeof(extens) / sizeof(TCHAR);

    How can i achieve it right?

    The extense array is an array of pointers.  To deterine it's size at compile time use:

    int extCount = sizeof(extens) / sizeof(LPTSTR);
    

    If you  want have code that works in both ANSI and Unicode you should define the extens array like:

    TCHAR *extens[] = { _T(".*") };
    

    Now that you have the correct size of the array you can loop through it as follows:

    int returnCode;
    
    for(int i = 0; i < extCount; i++)
       returnCode = _tcsicmp( extens[i], _T("String To Compare") );
    
    

    This will perform a case insensitive comparison of your search string with the strings pointed to in the extens array.

    • Marked as answer by freelsd Monday, July 27, 2015 11:08 AM
    Monday, July 27, 2015 10:38 AM

All replies

  • The easiest way would be converting the string before searching to lowercase or uppercase, than you can perform the serach.

    Best regards

    Bordon

    Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.

    Monday, July 27, 2015 9:25 AM
  • How can i convert to lowercase a TCHAR array?
    Monday, July 27, 2015 9:26 AM
  • For TCHAR you could use the _tcslwr* or _tcsupr* functions. There are different versions of this particular functions.

    Best regards

    Bordon

    Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.


    • Edited by Bordon Monday, July 27, 2015 10:40 AM
    Monday, July 27, 2015 10:37 AM
  • I need to find some string in TCHAR array but i need to do it in case-insensitive way. Is there a function to achieve that? Also, i cant determine the size of * TCHAR[] correctly.

    TCHAR *extens[] = { L".*" };int extCount = sizeof(extens) / sizeof(TCHAR);

    How can i achieve it right?

    The extense array is an array of pointers.  To deterine it's size at compile time use:

    int extCount = sizeof(extens) / sizeof(LPTSTR);
    

    If you  want have code that works in both ANSI and Unicode you should define the extens array like:

    TCHAR *extens[] = { _T(".*") };
    

    Now that you have the correct size of the array you can loop through it as follows:

    int returnCode;
    
    for(int i = 0; i < extCount; i++)
       returnCode = _tcsicmp( extens[i], _T("String To Compare") );
    
    

    This will perform a case insensitive comparison of your search string with the strings pointed to in the extens array.

    • Marked as answer by freelsd Monday, July 27, 2015 11:08 AM
    Monday, July 27, 2015 10:38 AM
  • Much appreciated for that fast reponse, thanks for all of you, that posts really helped!
    Monday, July 27, 2015 11:08 AM
  • I need to find some string in TCHAR array but i need to do it in case-insensitive way. Is there a function to achieve that? Also, i cant determine the size of * TCHAR[] correctly.

    TCHAR *extens[] = { L".*" };int extCount = sizeof(extens) / sizeof(TCHAR);

    How can i achieve it right?

    extens is an array of pointers that contains exactly one element.  The size of the array and the size of its element are both irrelevant.  You have two choices:

         You can remove the * from the type.  This will make extens an array TCHAR and extCount will contain the total number of elements in the array including the terminating nul character.  If you don't want the last character, subtract one.

         Change the initialization of extCount to
              int extCount = _tcslen(extens[0]);
    which will not include the terminating nul character.

    Monday, July 27, 2015 5:23 PM