• Advertisement

Archived

This topic is now archived and is closed to further replies.

Put some strings together

This topic is 5527 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do I put some strings together? ____________________________________________________________ Nothing dosen´t exist. Does it?

Share this post


Link to post
Share on other sites
Advertisement
*sigh* More info please.
  • Do you mean character strings (char*'s)
  • Do you mean std::string?
  • Do you mean "How do i allocate space for strings"?
  • Do you mean "How do i concattonate strings" (i can't spell )
  • Do you mean "How do i make my own string class?




    [edited by - Evil Bill on December 2, 2002 2:20:52 PM]

    Share this post


    Link to post
    Share on other sites

    Line them up side by side - carefully take an end of each string between thumb and forefinger - cross ends over each other and twist. Bring first end under and through loop in second string and pull taught. Voila!

    (also, if you are using STL - WHICH YOU SHOULD BE - use MyString += MyOtherString or MyString = MyFirstString + MySecondString - if you are using C or C++ without STL (spit), use strcat).

    Oh by the way, what language\platform are you using and what exactly are you trying to do?

    Byee

    Share this post


    Link to post
    Share on other sites
    cough

    do you want to know in C, Visual Basic, C++, Java, Ruby?

    in c++ if you''re using std::string you can just add them


      
    #include <iostream>
    #include <string>

    using std::cout;
    using std::string;
    using std::endl;

    string greeting("hälsning");
    string name = "peter";
    string age("16");
    string space(" ");
    string wholeText = greeting + space + name + " who is" + age + " years old";

    cout << wholeText;

    cout << greeting << space << name << " who is " << age << " years old";

    Share this post


    Link to post
    Share on other sites
    quote:
    Original post by Robbo
    Line them up side by side - carefully take an end of each string between thumb and forefinger - cross ends over each other and twist. Bring first end under and through loop in second string and pull taught. Voila!

    you obviously never were a sailor
    strings tied in such a manner would not withstand the high seas!

    Share this post


    Link to post
    Share on other sites
    I want to put some chars together, without STL.

    ____________________________________________________________
    Nothing dosen´t exist. Does it?

    Share this post


    Link to post
    Share on other sites

        
    char a[50];
    char b[50];
    strcpy(a,"Hello ");
    strcpy(b,"World!");
    strcat(a,b);





    [edited by - Evil Bill on December 2, 2002 2:51:18 PM]

    Share this post


    Link to post
    Share on other sites
    quote:
    Original post by petewood
    any reason for not wanting to use std::string?


    Yeah, I don´t know STL yet and it works fine with standard C++.



    ____________________________________________________________
    Nothing dosen´t exist. Does it?

    Share this post


    Link to post
    Share on other sites
    quote:
    Original post by peter86
    Yeah, I don´t know STL yet

    Then now is a good time to start. std::string is easier to use than char arrays.
    quote:

    and it works fine with standard C++.

    So does std::string.

    Share this post


    Link to post
    Share on other sites
    quote:
    Original post by peter86
    Yeah, I don´t know STL yet and it works fine with standard C++.



    stl is standard c++. if u use strcat and such, u use standard c.



    "take a look around" - limp bizkit
    www.google.com

    Share this post


    Link to post
    Share on other sites
    if you''re using c++ it''s the standard way to handle characters. if you want to resort to c style character arrays etc then you''re deliberately making things difficult for yourself.

    the example i gave is about as hard as it gets for normal usage (actually not completely true, you can get very complicated if you wish)

    use it. feel comfortable. be able to say, ''i know a little of the C++ standard library''.

    i get the feeling you didn''t read the example. you didn''t even notice i dug up a swedish greeting for you

    Share this post


    Link to post
    Share on other sites
    quote:
    Original post by petewood
    i get the feeling you didn't read the example. you didn't even notice i dug up a swedish greeting for you


    Sorry, I missed the greeting. I want to understand my code so I didn´t take much time to look at the example. So how do you know swedish when you´re from Peru?

    ____________________________________________________________
    Nothing dosen´t exist. Does it?

    [edited by - peter86 on December 2, 2002 4:55:43 PM]

    Share this post


    Link to post
    Share on other sites
    I don''t understand why so many people are advocating the use of STL''s string class. While a pretty good class, it should ONLY be used for complex situations where usage of regular strings is difficult.

    There are many functions that operate on normal strings. Some of the better ones are:

    strcpy
    strcat

    (used to copy a string into another string, or concatenate one string onto the end of another).

    Also:

    sprintf
    sscanf

    (sprintf is like printf but prints to a string instead of the screen... VERY powerful. sscanf is like scanf but reads from a string rather than the keyboard. Also very useful -- you can use this to extract formatted output from a string).

    strtok

    (Breaks a string up into multiple strings by using a "token" character. For example, you can break a sentence up into individual words by making a space the token. Or a comma-separated list by making a comma a token).

    Only use std::string if you have a situtation where your string will be changing very frequently.

    - Andy Oxfeld

    Share this post


    Link to post
    Share on other sites
    Some of the worse ones:

    strchr
    strcmp
    strlen

    Also, some of the average rated ones:

    strncmp
    strpbrk
    strxfrm

    And of course... The highest rate of them all:

    strstr!!!!

    Big thanks to Andy for the idea for this useless post. Thanks Andy!

    Share this post


    Link to post
    Share on other sites
    quote:
    Original post by AndyOxfeld
    I don''t understand why so many people are advocating the use of STL''s string class. While a pretty good class, it should ONLY be used for complex situations where usage of regular strings is difficult.

    It''s obvious these people advocate the use of std::string because they know things you don''t. Usage of "regular" strings (really null-terminated character arrays) in C and C++ is always difficult because of the fact that arrays aren''t first-class entities in these languages, especially dynamically allocated ones.

    quote:
    There are many functions that operate on normal strings. Some of the better ones are:

    Actually, those functions do no range validation and have been the culprits time and time again for buffer exploits. If you insist on using the Standard C string functions, please use the counted versions - strncpy, strncat and snprintf.

    quote:
    (sprintf is like printf but prints to a string instead of the screen... VERY powerful.

    std::stringstream is better.

    quote:
    ...sscanf is like scanf but reads from a string rather than the keyboard. Also very useful -- you can use this to extract formatted output from a string).

    And can raise hell if you use the wrong format specifier.

    quote:
    strtok

    Destroys the original string, uses an internal buffer for storage and generally does a lot of bad things.

    quote:
    Only use std::string if you have a situtation where your string will be changing very frequently.

    You''ve also never heard of const?

    I didn''t mean to rip you up or anything, but authoritatively stating errant information can be dangerous when sucseptible newbies are around.

    Share this post


    Link to post
    Share on other sites
    but andy, all those things you say can be done too using the string class and other parts of the standard library, many in a typesafe way and also without relying on getting the lengths of strings correct, null terminating them etc.

    as you seem to love formatted output have you seen boost''s new typesafe code?

    Share this post


    Link to post
    Share on other sites
    There are many versions of string functions for various needs. For example, strtok has a re-entrant version, strtok_r. printf/scanf functions can be prefixed with a v and then passed a va_list, for writing functions with variable arguments.

    I made the assumption that a user would read the docs before using a function, thus I only gave the base function names.

    It's true that anything you can do with regular strings, you can do with STL strings. It's also true that this is a game development webpage, and for maximum performance in games, bloat should be kept to a minimum.

    The author of the original post did not go into much detail, but let's say that he wanted to have the user enter a first name and a last name separately, but in some places, he wanted to refer to them together. You could do:

    std::string mystr;
    mystr = first + " " + last;

    or you could do

    char mystr[31];
    snprintf(mystr, 30, "%s %s", first, last);

    Which is better?

    Advantages of STL version:
    1. No limits to size
    2. More readable to newbies

    Advantages of stdio version:
    1. Portable -- while games will most likely be running on Windows, or a UNIX system new enough to have a C++ compiler, this is less of an issue that it'd usually be. But there are still many types of programming where the system does not have a C++ compiler, and thus only the C-way will work.

    2. Faster: The STL version starts by allocating enough memory to copy first into mystr, then memory for the space, then memory for last. Actually, due to the way STL's string class works, probably only two allocations will be done, but that's still a simple case, and it's still twice as much memory allocations as with the C way.

    3. Flexibility: Using the powerful format string specifiers, much more complex things can be done with snprintf than with simple string concactenation.

    Maybe the author does have a complex situation where using STL's string class is warranted. As I think I mentioned in my earlier post, STL's string class is very good, far less bloated than the MFC class, or the apstring class used in some school systems. But there's no point in using it when a simpler solution will suffice.

    And Oluseyi: "It's obvious these people advocate the use of std::string because they know things you don't." A bit arrogant... I only wanted to provide the poster, and any other readers, with the best advice possible.

    To petewood: (quote snipped) "and also without relying on getting the lengths of strings correct, null terminating them etc." Actually, manipulation of the null termination character is very powerful. Using standard strings, stuff like this can be done:

    str[20] = 0; /* Strip off all but first 19 characters */
    str[strlen(str) - 5] = 0; /* Strip off last 5 characters */
    printf("%s", str + 3); /* Print str with first 3 characters stripped off */
    snprintf(str + strlen(str), 40, "%.4f %d %ld", var1, var2, var3); /* Concactenate a formatted string onto the end of an existing string */

    To Melraidin: You obviously missed the entire point of my post, which was that I firmly believe std::string is NOT the best solution to every problem, and should NOT always be used in place of standard strings.

    - Andy Oxfeld

    [edited by - AndyOxfeld on December 2, 2002 11:04:43 PM]

    Share this post


    Link to post
    Share on other sites
    quote:
    Original post by AndyOxfeld
    2. Faster: The STL version starts by allocating enough memory to copy first into mystr, then memory for the space, then memory for last. Actually, due to the way STL''s string class works, probably only two allocations will be done, but that''s still a simple case, and it''s still twice as much memory allocations as with the C way.



    yes, but you''re ignoring the fact that snprintf has to parse the format string ("%s %s"), which is overly complicated when you can do it in code.



    "That''s not a bug, it''s a feature!"
    --me

    Share this post


    Link to post
    Share on other sites
    While I agree with you Andy that in some cases C strings can certainly be preferable, I''d really hate to see any code like what you posted:

    quote:

    str[20] = 0; /* Strip off all but first 19 characters */
    str[strlen(str) - 5] = 0; /* Strip off last 5 characters */
    printf("%s", str + 3); /* Print str with first 3 characters stripped off */
    snprintf(str + strlen(str), 40, "%.4f %d %ld", var1, var2, var3); /* Concactenate a formatted string onto the end of an existing string */



    Quite honestly, if I saw that in production code an employee had written there''d either have to be a good explanation for it or they''d be moving themselves just a little closer to an early (mandatory) retirement.

    OTOH, as I stated above, there are times where I''d use C strings. ie: simple string manipulation where performance is imperative.

    Share this post


    Link to post
    Share on other sites
    AndyOxfeld, if you''re saying that we shouldn''t use std::string because there won''t be a c++ compiler to compile it for the target platform, then that''s a completely different issue. If there wasn''t a c++ compiler I wouldn''t even be considering it, right?

    But I''m using c++, I have a compiler, I like type safety etc, objects, encapsulation.

    If I use std::string to store all my strings in my classes then if it came down to it I could still use many of the functions you mention by calling string::c_str();

    Granted, there might not be a c++ standard library implementation available but then we''re getting quite specific about the situation in which you''re saying you wouldn''t be able to use std::string.

    You use what your target platform dictates. Know your tools.

    Share this post


    Link to post
    Share on other sites
    quote:
    Original post by AndyOxfeld
    For example, strtok has a re-entrant version, strtok_r.

    Not mandated by the C++ Standard - it''s a compiler-specific extension.
    quote:

    It''s true that anything you can do with regular strings, you can do with STL strings. It''s also true that this is a game development webpage, and for maximum performance in games, bloat should be kept to a minimum.

    It''s also a myth that std::string necessarily results in bloat.
    quote:

    Which is better?

    The std::string version, obviously.
    quote:

    Advantages of stdio version:
    1. Portable -- while games will most likely be running on Windows, or a UNIX system new enough to have a C++ compiler, this is less of an issue that it''d usually be. But there are still many types of programming where the system does not have a C++ compiler, and thus only the C-way will work.

    Since the OP is programming in C++, I hardly think lack of a C++ compiler is going to be a problem. Stop inventing problems.
    quote:

    2. Faster:

    Complete myth. It depends what you are doing, and there are known scenarios where std::string will outperform C strings. Did you ever learn what "premature optimisation" means?
    quote:

    3. Flexibility: Using the powerful format string specifiers, much more complex things can be done with snprintf than with simple string concactenation.

    Wrong. See stringstreams. For instance, using the "powerful" string format specifiers, how do you print a user-defined type?
    quote:

    Maybe the author does have a complex situation where using STL''s string class is warranted.

    No, he appears to have a simple situation, also warranting std::string.
    quote:

    As I think I mentioned in my earlier post, STL''s string class is very good, far less bloated than the MFC class, or the apstring class used in some school systems. But there''s no point in using it when a simpler solution will suffice.

    It *is* the simple solution: it encapsulates memory management and pointer tracking, and overloads various operators.
    quote:

    And Oluseyi: "It''s obvious these people advocate the use of std::string because they know things you don''t." A bit arrogant... I only wanted to provide the poster, and any other readers, with the best advice possible.

    Then you failed.
    quote:

    To petewood: (quote snipped) "and also without relying on getting the lengths of strings correct, null terminating them etc." Actually, manipulation of the null termination character is very powerful. Using standard strings, stuff like this can be done:

    All of those can be done with std::string, and in a far clearer manner.
    quote:

    To Melraidin: You obviously missed the entire point of my post, which was that I firmly believe std::string is NOT the best solution to every problem, and should NOT always be used in place of standard strings.

    The weight of opinion is obviously not in your favour.

    Share this post


    Link to post
    Share on other sites
    "It''s also a myth that std::string necessarily results in bloat" and "Complete myth. It depends what you are doing, and there are known scenarios where std::string will outperform C strings. Did you ever learn what "premature optimisation" means?"
    It depends on what you do with it. strcat()ing a few strings will be faster than strA + " " + strB, because of the allocation, assignment, and deleting of memory that string causes. I''m sure he knows what premature optimisation is, but you need to design the algorithm beforehand. If you are using std::strings, profile and find you need to change to C-strings, you''ll probably have a fair bit of work to do - especially if the strings are used all over the place.

    "...and there are known scenarios where std::string will outperform C strings. "
    Like what? (just curious) Apart from strlen().

    There''s certianly arguments for and against, but std::string tends to be the more C++ OOP way about it.
    I use my own wee string class which uses both dynamic memory and a little char[16] buffer for short strings (read about the idea somewhere, dunno where). My class actually outperforms std::string doing casual stuff (d=a+b+c, etc.).

    2p

    Share this post


    Link to post
    Share on other sites
    quote:
    Original post by Evil Bill
    It depends on what you do with it.

    Isn't that exactly what I just said?
    quote:

    If you are using std::strings, profile and find you need to change to C-strings

    That shouldn't happen. Instead, you write a string class geared towards your needs, but *only* when you've identified the ready-made solutions are insufficient.

    [edited by - SabreMan on December 3, 2002 8:21:58 AM]

    Share this post


    Link to post
    Share on other sites

    This topic is 5527 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

    If you intended to correct an error in the post then please contact us.

    Guest
    This topic is now closed to further replies.

    • Advertisement