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.
Printable View
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.
Use "StrCpy" to add two strings into another string like this:
VB Code:
char str1[]="String1"; char str2[40]="String2; char str3[40]; strcpy (str3,str1); strcat(str3,str2);
Ok, and to get the 4 last letter of a string?
ooopppss...forgot that part:D
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];
}
Thank you for the help.
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 :)
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.
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).