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

Discussion in 'Programming' started by EvanSki, Aug 14, 2019.

  1. EvanSki

    EvanSki King of Raccoons

    Joined:
    Apr 17, 2018
    Posts:
    580
    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
     
  2. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    833
    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:
     
    Last edited: Aug 15, 2019
    EvanSki and Nocturne like this.
  3. EvanSki

    EvanSki King of Raccoons

    Joined:
    Apr 17, 2018
    Posts:
    580
    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
     
  4. CloseRange

    CloseRange Member

    Joined:
    Jul 2, 2016
    Posts:
    833
    @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 likes this.
  5. EvanSki

    EvanSki King of Raccoons

    Joined:
    Apr 17, 2018
    Posts:
    580
    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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice