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

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.


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


oops.. slow internet...


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!
}

Awesome, thanks! Both suggestions are helpful.


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


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


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.)


"Sometimes, Y goes both ways."

This explains a lot, actually.


Log in to post a reply.

Support

Forums