Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: on Mon 18/02/2019 14:43:46

Title: Scroll a string and change letter. [SOLVED]
Post by: on Mon 18/02/2019 14:43:46
Morning.
I'm wearing the parser and I have a problem with a string. I would like to go through it letter by letter and change one that has an accent sign. But I find it impossible to compare because the compiler gives the following error: Scroll a string and change letter. I use this code.

Code (ags) Select
while (i < entrada.Length)
  {
    if(entrada.Chars[i]=="á")
    {
      entrada.ReplaceCharAt(entrada.Chars[i], 'a');
    }
    if(entrada.Chars[i]=="é")
    {
      entrada.ReplaceCharAt(entrada.Chars[i], 'e');
    }
    if(entrada.Chars[i]=="í")
    {
      entrada.ReplaceCharAt(entrada.Chars[i], 'i');
    }
    if(entrada.Chars[i]=="ó")
    {
      entrada.ReplaceCharAt(entrada.Chars[i], 'o');
    }
    if(entrada.Chars[i]=="ú")
    {
      entrada.ReplaceCharAt(entrada.Chars[i], 'u');
    }
    i++;
  }
Title: Re: Scroll a string and change letter.
Post by: Khris on Mon 18/02/2019 14:58:24
1. you need entrada.Chars[i]

2. ReplaceCharAt expects the index as int, and it returns the new string since strings are immutable.
You need entrada = entrada.ReplaceCharAt(i, 'e');
Title: Re: Scroll a string and change letter.
Post by: Snarky on Mon 18/02/2019 15:42:44
Quote from: Khris on Mon 18/02/2019 14:58:24
1. you need entrada.Chars[i]

Neo_One posted the code without any [code] tags, which means that [i] disappears, turning the text into italics. I've taken the liberty of editing the post to wrap the code in the necessary tags.

Neo_One, please do so in the future, using [code=ags]your code here[/code]. (If you want to post tags in the text itself, like I've done in this post, there's another trick to it.)
Title: Re: Scroll a string and change letter.
Post by: Crimson Wizard on Mon 18/02/2019 16:05:41
Also, if I remember correctly, in these conditions you need to use single quotemark instead of double because single marks mean character and double mean string, which cannot be compared to type "char" returned from Chars:
Code (ags) Select

if(entrada.Chars[i]=='á')


Quote from: Khris on Mon 18/02/2019 14:58:24
2. ReplaceCharAt expects the index as int, and it returns the new string since strings are immutable.
You need entrada = entrada.ReplaceCharAt(i, 'e');

Would not Replace("á", "a") work better? It replaces all cases of first string at once. So instead of the for loop the code could be like:
Code (ags) Select

entrada = entrada.Replace("á", "a");
entrada = entrada.Replace("é", "e");
entrada = entrada.Replace("í", "i");
entrada = entrada.Replace("ó", "o");
Title: Re: Scroll a string and change letter.
Post by: on Mon 18/02/2019 18:32:29
Thank you, but now gives error in the IF, where is the letter with the accent sign. GlobalScript.asc(40): Error (line 40): undefined symbol '-13'.
The number changes depending on whether it is á, é, etc... Is there a problem with accent signs?

OK Snarky I'll take it into account next time.
Title: Re: Scroll a string and change letter.
Post by: on Mon 18/02/2019 19:03:51
I solved of I've solved it this way.

Code (ags) Select
    entrada=txtPrompt.Text;
    acentos = entrada.Replace("á","a");
    entrada=acentos;
    acentos = entrada.Replace("é","e");
    entrada=acentos;
    acentos = entrada.Replace("í","i");
    entrada=acentos;
    acentos = entrada.Replace("ó","o");
    entrada=acentos;
    acentos = entrada.Replace("ú","u");
    entrada=acentos;


Many thanks to everyone for the inconvenience :)
Title: Re: Scroll a string and change letter. [SOLVED]
Post by: Crimson Wizard on Mon 18/02/2019 19:16:23
Neo_One, you are doing extra work there, you can do simply entrada = entrada.Replace("á","a"); that will have same result.

Also please tell if that actually works, because tbh I am not sure how well AGS deals with these letters.

Regarding earlier problem, that could be a bug in AGS compiler...
Title: Re: Scroll a string and change letter. [SOLVED]
Post by: on Mon 18/02/2019 22:26:09
Not Crimson.  If it is done that way the compiler does not do well the substitution and changes the letter to ?.

I think it may be a mistake not to support the parser words with accented signs.
Title: Re: Scroll a string and change letter. [SOLVED]
Post by: Snarky on Mon 18/02/2019 22:50:46
Quote from: Neo_One on Mon 18/02/2019 22:26:09
Not Crimson.  If it is done that way the compiler does not do well the substitution and changes the letter to ?.

That seems really unlikely. These two versions:

Code (ags) Select
    acentos = entrada.Replace("á","a");
    entrada = acentos;


And:

Code (ags) Select
    entrada = entrada.Replace("á","a");

Should always give the same result in entrada.

The only thing I can think of that could conceivably make a difference is how you input the code. If you copy-paste one version and type the other yourself, I wonder if the symbol could be stored differently in the text, so that in one version it doesn't match the character in the String.
Title: Re: Scroll a string and change letter. [SOLVED]
Post by: on Tue 19/02/2019 12:55:48
It is necessary to look at letter by letter because it is not known which parallels will be used by the user who will carry accent signs, and if two or more are joined it ends up breaking.
If you do it that way, in the end the interpreter does not recognize the word because he ends up transforming it into, for example: gestión inventário, in the chain it ends up being "gesti?n invent?rio".
It is necessary to do it this way so that it refreshes to each step and not of errors.
Title: Re: Scroll a string and change letter. [SOLVED]
Post by: Crimson Wizard on Tue 19/02/2019 13:42:34
What we are saying is that
Code (ags) Select

acentos = entrada.Replace("á","a");
entrada = acentos;

is performing same operation as
Code (ags) Select

entrada = entrada.Replace("á","a");


Both of the code above do exactly same thing in the end. The only difference is that in the first case you store the changed string in "acentos" and then store it in "entrada" too, in the second case you store the new string in "entrada" right away.

The error with "?" signs might be caused by something else, rather than the difference in this code.

This is not very important, if your code works for you that's okay, but I thought I'd mention this because it feels like there's a confusion about how strings work.