Thread: string manipulation

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    string manipulation

    I want to find out if the string "/m=" is in tempPrinterName anywhere. Would wcscmp(L"/m=", tempPrinterName) be the best way of doing so? If not, what would you suggest?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Since this is the C++ forum I'll assume tempPrinterName is a C++ wstring. If so, use the find member function:
    Code:
    if (tempPrinterName.find(L"/m=") != std::wstring::npos)
    {
      // found
    }
    If you need to find the position in the string then just save the return value of find as a std::wstring::size_type.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    what if tempPrinterName is also a LPTSTR? How would I find that string using C-style methods?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    LPTSTR mystring;
    std::string s = mystring;
    // Repeat as above

    Don't fall into the C trap - this is C++!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    222
    What would be the easiest way to parse the string contents after "/m=" to a different string if found?

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    .substr!
    std::string s = "/m=blablabla";
    std::string s2 = s.substr(3);

    There you have everything after the "/m=" (at least I hope).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If tempPrinterName is an LPTSTR, then you might not want to use string, you might want to use some typedef that will be either string or wstring depending on whether you're using UNICODE or not. Also, if you're using LPTSTR, then you should be using _T("/m=") instead of L"/m=". (If your application definitely uses or does not use unicode then you can just pick the appropriate string and use L"" or "".)

    To get the string after the "/m=", use the result of find. Since "/m=" has three characters, then pass the result of find + 3 to substr.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah, no, LPTSTR is actually TCHAR* (aka TSTR*).
    Grr to Microsoft typedefs. They confuse and disorient :/
    Sorry. Daved is right.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    This is the typedef I usually use (that way you don't need an #if #else block):
    Code:
    typedef std::basic_string<TCHAR>   Tstring;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:41 AM