Mega Code Archive

 
Categories / Delphi / Strings
 

Message String to SMS String Validation Function

Title: Message String to SMS String Validation Function Question: We have a central directory for SMS text messages that are sent out via a windows service. The directory contains free form message text files that are placed there by various applications and users. The problem we have is that some messages are too long, some have invalid characters (such as CrLf \ [ or ]) and some contain extra spaces. This function will convert a passed string to an optimised SMS string of a desired length (default = 160) with invalid chars replaced and extra blanks removed. The function uses certain elements of my "General.pas" black-box unit which I have also included. The code is easily adaptable to different locales and languages in the world. example: Using StrToSms('File in| \temp\error is [too big]'#13#10'New Line 3'); Results in 'File in: /temp/error is (too big) New Line 3' Answer: // =================================================== // Elements from General.pas required by function // =================================================== uses Math; const // Character Sets Constatnts // Edit SMS_CHARS to suit your needs // Don't need them all, just included for completeness ALL_CHARS = [#0..#255]; ANSI_CHARS = [#0..#127]; INVALIDFILE_CHARS = [#0..#31,#34,#42,#60,#62,#63,#124]; VALIDFILE_CHARS = ALL_CHARS - INVALIDFILE_CHARS; NUMERIC_CHARS = ['0'..'9']; UPPERCASE_CHARS = ['A'..'Z']; LOWERCASE_CHARS = ['a'..'z']; ALPHA_CHARS = UPPERCASE_CHARS + LOWERCASE_CHARS; ALPHANUMERIC_CHARS = ALPHA_CHARS + NUMERIC_CHARS; PROPERNAME_CHARS = ALPHANUMERIC_CHARS + [' ']; HEX_CHARS = NUMERIC_CHARS + ['A'..'F','a'..'f']; BINARY_CHARS = ['0','1']; WHITESPACE_CHARS = [#9..#13,' ']; PUNCTUATION_CHARS = ['!','"','''','(',')',',','.',';',':','?','[',']']; SIGN_CHARS = ['+','-']; CONTROL_CHARS = [#0..#31]; OPERATOR_CHARS = ['+','-','*','/','^']; BRACKET_CHARS = ['{','[','(',')',']','}']; VOWEL_CHARS = ['a','e','i','o','u','y','A','E','I','O','U','Y']; SMS_CHARS = PROPERNAME_CHARS + ['(',')','#','$','%','@',';'] + ['/','+','-','"','','*','&','=','_']; // ======================================================= // Replace multiple characters in a string with others // Like Oracle Translate() // ======================================================= procedure ReplaceChars(var AString : string; const ATheseChars,AWithChars : string; AIgnoreCase : boolean = false); overload; var i,ii,iLen : integer; begin iLen := Min(length(ATheseChars),length(AWithChars)); for i := 1 to iLen do begin for ii := 1 to length(AString) do begin if AIgnoreCase then begin if UpCase(AString[ii]) = UpCase(ATheseChars[i]) then AString[ii] := AWithChars[i]; end else if AString[ii] = ATheseChars[i] then AString[ii] := AWithChars[i]; end; end; end; // ========================================== // Remove duplicate spaces in a string // eg. // 'Hello World is lots space' // 'Hello World is lots space' // ========================================== function StripDupSpaces(const AString : string) : string; var sString : string; bIsSpace : boolean; i,iLen : integer; begin SetLength(sString,length(AString)); bIsSpace := false; iLen := 0; for i := 1 to length(AString) do begin if AString[i] = ' ' then begin if not bIsSpace then begin inc(iLen); sString[iLen] := AString[i]; end; bIsSpace := true; end else begin inc(iLen); bIsSpace := false; sString[iLen] := AString[i]; end; end; SetLength(sString,iLen); Result := sString; end; // =========================================================== // Main Function StrToSms() // Replace or remove invalid chars and duplicate spaces for // SMS message. Also truncate to 160 chars // =========================================================== function StrToSms(const AMsg : string; ALength : integer = 160) : string; var sResult : string; i : integer; begin sResult := AMsg; // Modify following for any char translation you require ReplaceChars(sResult,'[]{}\~|','()()/-:'); // Replace Invalid chars ith spaces for i := 1 to length(sResult) do if not (sResult[i] in SMS_CHARS) then sResult[i] := ' '; // Remove duplicate spaces sResult := StripDupSpaces(sResult); // Truncate message string to ALength Result := copy(sResult,1,160); end;