Transfering strings
#1

How do you transfer a string from one array to another?

For example if I have

new string1[8] = "Hello";

and

new string2[8] = "Goodbye";

How do I set string1 to what is in string2?
Reply
#2

pawn Код:
format(string1,sizeof(string1),"%s",string2);
Reply
#3

pawn Код:
format(string1, sizeof(string1), "%s", string2);
EDIT: was abit too late, had this page tabbed sorry.
Reply
#4

pawn Код:
strmid(string2, string1, false, strlen(string1), sizeof(string2));
pawn Код:
strmid(string2, string1, false, strlen(string1), 128);
Reply
#5

IF you wanted a 'hello goodbye' string, you could do this:

pawn Код:
format(string1, sizeof(string1), "%s %s",string1, string2);
Yes, you can insert a string into itself.
Reply
#6

Quote:
Originally Posted by Schurman
Посмотреть сообщение
IF you wanted a 'hello goodbye' string, you could do this:

pawn Код:
format(string1, sizeof(string1), "%s %s",string1, string2);
Yes, you can insert a string into itself.
strcat (appends)

strins (recreates it)
Reply
#7

http://forum.sa-mp.com/showpost.php?...1&postcount=19
Reply
#8

I think strmid would be easiest, but is it the best? Thanks for the replies guys.
Reply
#9

Its' the fastest, besides strcpy that uses strcat I believe.
Reply
#10

If the two arrays/strings has the same size, using
pawn Код:
new string1[8] = "Hello",string2[8] = "world!";
string1 = string2;
will be the fastest (and easy to use).

If you want to add some more strings behind a string you can use strcat/strcpy.
However, if more than 4-5 strcat is required to make a string, or there's non-string values to be added into the string, format will be faster.
pawn Код:
new string[128] = "Hello,",playername[MAX_PLAYER_NAME];
GetPlayerName(playerid,playername,sizeof(playername));
strcat(string,playername);
strcat(string,"! Welcome to ");
strcat(string,servername);
strcat(string,"!");
has the similar efficiency as:
pawn Код:
new string[128],playername[MAX_PLAYER_NAME];
GetPlayerName(playerid,playername,sizeof(playername));
format(string,sizeof(string),"Hello,%s! Welcome to %s!",playername,servername);
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)