Text in capital letters to lower case
8 replies



13.02.16 06:31:40 pm
Was thinking around about making a simple script which converts all the text in capital letters to lower case one. string.lower will do the job as it is already explained there in Lua pil tutorial that it converts any type of arguments or sentences into lower case.
To begin with, here's the code:
As you can see, in 2 line the output function has two arguments. The ID of a player and the text defined to string.lower whenever a player says something but in caps though so it'll be converted. Although there's a problem with this script. CS2D triggered an error and Console printed that on 2 line, OutPutSayFunction attempted to call globally (a nil value). I already know why it does come up with this issue so you don't have to tell me that. The thing is, I don't have a clue what's the best trick to check for the output when a player types in capital letters. Was also rolling around with
msg as well and adding string.lower in it yet no results whatsoever.
I've seen also a thread where someone asked a script similar of this. Though the user who posted the code is pretty much similar in coding terms (outputsayfunction thingy).
Any thoughts about this? Is there a way somehow?
To begin with, here's the code:
Code:
1
2
3
4
5
6
2
3
4
5
6
function NoCaps(id, text)
OutPutSayFunction(id, text:lower())
msg2(id,string.char(169) .. "240128128Please refrain at using Caps-Lock while typing! All the text in capital letters are now converted to lower case.")
end
addhook("say","NoCaps")
OutPutSayFunction(id, text:lower())
msg2(id,string.char(169) .. "240128128Please refrain at using Caps-Lock while typing! All the text in capital letters are now converted to lower case.")
end
addhook("say","NoCaps")
As you can see, in 2 line the output function has two arguments. The ID of a player and the text defined to string.lower whenever a player says something but in caps though so it'll be converted. Although there's a problem with this script. CS2D triggered an error and Console printed that on 2 line, OutPutSayFunction attempted to call globally (a nil value). I already know why it does come up with this issue so you don't have to tell me that. The thing is, I don't have a clue what's the best trick to check for the output when a player types in capital letters. Was also rolling around with

I've seen also a thread where someone asked a script similar of this. Though the user who posted the code is pretty much similar in coding terms (outputsayfunction thingy).
Any thoughts about this? Is there a way somehow?
Yes. Whenever someone tries to type something but in capital letters it'll be converted in small / lower case.
Here we go!
(Be aware that special letters like space or brackets are included in the calculation. You can optimize this function by exclude them from the calculation. Simply count how many Arabic letters are in there.)
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
--Returns 'true' whenever the amount of found capital letters per cent is bigger or equal than the parameter percent
--range for percent is 0.0 to 1.0
--teh_string is the string to search through
function IsCapital(teh_string, percent)
local capital_found = 0.0;
for i = 1, string.len(teh_string) do
local bait = string.byte(teh_string, i);
if (bait >= 65 and bait <= 90) then --'A' to 'Z'
capital_found = capital_found + 1;
end
end
return ((capital_found / string.len(teh_string)) >= percent);
end
--range for percent is 0.0 to 1.0
--teh_string is the string to search through
function IsCapital(teh_string, percent)
local capital_found = 0.0;
for i = 1, string.len(teh_string) do
local bait = string.byte(teh_string, i);
if (bait >= 65 and bait <= 90) then --'A' to 'Z'
capital_found = capital_found + 1;
end
end
return ((capital_found / string.len(teh_string)) >= percent);
end
(Be aware that special letters like space or brackets are included in the calculation. You can optimize this function by exclude them from the calculation. Simply count how many Arabic letters are in there.)
Muh
Or you can use this code below, which searches and detects for the capital letters from the given start point.
As I put a variable to have the start's point value. 2 means it starts from the second letter in the text you are saying.
I know it's a bit basic and somehow buggy.
As I put a variable to have the start's point value. 2 means it starts from the second letter in the text you are saying.
I know it's a bit basic and somehow buggy.
Code:
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
start_point = 2;
addhook("say", "_say")
function _say(id, txt)
if string.find(txt, "%u", start_point) then
msg("Please don't use the Caps lock!");
return 1;
end
end
addhook("say", "_say")
function _say(id, txt)
if string.find(txt, "%u", start_point) then
msg("Please don't use the Caps lock!");
return 1;
end
end
I don't know what I'm doing with my life.
Code:
1
2
3
4
5
6
2
3
4
5
6
text = "HellO:D My FrieNd!"
local count = 0
for ucase in text:gmatch("%u") do
print(ucase)
count = count + 1
end
local count = 0
for ucase in text:gmatch("%u") do
print(ucase)
count = count + 1
end
I thought

Moreover, probably it has less potential bugs because it doesn't determine itself which letters are to be considered upper-case, instead Lua does.
UPD: @


Thanks for the quick answers so far. Tried some of scripts posted above and it work. I guess now I got the point somehow to check when a player types a text in upper case.



Muh



