Is there a way to query the beginning of a string?

Shadecerule
13 May 2018, 23:01

Is there a command that lets you determine the first letter of a string? I want to be able to determine if the first letter of a player's custom variable is a vowel or not.


DarkLizerd
13 May 2018, 23:28

Left(the string you are looking at,1)
1 for the first letter


DarkLizerd
13 May 2018, 23:29

oops.. slow internet...


K.V.
14 May 2018, 02:12

Hello.

StartsWith will check that, but you'll need to use foreach to check for each vowel.

Create a function called StartsWithVowel.

Give it a single parameter: string

Set the return type to: boolean

Put this for the script in code view:

  vowels = Split("a;e;i;o;u", ";")
  starts_with_vowel = false
  foreach (vowel, vowels){
    if (StartsWith(LCase(string), vowel)){
      starts_with_vowel = true
    }
  }
  return (starts_with_vowel)

That will look like this in full code view:

<function name="StartsWithVowel" parameters="string" type="boolean">
  vowels = Split("a;e;i;o;u", ";")
  starts_with_vowel = false
  foreach (vowel, vowels){
    if (StartsWith(LCase(string), vowel)){
      starts_with_vowel = true
    }
  }
  return (starts_with_vowel)
</function>

To use it:

if (StartsWithVowel("Iterate")){
  // This starts with a vowel!
}
else {
   // This does not start with a vowel!
}

Shadecerule
14 May 2018, 06:33

Awesome, thanks! Both suggestions are helpful.


mrangel
14 May 2018, 07:36

If it's only used in one place, I'd make a one-line expression:
if (Instr("AEIOUaeiou", Left (your attribute here, 1)) > 0) {

or:
if (IsRegexMatch ("^[aeiou]", LCase(attribute/variable here)))) {

Whichever method you use, remember to either check for both lowercase and capital letters, or use LCase() on the attribute first.

If you want to check if the first letter is a vowel, even if there's a space or something before it, you'd use:
if (IsRegexMatch ("^[^a-z]*[aeiou]", LCase(attribute/variable here)))) {

I assume you're only accepting English words? If not, you might need to include things like ä, œ, ю, or ω in your list of vowels. There's a lot of them :S


jmnevil54
14 May 2018, 15:24

I believe CapFirst is a function, if I spelled that right....
Also, the opposite of vowel is constanent.


K.V.
14 May 2018, 16:01

You are correct, jmne. CapFirst is a function which capitalizes the first letter of a string.

...and, if a letter is not a vowel, it is a consonant. (Sometimes, Y goes both ways.)


JenniferCampbell
14 May 2018, 17:34

"Sometimes, Y goes both ways."

This explains a lot, actually.