|
-
Oct 31st, 2001, 10:18 PM
#1
Thread Starter
Addicted Member
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.
-
Oct 31st, 2001, 10:27 PM
#2
PowerPoster
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);
-
Oct 31st, 2001, 10:29 PM
#3
Thread Starter
Addicted Member
Ok, and to get the 4 last letter of a string?
-
Oct 31st, 2001, 10:46 PM
#4
PowerPoster
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];
}
-
Oct 31st, 2001, 10:55 PM
#5
Thread Starter
Addicted Member
-
Nov 1st, 2001, 12:58 PM
#6
Monday Morning Lunatic
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
-
Nov 1st, 2001, 01:03 PM
#7
transcendental analytic
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.
-
Nov 1st, 2001, 01:08 PM
#8
Monday Morning Lunatic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|