Thread: String handling for a newbie

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    8

    String handling for a newbie

    I have two questions about strings.

    1: How do I compare two strings within an if statement?

    2: How do I glue two strings together to make one longer string?

    I know these are simple issues but I haven't found answers in online tutorials.

    Thanks,

    BeerGut


    ps: I'm using C++ strings.
    Last edited by BeerGut; 10-23-2009 at 06:35 AM.

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    1: How do I compare two strings within an if statement?
    Ans: strcmp

    2: How do I glue two strings together to make one longer string?
    Ans: strcat

    it is in string.h

    google strcat

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BeerGut
    1: How do I compare two strings within an if statement?
    You can compare them using the usual comparison operators, e.g., str1 == str2.

    Quote Originally Posted by BeerGut
    2: How do I glue two strings together to make one longer string?
    Use the overloaded operator+ to concatenate, e.g., str1 + str2.

    EDIT:
    Refer to the website linked to in my signature for a reference.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Of course laserlight assumes you're using std::string for string variables, which you should definitely do. RockyMarrone assumes you're using C-style strings (char arrays), which you should not do.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    8

    Talking Thanks for all your suggestions

    I am using std::string and thanks for your help.
    You have answered my questions.

    Thanks again,
    BeerGut

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    8

    Question Converting strings to ints

    I have another question about strings.

    If I have str1 which contains "123" how do I convert this to the int 123 and back again.
    I have some dates in string format yyyymmdd which I wish to increment by days and months and years.

    Thanks for reading,
    BeerGut

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by BeerGut
    If I have str1 which contains "123" how do I convert this to the int 123 and back again.
    Read up on how to use a stringstream. If you happen to have access to the Boost libraries, you could use boost::lexical_cast instead (its implementation uses a stringstream).

    Quote Originally Posted by BeerGut
    I have some dates in string format yyyymmdd which I wish to increment by days and months and years.
    Again, if you have access to Boost you could use Boost.Date_Time. If you do not have access to any such date handling library, then you have to implement this yourself.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Newbie: having a problem with my string function.
    By dsharp in forum C Programming
    Replies: 3
    Last Post: 10-20-2003, 10:11 AM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM