Selecting part of a string - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (
https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Selecting part of a string (
/showthread.php?tid=225749)
Selecting part of a string -
_Tommy - 14.02.2011
I couldn't find an answer for my problem, so I decided to post it here.
I have a function which should color-select a part of a string.
For example, I have the next string: "This is a simple 'text line' in my string".
I need somehow to make the new string look like: "This is a simple {FFFFFF}text line{NORMAL COLOR} in my string".
NORMAL COLOR - the default color I work with
FFFFF - Another color to "select" the needed text.
I need a way to replace the first " ' " with the selected color, to check where it ends and to set it back to the normal color (replace the second " ' " with the normal hex color - {}).
It would be easily done with scanf, but I also need to check each time how many selected words I have in my string. Each word which is between " ' " tags, should be color-selected.
I would be glad to get some help, thank you.
Re: Selecting part of a string -
_Tommy - 14.02.2011
Bump (second page)
Re: Selecting part of a string -
dice7 - 14.02.2011
Here is a very simple example of what you want, I hope you'll get the basic idea.
pawn Код:
new str[] = "This is a simple 'text line' in my string";
new text[128]; //This is a simple {FFFFFF}text line{NORMAL COLOR} in my string
new count = 0;
new character = false;
new i = 0;
while (str[i] != EOS)
{
if (str[i] == ''')
{
if (character)
{
text[count] = '{';
count++;
text[count] = '0';
count++;
text[count] = '0';
count++;
text[count] = '0';
count++;
text[count] = '0';
count++;
text[count] = '0';
count++;
text[count] = '0';
count++;
text[count] = '}';
count++;
}
else
{
text[count] = '{';
count++;
text[count] = 'F';
count++;
text[count] = 'F';
count++;
text[count] = 'F';
count++;
text[count] = 'F';
count++;
text[count] = 'F';
count++;
text[count] = 'F';
count++;
text[count] = '}';
count++;
}
character = !character;
i++;
}
text[count] = str[i];
i++;
count++;
}
text[count] = EOS;
print(text);
Note that you will need to implenet stuff like error checking (to much or to little ' chars, etc ...)
Re: Selecting part of a string -
_Tommy - 14.02.2011
Quote:
Originally Posted by dice7
Here is a very simple example of what you want, I hope you'll get the basic idea.
pawn Код:
new str[] = "This is a simple 'text line' in my string"; new text[128]; //This is a simple {FFFFFF}text line{NORMAL COLOR} in my string new count = 0; new character = false; new i = 0; while (str[i] != EOS) { if (str[i] == ''') { if (character) { text[count] = '{'; count++; text[count] = '0'; count++; text[count] = '0'; count++; text[count] = '0'; count++; text[count] = '0'; count++; text[count] = '0'; count++; text[count] = '0'; count++; text[count] = '}'; count++; } else { text[count] = '{'; count++; text[count] = 'F'; count++; text[count] = 'F'; count++; text[count] = 'F'; count++; text[count] = 'F'; count++; text[count] = 'F'; count++; text[count] = 'F'; count++; text[count] = '}'; count++; } character = !character; i++; } text[count] = str[i]; i++; count++; } text[count] = EOS; print(text);
Note that you will need to implenet stuff like error checking (to much or to little ' chars, etc ...)
|
Thank you
dice7, it works perfectly for me

I'll try to understand what you were doing here.