Exploding string
#1

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.
Reply
#2

I dont't know exactly what u want, but do it like this:

PHP код:
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;

Reply
#3

https://sampforum.blast.hk/showthread.php?tid=85697
Reply
#4

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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)