Txt-Case converter

From Vectorlab
Jump to: navigation, search


Function to convert a string to upper/lower case or title caps. By Orso B. Schmid


{ Orso *********************************************** }
{ convert to upper/lower case, title caps }
{ use 0 for title caps, 1 for lower case, 2 for upper case }
FUNCTION T_CaseConverter(str: STRING; capsIndx: INTEGER): STRING;
	VAR 
		cnt	: INTEGER;
		ChrArr	: DYNARRAY[] OF CHAR;
		c : CHAR;
		
	BEGIN
		IF Len(str) > 0 THEN BEGIN
			CASE capsIndx OF { case 1 }
			{ 0 title caps, 1 lower case }
			0..1:  
				BEGIN
					{ make an array of all chars of the string }
					ALLOCATE ChrArr[1..1]; { empties array }
					ALLOCATE ChrArr[1..Len(str)];
					ChrArr := str;
					
					CASE capsIndx OF { case 2 }
					{ title caps }
					0: 	FOR cnt := 1 TO Len(str) DO BEGIN
							c := ChrArr[cnt];
							UprString(c);
							ChrArr[cnt] := c;
							
							{ first letter of the string must be upper case, not the others }
							IF cnt > 1 THEN
									{ 
									45			-
									48..57		0-9
									65..90		A-Z
									95			_	
									97..122		a-z	32
									-
									192..207	À-Ï
									209..220	Ñ-Ü
									223			ß
									
									224..239	à-ï
									241..252	ñ-ü
									255			ÿ
									}
								
								{ if the previous letter is a letter or a number, 
								we are at the beginning of the word }
								CASE Ord(ChrArr[cnt - 1]) OF
									48..57, 65..90, 97..122, 192..207, 209..220, 223, 224..239, 241..252, 255 :
										{ everything to lower case }
										ChrArr[cnt] := T_LowerCase(Ord(c));
								END;
						END;
					
					{ lower case }
					1:	FOR cnt := 1 TO Len(str) DO 
							ChrArr[cnt] := T_LowerCase(Ord(ChrArr[cnt]));
					END; { case 2 }
					
					str := ChrArr;
				END; 
			
			{ uppercase }
			2:	UprString(str);
			END; { case 1 }
		END; { if }
		
		T_CaseConverter := str;
	END;