Find Character after Text ? - 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)
+--- Thread: Find Character after Text ? (
/showthread.php?tid=630919)
Find Character after Text ? -
JR_Junior - 20.03.2017
Hello everybody!
I have a small problem here and someone can help me, I thank you.
Well, I need to detect only the first character after a certain text, so I created this function:
PHP код:
#include <a_samp>
public OnFilterScriptInit()
{
//I want to return only the char after #25 = B
GetCharAfter("Hello #25Best", "#25");
return 1;
}
stock GetCharAfter(text[], find[])
{
new pos = strfind(text, find, true);
if(pos != -1)
printf("Char after Find: %s",text[pos+strlen(find)]);
}
But the code is returning:
PHP код:
Char after Find: Best
Re: Find Character after Text ? -
JR_Junior - 20.03.2017
Fixe with:
PHP код:
stock strgetc(const string[], index) {
if (index < 0)
return '\0';
new len = strlen(string);
if (index >= len)
return '\0';
return ispacked(string) ? string{index} : string[index];
}
PHP код:
stock GetCharAfter(text[], find[])
{
new pos = strfind(text, find, true);
if(pos != -1)
printf("Char after Find: %s",strgetc(text,pos+strlen(find)));
}
From:
https://github.com/oscar-broman/strl...ter/strlib.inc
Thanks!
Re: Find Character after Text ? -
AndySedeyn - 20.03.2017
PHP код:
#include <a_samp>
main() {
GetCharAfter("Hello #25Best", "#25");
}
GetCharAfter(text[], after[]) {
new
pos = strfind(text, after, true)
;
if(pos != -1) {
printf("Char after find: %c", text[pos + strlen(after)]);
}
return true;
}
This is your function modified to print the correct result. Notice that I used the %c specifier instead of %s. The string specifier will be replaced by the contents of 'text' after the xth position in the array.
Here's an example of what that means in action:
PHP код:
#include <a_samp>
main() {
new
text[7] = "Hello!"
;
printf("Everything: %s", text);
printf("Only the last 5 characters: %s", text[1]); // This will skip text[0] (start from text[1])
printf("Only the last 3 characters: %s", text[3]); // This will skip text[0], text[1] and text[2] (start from text[3])
printf("Only the 5th character: %c", text[4]); // This will only print out the character in text[4]
}
Re: Find Character after Text ? -
JR_Junior - 20.03.2017
Quote:
Originally Posted by AndySedeyn
PHP код:
#include <a_samp>
main() {
GetCharAfter("Hello #25Best", "#25");
}
GetCharAfter(text[], after[]) {
new
pos = strfind(text, after, true)
;
if(pos != -1) {
printf("Char after find: %c", text[pos + strlen(after)]);
}
return true;
}
This is your function modified to print the correct result. Notice that I used the %c specifier instead of %s. The string specifier will be replaced by the contents of 'text' after the xth position in the array.
Here's an example of what that means in action:
PHP код:
#include <a_samp>
main() {
new
text[7] = "Hello!"
;
printf("Everything: %s", text);
printf("Only the last 5 characters: %s", text[1]); // This will skip text[0] (start from text[1])
printf("Only the last 3 characters: %s", text[3]); // This will skip text[0], text[1] and text[2] (start from text[3])
printf("Only the 5th character: %c", text[4]); // This will only print out the character in text[4]
}
|
Thank you!