Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Can I detect if a string has an "s" at the end of it?

Asked by 3 years ago

Could I use "string.find" for this?

Thank you :)

1
if string.find("Some Strings", "s$"); Where the dollar sign means that the match must be at the end imKirda 4491 — 3y

2 answers

Log in to vote
1
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

You can detect how much character is in the string by using string.len(). Not to be confused, let me make an example and how is the concept:

Like a string called "LOL" have 3 character, we can use string.len("LOL") and... It returns three! But what is the last letter at? It's at the 3rd column. Well it matches with the length! Also, we can use string.sub() to get a specific place from the start to the end. Well, you wanted to locate the part only at the last letter. So what are we gonna do is:

local String = "I like apples"
local String2 = "I like banana"

print(string.sub(String, string.len(String), string.len(String))) -- s
print(string.sub(String2, string.len(String2), string.len(String2))) -- a

-- Not to be confused, string with capital s is the variable, and string with a small letter s is a built-in function

If you understand what I mean in the top and understood how it works, you can skip this. And mark this as an answer if this helped.

Also if you are confused, let me explain again, like "LOL" have 3 characters, the end is between the 3rd and the 3rd, that's how we locate one string letter. and string.len() returns the string length and "LOL" have 3 character and the 3 is also the last letter (in column) of the string.

What the system sees are the string.sub() of the string, as known as "LOL", get the letter between 3rd to 3rd, and the 3 comes from length.

Also if you want to make it lowercase or uppercase, use string.lower(String) or string.upper(String)

Thank you for reading! Bye bye

0
Also if you see peoples doing string.sub(String, 5), it means getting the string from 5 to the last letter or etc. But I'd recommend you that way so you can change it to any specific subs places. Xapelize 2658 — 3y
0
Ohhhh, thank you CoolBlueJay000 48 — 3y
Ad
Log in to vote
0
Answered by
notfenv 171
3 years ago

Probably, but you can also use string.sub()

e.g.

local str = "Bruhlol MAIN"
print(string.sub(str, 9)

which returns MAIN

0
Just a minor you can also do string.sub(str,#str) notfenv 171 — 3y
0
Thank you! CoolBlueJay000 48 — 3y

Answer this question