splliting text
#1

Hello everybody, so i was trying to spllit a string text (Ex: Text1\nText2\nText3)
for example:
Код:
new var1[][110] = "Text1\nText2\nText3", var2[][110], var3 = 0; 
for(new str = 0; str2 <= strlen(var1); str < str2; str++)
{
   if(var1[str] == "\n")
  {
     var2[var3++] = var1[str - 1];
  }
}
well... my example isn't correct one && isn't completed.
please some help, i have no idea how to do it...
Reply
#2

I'd try something like this. Not tested, but if my brain analyzed it correctly it should work.
PHP код:
new
    
originalString[] = "Text1\nText2\nText3";
new
    
posStart = -1,
    
posEnd 0,
    
index 0,
    
destination[5][110]; // maximum 5 splits
new i;
while(
originalString[i++] && index sizeof(destination)) // loop original string until end or until destination is full
{
    
// found linefeed
    
if(originalString[i] == '\n' || originalString[i] == EOS)
    {
        if(
posStart posEnd)
        {
            
posStart i;
        }
        else
        {
            
posEnd i;
        }
        if(
posEnd posStart)
        {
            
strmid(destination[index], originalStringposStartposEndsizeof(destination[]));
            
index++;
        }
    }

Reply
#3

Quote:
Originally Posted by Vince
Посмотреть сообщение
I'd try something like this. Not tested, but if my brain analyzed it correctly it should work.
PHP код:
new
    
originalString[] = "Text1\nText2\nText3";
new
    
posStart = -1,
    
posEnd 0,
    
index 0,
    
destination[5][110]; // maximum 5 splits
new i;
while(
originalString[i++] && index sizeof(destination)) // loop original string until end or until destination is full
{
    
// found linefeed
    
if(originalString[i] == '\n' || originalString[i] == EOS)
    {
        if(
posStart posEnd)
        {
            
posStart i;
        }
        else
        {
            
posEnd i;
        }
        if(
posEnd posStart)
        {
            
strmid(destination[index], originalStringposStartposEndsizeof(destination[]));
            
index++;
        }
    }

Thanks so much sir
+repped

Edit: it says you must spread some reputation around... so sorry sir
Reply
#4

Using sscanf would be much better
PHP код:
new splitted[3][110];
new 
text[] = "Text1\nText2\nText3";
sscanf(text,"p<\n>s[110]s[110]s[110]",splitted[0],splitted[1],splitted[2]); 
https://sampforum.blast.hk/showthread.php?tid=570927

Quote:
Originally Posted by Emmet_
Посмотреть сообщение
  • Delimiters
The previous version of sscanf had "p" to change the symbol used to separate tokens. This specifier still exists but it has been formalised to match the array and enum syntax. What was previously:

pawn Код:
sscanf("1,2,3", "p,iii", var0, var1, var2);
Is now:

pawn Код:
sscanf("1,2,3", "p<,>iii", var0, var1, var2);
The old version will still work, but it will give a warning. Enum specifications can include delimiters, and is the only time "<>"s are contained in other "<>"s:

pawn Код:
sscanf("1 12.0 Bob,c", "e<ifp<,>s[32]c>", var);
Note that the delimiter will remain in effect after the enum is complete. You can even use ">" as a specifier by doing "p<\>>" (or the older "p>").

When used with strings, the collection behaviour is overruled. Most specifiers are still space delimited, so for example this will work:

pawn Код:
sscanf("1 2 3", "p<;>iii", var0, var1, var2);
Despite the fact that there are no ";"s. However, strings will ONLY use the specified delimiters, so:

pawn Код:
sscanf("hello 1", "p<->s[32]i", str, var);
Will NOT work - the variable "str" will contain "hello 1". On the other hand, the example from earlier, slightly modified:

pawn Код:
sscanf("hello there>27", "p<>>s[32]i", str, var);
WILL work and will give an output of:

Код:
hello there
27
You can now have optional delimiters using "P" (upper case "p" to match other "optional" specifiers). These are optional in the sense that you specify multiple delimiters and any one of them can be used to end the next symbol:

pawn Код:
sscanf("(4, 5, 6, 7)", "P<(),>{s[2]}iiii", a, b, c, d);
This uses a "quiet section" to ignore anything before the first "(", and then uses multiple delimiters to end all the text.
Reply
#5

Have up to 50 parts (change all '[50]'s you find for whatever limit you want to set):
PHP код:
new text[] = "Text1\nText2\nText3\nText4"parts[50][110];
sscanf(text"p<\n>a<s[110]>[50]"parts);
for(new 
0sizeof(parts); && parts[i][0] != EOS++)
{
    
printf("%s"parts[i]);

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)