Check typed text box

Started by Vincent, Thu 11/07/2024 08:01:37

Previous topic - Next topic

Vincent

@Crimson Wizard Thank you it's working good now! 😊

@Khris Thanks. The "CompareTo" seems to be working much better than the "IndexOf" for this kind of task. Here is what I got so far:

Code: ags
function CheckWords()
{
  // Array to store words
  String words[]; 

  // Split the input sentence into words
  words = txt.Split(" ");
  
  // Print out the words
  for (int i = 0; i < words.Length; i++) 
  {
    if (words[0].CompareTo("where") == 0) 
    {
      Display(String.Format("Word is: %s", words[i])); 
    }
    else
    {
      Display("No words to check.");
    }
  }
}

I don't know how to write something like -
if 3 words has been found then the display will format 3 words, if 2 words has been found then the display will format 2 words and so on and if none has been found then he just print no words to check. I could add those manually each time but I'd like to avoid doing that a thousand of times

eri0o

You can use == directly for string comparison, you only need to use CompareTo if you need to make it case insensitive, but you can also just lower case the input string and then just use word == "what".

Vincent

#22
@eri0o Thanks for the tips. I was wondering how can I check how many words has been found and adding them to the formatting string? For example let's suppose I have something like this:

Code: ags
if (words[0].CompareTo("where") == 0) 
{
  if (words[1].CompareTo("is") != 0) Display(String.Format("Word is: %s", words[i])); 
  else Display(String.Format("Words are: %s %s", words[i], words[i])); 
}

// so if one word has been found:
// Display(String.Format("Word is: %s", words[i])); 
// if two word has been found:
// Display(String.Format("Words are: %s %s", words[i], words[i]));
// and so on -

Now while I was writing this message I thought that maybe I can add a new string called "message" which take care of that like:

// if one word has been found:
Code: ags
message = String.Format("%s", words[i])); 
// if two word has been found:
Code: ags
message = String.Format("%s %s", words[i], words[i])); 

And then later on I could simple call:
Code: ags
Display(String.Format("%s", message));

eri0o

Note that if you lower case the entire string and if you split you can then join the string back with a normalized separator and then you can use IndexOf again checking this specific separator to get the information if a specific string exists in it. And actually if you use IndexOf(needle) and then check if the Char[ineedle+needle.Length) is a separator or a normal letter.

I recommend also use System.Log to print the strings - or alternatively add the variables to watch pane so you can check under the debugger.

Khris

@Vincent Most languages have an Array.Join() method, the reverse of String.Split().
With AGS you have to for-loop over the array and add it to a string.

As an aside, regarding
Code: ags
  Display(String.Format("%s", message));
There's no need for this. First of all, Display already supports string formatting. But more importantly, you never need to write the above; you can simply call Display(message); since it's already a String.

Vincent

Thank you guys for the tips I really appreciated, I will keep them in mind now. Yesterday I tried to keep coding the piece of script I shared with you, just to see how this will look in the long term. While all of this seems to be working good the whole script looks like an nightmare and difficult to keep track of. I might need to split each step on different functions and not doing everything in a single one.

eri0o

Quote from: Khris on Sat 13/07/2024 09:29:45@Vincent Most languages have an Array.Join() method, the reverse of String.Split().

Hey @Khris, in the latest ags4 AGS has String.Join that does this. :)

Vincent

Yeah I notice there is now String.Join() which seems to be looking cool. I've been trying to use it but it always gives me an error that it can't convert string[] to string or viceversa, I am sure I am doing something wrong. By the way, I've been trying to code a different version to split the words into array. While this seems to be working there is something which it doesn't for some reason.

Code: ags
#define MAX_NUM_WORDS 7
  
String Words[MAX_NUM_WORDS];
int numWords = 0;

function SplitWordsIntoArray(String input)
{
  bool _go = true;
  numWords = 0;
  int space;
  
  while(_go)
  {
    space = input.IndexOf(" ");
    
    if (space == -1) // only a single word has been found
    {
      Words[numWords] = input; 
      _go = false;
    }
    else // add words into array
    {
      Words[numWords] = input.Truncate(space);
      input = input.Substring(space + 1, input.Length);
    }
    numWords++;
  }
}

Now if I use to "Display" those words its working good:

Code: ags
int i = 0;
while(i < numWords)
{
  Display(Words[i]);
  i++;
}

but if I use a label then it display only the last word, I've been trying to do this also in rep_exe but its always gives me only the last word

Khris

If you set the label text in a loop by just
Code: ags
  lblWords.Text = Words[i];
it'll obviously just override the entire text each time.

Do this instead:
Code: ags
  String allWords = Words[0];
  for (int i = 1; i < numWords; i++) {
    allWords = allWords.Append(String.Format(" %s", Words[i]));
  }

Vincent

Thanks Khris, that's right, I just had to append the whole thing which make sense.

SMF spam blocked by CleanTalk