Jump to content


Photo

If String Is At Position


  • Please log in to reply
8 replies to this topic

#1 Rixeno

Rixeno

    The One 'n' Only

  • New Member
  • 513 posts

Posted 11 January 2008 - 01:06 AM

String_at_pos
What does this do?
This script returns whether or not a substring is found the desired position in a string.
How can I use it?
Well, the first character is 0, then 1, and so on. It compares every character and returns "true" only if all the characters are found at the right position.
Do you want credit?
Well, this is my second attempt at this, so it would be very nice to get some credit for this script.
So where's the script?
Right here:
//string_at_pos
//Returns whether or not a substring is found at the desired location 
//in the main string
//Argument(s):
//argument0 = main string
//argument1 = sub-string
//argument2 = position index
//Returns: TRUE/FALSE
//Written by Rixeno
var i1,i2,str1,str2;
i2=0
for (i1=1;i1<string_length(argument1)+1;i1+=1)
{
str1=string_char_at(argument0,i1+argument2)
str2=string_char_at(argument1,i1)
    if (str2==str1)
    i2+=1;
}
if (i2>string_length(argument1)-1)
    return true;
        return false;
Enjoy ;)
  • 0
Do you need an example? You can PM me a request and I'll try my best.
Creations:
- Extra String Functions - Sprite Chroma Key - String at Position - Word Censor - Draw_Line_Width (Unreg GM6-) - Shadowed Text - Draw N-Gon - PIXEL PERFECT COLLISION ENGINE-

#2 $pecter

$pecter

    GMC Member

  • GMC Member
  • 1616 posts
  • Version:GM8

Posted 11 January 2008 - 01:25 AM

Wouldn't it be faster to check if the substr actually exists before cycling through every character?

EDIT: Like so,
//string_at_pos(string,substring,location)
var substr,pos,str;
str=argument0;
substr=argument1;
pos=string_pos(substr,str);

while(pos!=argument2 && string_count(substr,str)>0)
{
    str=string_delete(str,0,pos);
    pos=string_pos(substr,str);
}

if pos=argument2
return 1
else
return 0

Edited by $pecter, 11 January 2008 - 01:26 AM.

  • 0

#3 torigara

torigara

    GMC Member

  • GMC Member
  • 6507 posts

Posted 11 January 2008 - 09:19 AM

Umm, I might have read it wrongly, but is it something different than:
return (string_copy(argument0, argument2, string_length(argument1)) == argument1);
A short example of the usage of the script will be appreciated. For instance, is the script meant to yield the following result?
string_at_pos("alphalpha", "al", 1) will be true
string_at_pos("alphalpha", "la", 1) will be false
string_at_pos("alphalpha", "al", 3) will be false
string_at_pos("alphalpha", "al", 5) will be true
string_at_pos("alphalpha", "al", 9) will be false

Edited by torigara, 11 January 2008 - 10:06 AM.

  • 0

#4 $pecter

$pecter

    GMC Member

  • GMC Member
  • 1616 posts
  • Version:GM8

Posted 11 January 2008 - 09:26 AM

Umm, I might have read it wrongly, but is it something different than:

return (string_copy(argument0, argument2, string_length(argument1)) == argument1);

<{POST_SNAPBACK}>

I think you win. <_<
  • 0

#5 Knightmare

Knightmare

    OMGLOLWTFZORZ!1!1!!11

  • New Member
  • 867 posts
  • Version:Unknown

Posted 11 January 2008 - 08:53 PM

Can't you use string_pos function...
  • 0


#6 $pecter

$pecter

    GMC Member

  • GMC Member
  • 1616 posts
  • Version:GM8

Posted 11 January 2008 - 09:47 PM

That will only return the first occurrence.

eg if string_pos('al','alnnalnnn')=5
will return false even though 'al' is indeed located at position 5
  • 0

#7 Rixeno

Rixeno

    The One 'n' Only

  • New Member
  • 513 posts

Posted 12 January 2008 - 12:18 AM

Ah well, I just made it cause I needed it in my program, it works perfectly with me without a problem. There is most likely better ways of doing it, that was my way... I'm trying to learn some new stuff/techniques after 3-4 years... Either way, it does it's supposed to do.
It takes a character at the desired position in the main string and compares it with a character from the substring. If it is true, it adds 1 to i2. In the end, if i2 is equal to the length of the substring, meaning that they are all the same characters, it will return true, otherwise, it is false.
You can use it like this:
string_at_pos("Hello people","lo",3) will return TRUE
string_at_pos("Hello people","lo",10) will return FALSE
@torigara:
Yes sorry I didn't explain. The first character is 0, so you would have to subtract 1 from all the numbers the get that answer. But yes, that's what it does.
@$pecter:
Yep, and my script will work any position, so it's pretty useful :P

Edited by Rixeno, 12 January 2008 - 12:21 AM.

  • 0
Do you need an example? You can PM me a request and I'll try my best.
Creations:
- Extra String Functions - Sprite Chroma Key - String at Position - Word Censor - Draw_Line_Width (Unreg GM6-) - Shadowed Text - Draw N-Gon - PIXEL PERFECT COLLISION ENGINE-

#8 $pecter

$pecter

    GMC Member

  • GMC Member
  • 1616 posts
  • Version:GM8

Posted 12 January 2008 - 12:24 AM

@torigara:
Yes sorry I didn't explain. The first character is 0, so you would have to subtract 1 from all the numbers the get that answer. But yes, that's what it does.

Strings start at 1.
  • 0

#9 Rixeno

Rixeno

    The One 'n' Only

  • New Member
  • 513 posts

Posted 12 January 2008 - 05:27 PM

Oops, oh well, it still does what it's supposed to, the only thing is that the first character would be defined as zero, it's easy to change though... :P
  • 0
Do you need an example? You can PM me a request and I'll try my best.
Creations:
- Extra String Functions - Sprite Chroma Key - String at Position - Word Censor - Draw_Line_Width (Unreg GM6-) - Shadowed Text - Draw N-Gon - PIXEL PERFECT COLLISION ENGINE-