PDA

View Full Version : Exploding string


Chriham3
02/10/2015, 04:59 PM
Hello, I have a string like
"Hello||Sup||Nothing, wbu?||Just chilling", and I want to use the || as a delimiter, and loop through each to
SendClientMessageToAll(-1, "Hello");
SendClientMessageToAll(-1, "Sup");
SendClientMessageToAll(-1, "Nothing, wbu?");
etc..

The amount of messages is fluctuating. I haven't scripted on SA-MP for several years, so I am extremely rusty. Something like PHP's explode function.

In PHP, it's

<?php

$string = "Hello||Sup||Nothing, wbu?||Just chilling";

$messages = explode("||", $string);

foreach($messages as $message)
{
echo $message . "<br>";
}

?>


which outputs


Hello
Sup
Nothing, wbu?
Just chilling


Any help would be appreciated, thanks.

Kaliber
02/10/2015, 05:07 PM
I dont't know exactly what u want, but do it like this:

SendExplodedMessage(playerid,-1,"Hi|This|is|a|Test");
/*
Send the player:
Hi
This
is
a
Test
*/



stock SendExplodedMessage(playerid,color,const string[])
{
new tmp[128],p=strfind(string,"|"),old=-1;
if(p==-1) return SendClientMessage(playerid,color,string);
for(new i=p; ; i=strfind(string,"|",false,i+1)) {
strmid(tmp,string,old+1,(i==-1)?strlen(string):i,sizeof(tmp)),old=i;
SendClientMessage(playerid,color,tmp);
switch(i) {
case -1: break;
}
}
return 1;
}

Lordzy
02/10/2015, 05:26 PM
http://forum.sa-mp.com/showthread.php?t=85697

Vince
02/10/2015, 05:41 PM
I would rather look at the problem in the underlying system. I don't know the specifics of your system, but delimited strings are nearly always a bad way to transmit or store multiple pieces of data.