Results 1 to 8 of 8

Thread: String, VB to C++

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    Québec, Canada
    Posts
    131

    String, VB to C++

    Hello, I move in C++ and I have problem with strings. Is there any C++ equivalent to "&" to append two string? Another question: How can I get the 4 last character of a string? And the first 4?

    Thanks for any help.
    Khavoerm Irithyl

  2. #2
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Use "StrCpy" to add two strings into another string like this:

    VB Code:
    1. char str1[]="String1";
    2.   char str2[40]="String2;
    3.   char str3[40];
    4.   strcpy (str3,str1);
    5.   strcat(str3,str2);
    Baaaaaaaaah

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    Québec, Canada
    Posts
    131
    Ok, and to get the 4 last letter of a string?
    Khavoerm Irithyl

  4. #4
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    ooopppss...forgot that part

    To get a certain number of characters from a string, it should be in an array.

    To get the last four characters from a string "my":

    PHP Code:
        char my[20] = "IHateBillGates";
        for (
    int i=strlen(my)-4;i<=strlen(my);i++)
        {
            
    cout<<my[i];
        } 

    And to get the first four characters from a string "my":

    PHP Code:
        char my[20] = "IHateBillGates";
        for (
    int i=0;i<=3;i++)
        {
            
    cout<<my[i];
        } 
    Baaaaaaaaah

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    Québec, Canada
    Posts
    131
    Thank you for the help.
    Khavoerm Irithyl

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The language doesn't have direct support for strings as strings, but you can use character arrays which have been around since C. That's what the char* is (pointer to char).

    If you actually want a string, you use the standard library string class, on which there are many many wonderful threads in this forum

    Just as an FYI, because there's nothing wrong with C-style char arrays
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    the usage of C-style char arrays is usually a lot more efficient than strings classes, but you need to think more lowlevel and offer time in learning and time to develope as well as consider the insecurity of using C-style strings.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It's nearly always more efficient, unless you start reading many megabytes into it, in which case using a rope class is more efficient (see the STL) for most manipulations, because a C-style string must be stored in consecutive bytes (this is why text boxes under any Windows other than NT can only have 64K of text in, because the edit control in Win9x and below is 16-bit, meaning that it can only have up to one segment'sworth of memory = 64K).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width