String handling for a newbie

This is a discussion on String handling for a newbie within the C++ Programming forums, part of the General Programming Boards category; I have two questions about strings. 1: How do I compare two strings within an if statement? 2: How do ...

  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
    20,955
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,892
    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
    20,955
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21