Forum

> > CS2D > Scripts > Text in capital letters to lower case
Forums overviewCS2D overview Scripts overviewLog in to reply

English Text in capital letters to lower case

8 replies
To the start Previous 1 Next To the start

old Text in capital letters to lower case

GeoB99
Moderator Off Offline

Quote
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:
1
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")
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 cs2d lua cmd 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?

old Re: Text in capital letters to lower case

TimeQuesT
User Off Offline

Quote
Here we go!

1
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

Example usage >


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

old Re: Text in capital letters to lower case

THEMUD
User Off Offline

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

1
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

old Re: Text in capital letters to lower case

VADemon
User Off Offline

Quote
1
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
I thought user TimeQuesT's solution might not be very fast since it's using Lua to iterate through single characters, so I came up with gmatch matching upper-case letters. Should be faster.
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: @user THEMUD: Are you gonna forbid using any uppercase letters with string.find?

old Re: Text in capital letters to lower case

GeoB99
Moderator Off Offline

Quote
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.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview