GML why does string "index" not start at 0?

Evanski

Raccoon Lord
Forum Staff
Moderator
Heres an example of why im so mad at this

so you have a string: [pizza] hut
Code:
1. end_pos = string_pos("]", my_arguments);
2. print_string = string_delete(my_arguments,(end_pos),( (string_length(my_arguments)) ));

3. print_string = string_delete(print_string,2,1);

4. show_message(print_string);
okay so line 1 will find the position of "]" just fine
line 2 deletes "]" and everything after it just fine

line 3 is where my blood boils

THE MANUAL
You can use this function to remove a specific part of a string. So, you supply the input string and the start and end position within that string to remove characters (index starts at 1) and the function will return a new string without that section in it.

Syntax:
string_delete(str, index, count);

Argument Description
str The string to copy and delete from.
index The position of the first character to remove.
count The number of characters to remove.

Example:
str1 = "Helloo World";
str2 = string_delete(str1, 5, 1);

That works all well and good because postion 5 and 6 are "o"

Back to my example
our string is [pizza
i want to remove "["
its the first charecter, pos 1

so you should just be able to
Code:
print_string = string_delete(print_string,1,1);
because [ is in the first position, index counts from 1, so it should be 1

no, you get [pizza

but using 2
Code:
print_string = string_delete(print_string,2,1);
works, but [ is not the second character, its the first

So what is the first character of a string? an invisible space?

to add more context
str1 = "Helloo World";
str2 = string_delete(str1, 5, 1);

Helloo World
its deleting the 6th charecter not the 5th even though in code you said delete the 5th one
 

CloseRange

Member
no, the first character of YOUR string is an invisible space.
I copied your code just to be sure:

Code:
my_arguments = "[pizza] hut"
end_pos = string_pos("]", my_arguments);
print_string = string_delete(my_arguments,(end_pos),( (string_length(my_arguments)) ));
print_string = string_delete(print_string,1,1);
show_message(print_string);
all i did was set my_arguments = "[pizza] hut" like you said
and then added a show message.
the result? I get pizza as expected.

the problem probably comes in how you decide to get the string in the first place.
there is an invisible character there, try doing a string_length on your code to see if the size is wrong (it might be right but still a good check)
idk how you get the string so who knows, if you separate a big string on new line characters you might have forgotten to get rid of the carriage return.

as for why it starts at 0 instead of 1:
Well in GML string indexing always starts at 1 because Delphi strings start counting at 1 and that is what Mark Overmars (the original author is used to) - we are now stuck with that

Russell
 
Last edited:

Evanski

Raccoon Lord
Forum Staff
Moderator
no, the first character of YOUR string is an invisible space.
I copied your code just to be sure:

Code:
my_arguments = "[pizza] hut"
end_pos = string_pos("]", my_arguments);
print_string = string_delete(my_arguments,(end_pos),( (string_length(my_arguments)) ));
print_string = string_delete(print_string,1,1);
show_message(print_string);
all i did was set my_arguments = "[pizza] hut" like you said
and then added a show message.
the result? I get pizza as expected.

the problem probably comes in how you decide to get the string in the first place.
there is an invisible character there, try doing a string_length on your code to see if the size is wrong (it might be right but still a good check)
idk how you get the string so who knows, if you separate a big string on new line characters you might have forgotten to get rid of the carriage return.

as for why it starts at 0 instead of 1:
Hmm youre right What i start with is print [pizza] hut
and delete the print part, so i might not be deleting the space after print

I still hate the fact it counts from 1
 

CloseRange

Member
@EvanSki in that case you could just do this:
Code:
txt = "print [pizza] hut";
txt = string_delete(txt, 1, string_pos("[", txt));
txt = string_delete(txt, string_pos("]", txt), string_length(txt));
should delete everything before the [ (including the bracket itself) and then delete everything after the ] (also including the bracket)
worked for me at least
 

Evanski

Raccoon Lord
Forum Staff
Moderator
@EvanSki in that case you could just do this:
Code:
txt = "print [pizza] hut";
txt = string_delete(txt, 1, string_pos("[", txt));
txt = string_delete(txt, string_pos("]", txt), string_length(txt));
should delete everything before the [ (including the bracket itself) and then delete everything after the ] (also including the bracket)
worked for me at least
Thanks but I fixed it how im doing it by just adding 1 to the count to delete because i wasnt accounting for the space in between
 
Top