String manipulation routines

jbStr.Pas

Version 1.28

Adapted to Delphi 1.0 .. 6.0

Written by Jaro Benes

Copyright (c) 1991-2002 by Jaro Benes. All rights reserved.

Changes:

21.3.2002 (V1.28) Actualization of source and new functions added ().

9.7.2000 (V1.27) Fix some bugs and new function added (Alter, Reduce, JoinTo, ExtractNumber, ExtractAlphanum, StripChars and FindWord)

5.5.2000 (V1.26) Put out DLL management and make more natively

Please send comments to micrel@micrel.cz or see to my WEB pages http://www.micrel.cz/delphi  (sorry, still Czech only)

Note: When do you make modify my source code, please, send me your copy.

Features:

82 string manipulation non objected routines for generally using in Delphi 1.0 .. 6.0.

Tested on Delphi programs for versions Delphi 1,2,3,4,5.6.

Original function is Zip, Smash, Push, Insword, Hash, Mask, Count, Flop, Reduce, Alter, StripChars,

Strip, Change, Mult, Num, Doc, Turn, TestTo, Form and Trans.

Other function written for adapt old program modules to Delphi system or lost in Delphi

List of functions and short description

Declaration:

Function Alter(Str, AlterStr:String):String;

Description:

Alternative get of unempty string – when Str is empty then get AlterStr

Example:

Var S:String;

..

S := Alter('', 'John'); {α S contain 'John'}

Declaration:

Function AlterTo(Str, CondicStr, AlterStr:String):String;

Description:

Alternative get of unempty string – when CondicStr is equal Str then get AlterStr other Str is returned.

Example:

Var S:String;

S := ’Paolo’;

..

S := Alter(S, ’Paolo’, 'John'); {α S contain 'John' now}

Declaration:

Function Reduce(Const S:String, AboutSize:Integer):String;

Description:

Truncate string AboutSize characters to end of string.

Example:

Var S:String;

..

S := Reduce('John,Pablo,Ivan,', 1); {α S contain 'John,Pablo,Ivan'}

Declaration:

Function JointTo(PreStr, Delim, PostStr:String):String;

Description:

Confortable join words with delimitiers.

Example:

Var S:String;

..

S := JoinTo('John', ',', 'Ivan'); {α S contain 'John,Ivan'}

Declaration:

Function ExtractNumber(Const S:String):String;

Description:

Extract numbers only from string.

Example:

Var S:String;

..

S := ExtractNumber('2*5f8987'); {α S contain '258987'}

Declaration:

Function ExtractAlphanum(Const S:String):String;

Description:

Extract from string alphas and numbers only.

Example:

Var S:String;

..

S := ExtractAlphanum('5*69df5//6f9*'); {α S contain '569df56f9'}

Declaration:

Function StripChars(S:String;ch:CharSet):String;

Description:

Extract from string on special character set as mask only

Example:

Var S:String;

..

S := StripChars('1452a34c5c8946789abc',[ '1', '4', '8', 'c']); {α S contain '144cc848c'}

Declaration:

Function UpCase(CH:Char):Char;

Description:

Convert lower case character to upper with diacritics

Example:

Var C:Char;

..

C := UpCase('u'); {α C contain 'U'}

Declaration:

Function LoCase(CH:Char):Char;

Description:

Convert upper character to lower with diacritics

Example:

Var C:Char;

..

C := UpCase('U'); {α C contain 'u'}

Declaration:

Function StrLoCase(S:String):String;

Description:

Convert lower case characters in all string to upper with diacritics

Example:

Var S:String;

..

S := StrLoCase('HAHAHA'); {α S contain 'hahaha'}

Declaration:

Function StrUpCase(S:String):String;

Description:

Convert upper case characters in all string to lower with diacritics

Example:

Var S:String;

..

S := StrUpCase('hahaha'); {α S contain 'HAHAHA'}

Declaration:

Function StrUpCaseNoCs(S:String):String;

Description:

Standard up case characters string without Czech diacritics (for DB-field name)

Example:

Var S:String;

..

S := StrUpCase('hahaha'); {α S contain 'HAHAHA'}

Declaration:

Function CharStr(CH:Char;Len:Byte):String;

Description:

Make new string with specified characters

Example:

Var S:String;

..

S := CharStr('*',5); {α S contain '*****'}

Declaration:

Function StrStr(Const S:String;krat:Byte):String;

Description:

Similar as CharStr, input is string

Example:

Var S:String;

..

S := StrStr('*=',5); {α S contain '*=*=*=*=*='}

Declaration:

Function PadCh(S:String;CH:Char;Len:Byte):String;

Description:

Right padded string with specified character

Example:

Var S:String;

..

S := PadCh('CENA ','*',9); {α S contain 'CENA ****'}

Declaration:

Function Pad(Const S:String;Len:Byte):String;

Description:

Similar as PadCh, character is space

Example:

Var S:String;

..

S := Pad('CENA:',8); {α S contain 'CENA: '}

Declaration:

Function LeftPadCh(S:String;CH:Char;Len:Byte):String;

Description:

Left padded string with specified character

Example:

Var S:String;

..

S := LeftPadCh(' CENA','*',9); {α S contain '**** CENA'}

Declaration:

Function LeftPad(Const S:String;Len:Byte):String;

Description:

Similar as LeftPadCh, character is space

Example:

Var S:String;

..

S := LeftPadCh('CENA',8); {α S contain ' CENA'}

Declaration:

Function Hash(Const S:String):LongInt;

Description:

Simply checksum ordinal code in string

Example:

Var L:LongInt;

..

L := Hash('Ahoj kamarßde'); {α L contain 1303 in code Latin2}

Declaration:

Function Space(B:Byte):String;

Description:

Make space fill string;

Example:

Var S:String;

..

S := Space(4); {α S contain ' '}

Declaration:

Function MakeStr(Const S:String;B:Byte):String;

Description:

Function is alias for CharStr;

Example:

Var S:String;

..

S := MakeStr('*',5); {α S contain '*****'}

Declaration:

Function TrimLead(Const S:String):String;

Description:

Trim lead on string character with ordinal #0 to #32 (space)

Example:

Var S:String;

..

S := TrimLead(#1#2#0'AHOJ'); {α S contain 'AHOJ'}

Declaration:

Function TrimTrail(Const S:String):String;

Description:

Trim trail on string character with ordinal #0 to #32 (space)

Example:

Var S:String;

..

S := TrimTrail('AHOJ'+#8#2#9); {α S contain 'AHOJ'}

Declaration:

Function Trim(Const S:String):String;

Description:

Trim lead and trail on string character with ordinal #0 to #32 (space)

Example:

Var S:String;

..

S := Trim(' AHOJ'+#8#2#9); {α S contain 'AHOJ'}

Declaration:

Function ZeroClip(Const S:String):String;

Description:

Clip lead zero character

Example:

Var S:String;

..

S := ZeroClip('000023658'); {α S contain '23658'}

Declaration:

Function CapitalizeWord(Const S:String):String;

Description:

For each word in string up case first letter

Example:

Var S:String;

..

S := CapitalizeWord('jaro benes'); {α S contain 'Jaro Benes'}

Declaration:

Function CenterCh(Const S:String;CH:Char;Width:Byte):String;

Description:

String is centered in specified width and with character

Example:

Var S:String;

..

S := CenterCh('AHOJ','-',8); {α S contain '--AHOJ--'}

Declaration:

Function Center(Const S:String;Width:Byte):String;

Description:

Similar as CenterCh, with space

Example:

Var S:String;

..

S := Center('AHOJ',8); {α S contain ' AHOJ '}

Declaration:

Function WordCount(S:String;WordDelims:CharSet):Byte;

Description:

Count word on string and use specified delimits

Example:

Var W:Word;

..

W := WordCount('AHOJ CARLO',[' ']); {α W contain 2}

Note:

If delimits is ',' and string is ',,,,' then result is not correct

Declaration:

Function ExtractWord(N:Byte;S:String; WordDelims:CharSet):String;

Description:

Extract specified word from string

Example:

Var S:String;

..

S := ExtractWord(2,'AHOJ CARLO',[' ']); {α S contain 'CARLO'}

Declaration:

Function ChangeWord(N:Byte;Wrd,S:String; WordDelims:CharSet):String;

Description:

Exchange specified word in string to Wrd

Example:

Var S:String;

..

S := 'AHOJ CARLO';

S := ChangeWord(2,'CARLUCCI',S,[' ']); {α S contain 'AHOJ CARLUCCI'}

Declaration:

Function GetFirstWord(Const S:String;WordDelims:CharSet):String;

Description:

Get first word from string

Example:

Var S:String;

..

S := GetFirstWord('AHOJ CARLO MAY',[' ']); {α S contain 'AHOJ'}

Declaration:

Function GetLastWord(Const S:String;WordDelims:CharSet):String;

Description:

Get last word from string

Example:

Var S:String;

..

S := GetLastWord('AHOJ CARLO MAY',[' ']); {α S contain 'MAY'}

Declaration:

Function FindWord(What:String; Const S:String;WordDelims:CharSet):Boolean;

Description:

Find word in string, when is delimited

Example:

If FindWord('Archimedes','Cleo,Carlo,Denise,Paula,Archimedes',[',']) Then ... {α result is TRUE}

Declaration:

Procedure WordWrap(InSt:String;Var OutSt,Overlap:String; Margin:Byte;PadToMargin:Boolean);

Description:

Break long string for multi line formatting

Example:

Var R,S:String;

..

S := 'xxx xxx xxx xxx uuu uuu uuu uuu iii iii iii';

Repeat

WordWrap(S,R,S,15,False); {15 is width of current line}

.. {R hold current wrapped line}

Until Length(S)=0;

{R contain result:}

{1. line 'xxx xxx xxx xxx'}

{2. line 'uuu uuu uuu uuu'}

{3. line 'iii iii iii'}

Declaration:

Function PopWord(B:Byte;Var S:String;WordDelims:CharSet):String;

Description:

Cut specified word from string

Example:

Var R,S:String;

..

R := 'AHOJ CARLO MAY';

S := PopWord(2,R,[' ']); {α S contain 'CARLO' and R 'AHOJ MAY'}

Declaration:

Function GetPos(B:Byte;S:String;WordDelims:CharSet):Byte;

Description:

Result is first char position of specified word

Example:

Var W:Word;

..

W := GetPos(2,'AHOJ CARLO MAY',[' ']); {α W contain 6 char position of 'CARLO'}

Declaration:

Function GetEnd(B:Byte;S:String;WordDelims:CharSet):Byte;

Description:

Result is last char position of specified word

Example:

Var W:Word;

..

W := GetEnd(2,'AHOJ CARLO MAY',[' ']); {α W contain 10}

Declaration:

Function InsWord(iWord,cWord,cString:String):String;

Description:

Exchange group characters iWord to new group cWord on string cString

Example:

Var S:String;

..

S := InsWord('ee','oo','Lideep'); {α S contain 'Lidoop'}

Declaration:

Function Smash(C:Char;Const S:String):String;

Description:

Delete specified character from string

Example:

Var S:String;

..

S := Smash(' ',' 1 -B CU 182 '); {α S contain '1-BCU182'}

Declaration:

Function Mask(CharOfMask:Char;Const StrMask,Matrice:String; Var NextPosic:Byte):String;

Description:

Search multiple occurrence group characters base of mask

Restriction:

Length(StrMask)=Length(Matrice)

Example:

Const Maska='uuuu-xxxx=iiiii';

Var Next:Byte;

S,Matric:String;

..

Next := 1; {first positon}

Matric := 'JEDN-0038=17115';

{search 'x' in Maska, return substring from Matric and actualise

next position of behind returned substring}

S := Mask('x', Maska, Matric, Next); {α S contain '0038'}

Declaration:

Function Count(CH:Char;Const Dest:String;Var Posic,Len:Byte):Boolean;

Description:

Scanning Dest string if contain CH character and if exist return

position Posit and size Len this substring;

Example:

Var S:String;

Posic,Len:Byte;

..

S := 'xxxxxx x"zzzzzz"ccccc"zzzz"cc';

Posic:=0;{begin here}

While Count('"',S,Posic,Len) Do Begin

{on Posit and Len size is found character '"'}

AnyActionHere(S,Posic,Len); {change, delimiting etc.}

Count('z',S,Posic,Len); {Posic is 10 and Len is 6}

Inc(Posic,Len); { skip this and search next}

End;

Declaration:

Function Push(Posic:Byte;Const Source,Dest:String):String;

Description:

Paste to string other string

Example:

Var S:String;

..

S := Push(3,'UUU','xxxxxxxx'); {α S contain 'xxUUUxxx'}

..

S := Push(11,'III','xxxxxxxx'); {α S contain 'xxxxxxxx III'}

Declaration:

Procedure Flop(Var S1,S2:String);

Description:

Exchange two string;

Example:

Var R,S:String;

..

S := 'Carlo';

R := 'Dominik';

Flop(S,R); {α S contain 'Dominik' and R contain 'Carlo'}

Declaration:

Function Strip(Const Mask,Source:String):String;

Description:

Delete group character on base mask from string

Example:

Var Maska,S:String;

..

Maska := 'XX XXXXX XX ';

S := '21CU..... 2156 a ß';

S := Strip(Maska,S); {α S contain 'CU 2156 ß'}

Declaration:

Function Change(S:String;Source,Dest:Char):String;

Description:

Exchange character Source to Dest on S string

Example:

Var S:String;

..

S := Change('Kokorokokoko','o','u'); {α S contain 'Kukurukukuku'}

Declaration:

Function ChangeTo(S:String; Source:CharSet; Dest:Char):String;

Description:

Exchange some characters in Source to Dest character on S string

Example:

Var S:String;

..

S := Change('Koxkoxroxkoxkoxkox',['o',’x’] ,'u'); {α S contain 'Kuukuuruukuukuukuu'}

 

Declaration:

Function ChangeXChars(FindChar, DestChar:Char, Const Source:String):String;

Description:

Exchange all characters FindChar to DestChar on Source string. Primarily for tables with delimiters.

Example:

Var S:String;

..

S := 'KX     CX     DX';

S := Change(' ', ’;’, S,['o',’x’]); {α S contain 'FX;CX;DX', that’s all}

Declaration:

Function Zip(Const Mask,Source:String):String;

Description:

Flexible formatting any string on base mask

Example:

Var S:String;

..

S := Zip('XX+XX=XX','333366'); {α S contain '33+33=66'}

..

S := Zip('XX.XX.XX','240298'); {α S contain '24.02.98'}

..

S := Zip('XX-XX.XX.X:XX/X','980224Apoß'); {α S == '98-02.24.A:po/ß'}

Declaration:

Function Turn(Const S:String):String;

Description:

String turn for back formatting

Example:

Var S:String;

..

S := Turn('ABCD'); {α S contain 'DCBA'}

..

S := Turn(Zip('XXX,XXX',Turn('12285'))); {α S contain ' 12,285'}

Declaration:

Function EnTab(Const Sx:String;TabSize:Byte):String;

Description:

Pack space to tabelator character (#9)

Example:

Var S:String;

..

S := EnTab(' AHOJ',3); {α S contain #9#9'AHOJ'}

Declaration:

Function DeTab(Const Sx:String;TabSize:Byte):String;

Description:

Expand tabelator character (#9) to space

Example:

Var S:String;

..

S := DeTab(#9#9'AHOJ',3); {α S contain ' AHOJ'}

Declaration:

Function HasExtension(Const Name:String; Var DotPos:Word):Boolean;

Description:

File name orientation function, scanning extension

Example:

Var Posic:Word;

..

If HasExtension('MARK.TXT',Posic) Then …

Declaration:

Function DefaultExtension(Const Name, Ext:String):String;

Description:

If file name has not extension, then push default

Example:

Var S:String;

..

S := DefaultExtension('C:\POKUS\MARK','TXT'); {α S contain 'C:\POKUS\MARK.TXT'}

Declaration:

Function ForceExtension(Const Name, Ext:String):String;

Description:

Change old extension

Example:

Var S:String;

..

S := ForceExtension('C:\POKUS\MARK','DOC'); {α S contain 'C:\POKUS\MARK.DOC'}

Declaration:

Function JustExtension(Const PathName:String):String;

Description:

Return from filename the extension only

Example:

Var S:String;

..

S := JustExtension('C:\POKUS\MARK.DOC'); {α S contain 'DOC'}

Declaration:

Function JustFilename(Const PathName:String):String;

Description:

Return from filename the name and extension only

Example:

Var S:String;

..

S := JustFilename('C:\POKUS\MARK.DOC'); {α S contain 'MARK.DOC'}

Declaration:

Function JustPathname(Const PathName:String):String;

Description:

Return from filename the pathname only

Example:

Var S:String;

..

S := JustPathname('C:\POKUS\MARK.DOC'); {α S contain 'C:\POKUS'}

Declaration:

Function JustName(Const PathName:String):String;

Description:

Return from filename the name only

Example:

Var S:String;

..

S := JustName('C:\POKUS\MARK.DOC'); {α S contain 'MARK'}

Declaration:

Function AddLastChar(C:Char;Const DirName:String):String;

Description:

If possible then add character to end string

Example:

Var S:String;

..

S := AddLastChar('\','C:\POKUS'); {α S contain 'C:\POKUS\'}

Declaration:

Function RemLastChar(Const DirName:String):String;

Description:

Remove last character from string

Example:

Var S:String;

..

S := RemLastChar('C:\POKUS\'); {α S contain 'C:\POKUS'}

Declaration:

Function CleanDOSFileName(FileName:String):String;

Description:

Remove too many character from filename

Example:

Var S:String;

..

S := CleanDOSFileName('C:\POKUS\AAA.TXTXTXT'); {α S contain 'AAA.TXT'}

Declaration:

Function TestFileName(FName:String):Boolean;

Description:

Testing input string to restricted letter in DOS style; if name is empty (\.) then error occur

Example:

If Not TestFileName('C:/POKUS/') Then ErrorOccurred;

Declaration:

Function ShortDirName (Len:Byte; Const PName:String):String ;

Description:

For shorting view directory name, path name only

Example:

Var S:String;

..

S := ShortDirName(18,'C:\POKUSY\PRACOV\ZADAN\NOVE\POSL\'); {α S contain 'C:\..\NOVE\POSL\'}

Declaration:

Function ShortFileName(Len:Byte;Const FName:String):String;

Description:

Similar as ShortDirName with file name

Example:

Var S:String;

..

S := ShortFileName(25,'C:\POKUSY\PRACOV\ZADAN\NOVE\POSL\MOJE.TXT');

{α S contain 'C:\..\NOVE\POSL\MOJE.TXT'}

Declaration:

Function Mult(Const S:String):Byte;

Description:

Scan multiple occurrence first character of string

Example:

Var W:Word;

..

W := Mult('AARON'); {α W contain 2}

Declaration:

Function Num(Const S:String;Base:Byte):LongInt;

Description:

Conversion string number on base 2..36 into long integer

Example:

Var L:LongInt

..

L := Num('FFFF',16); {α L contain 65535}

Declaration:

Function Doc(L:LongInt;Const Base:Byte):String;

Description:

Conversion number from long integer to string on base 2..36

Example:

Var S:String;

..

S := Doc(65535,16); {α S contain 'FFFF'}

Declaration:

Function PackNum(Const S:String):String;

Description:

Very simply bcd pack, accept '0'..'9' only,

if length is odd then padded $F

Example:

Var S:String;

..

S := PackNum('12345'); {α S contain #1'#E'}

Declaration:

Function UnpackNum(Const S:String):String;

Description:

Very simply bcd unpack

Example:

Var S:String;

..

S := UnPackNum(#1'#E'); {α S contain '12345'}

Declaration:

Function Str3Long(Const S:String):LongInt;

Description:

Reading and conversion to first non number character without control

Example:

Var L:LongInt;

..

L := Str3Long('12345aaaa'); {α L contain 12345}

Declaration:

Function Str2Long(Const S:String; Var I:LongInt):Boolean;

Description:

Convert long integer string to long integer number

Example:

Var L:LongInt;

..

If Not Str2Long('12345',L) Then ErrorOccurred;

Declaration:

Function Str2Word(Const S:String; Var I:Word):Boolean;

Description:

Convert word string to word number

Example:

Var W:Word;

..

If Not Str2Word('12345',W) Then ErrorOccurred;

Declaration:

Function Str2Int(Const S:String; Var I:Integer):Boolean;

Description:

Convert integer string to integer number

Example:

Var I:Integer;

..

If Not Str2Int('-12345',I) Then ErrorOccurred;

Declaration:

Function Str2Real(Const S:String; Var R:Real):Boolean;

Description:

Convert real string to real number

Example:

Var R:Real;

..

If Not Str2Real('-12345.05',R) Then ErrorOccurred;

Declaration:

Function Long2Str(L:LongInt):String;

Description:

Convert any integer to string

Example:

Var S:String;

..

S := Long2Str(12345); {α S contain '12345'}

Declaration:

Function Real2Str(R:Real; Width, Places:Byte):String;

Description:

Convert real number to string, width is size all string and places is from this size

Example:

Var S:String;

..

S := Real2Str(0.12589,5,2); {α S contain ' 0.14'}

Declaration:

Function Form(Const Mask:String; R:Real):String;

Description:

Simply formatting of real number to string

Example:

Var S:String;

..

S := Form('XXX.XX',0.13589); {α S contain ' 0.14'}

..

S := Form('X.X',55.256); {α overflow, S contain '*.*'}

Declaration:

Const

swKamenic = 0; {Czech code as brothers Kamenicky}

swWin31CE = 1; {Czech code Windows 3.1 CE}

swWin1250 = 2; {Czech code Windows page 1250}

swECMA = 3; {code ECMA ansi}

swLatin2 = 4; {code Latin 2}

swUsaAnsi = 5; {code as VGA generator}

swIbm = 6; {code without diacritics}

swSemigraph = 7;{as Ibm without graphics characters}

swMacIntosh = 8;{code for Mac}

Function Trans(St:String;odkud,kampak:Byte):String;

Description:

String will be transporting from odkud code to kampak code,

this oriented to Czech language and not support in the table other

but table is public

Example:

S := Trans(S, swKamenic, swWin1250);{result contain text in new code}

Declaration:

Function Roman2Int(Const S: String): LongInt;

Description:

Assume function, convert romanum number to long integer

Example:

Var L:LongInt;

..

L := Roman2Num('VII');{α L contain 7}

Declaration:

Function Int2Roman(Value: LongInt): String;

Description:

Assume function, convert long integer to romanum number

Example:

Var S:String;

..

S := Int2Roman(9);{α S contain 'IX'}

Declaration:

Function htmlSrcEmail(S:String): String;

Description:

Search first E-mail address in input string, for more break input string post result.

Example:

Var S:String;

S := '<A HREF="mailto:micrel@micrel.cz"><IMG BORDER=0 SRC="email.gif"></A>'

S := htmlSrcEmail(S);{α S contain 'micrel@micrel.cz'}

Declaration:

Function YesOrNo(B:Boolean): String;

Description:

Return string representation for true or false declared as ccYesString and ccNoString constant.

Example:

Var S:String;

S := YesOrNo(true);{α S contain '1'}

Function YesOrNoEx(B:Boolean; Const strYes, strNo:String): String;

Description:

Return string representation for true or false declared in parameters strYes and strNo.

Example:

Var S:String;

S := YesOrNoEx(true, ’Its OK’, ’Its poor’);{α S contain 'Its OK'}

Function TestTo(S:String; Sarr:Array of String): Boolean;

Description:

Return true is S string equal one from array of string. This is for simply equal test.

Example:

Var S:String;

If NOT TestTo(S, [’Carlo’, ’Irene’]) Then S := ’Peter’;{α if S isn’t Carlo or Irene string put into Peter string}

Function TestBeginTo(S:String; Sarr:Array of String): Boolean;

Description:

Return true is S string beginning one from array of string. This is for simply equal test.

Example:

Var S:String;

If TestBeginTo(S, [’001217’, ’001319’]) Then ShowMessage(’Telephone number to U.S.A.’);

Function PosN(Substring, Mainstring:string; occurrence:integer):integer

Description:

Function PosN get recursive - the "occurrence" the position of "Substring" in "Mainstring". Does the Mainstring not contain Substring the result is 0. Works with chars and strings. (NOTE: This function is from Swiss Delphi Center WEB Tips and Tricks).

Example:

Var I:Integer;

I := PosN('s','swissdelphicenter.ch',2 {second occurence}); {I variable contain 4 as position}

Declaration:

Type

CharSet=Set of Char;

Const

MaskZipChar:Char = 'X';

ccYesString = ‘1’;

ccNoString = ‘0’;

Description:

MaskZipChar is mask character for Strip, Zip and Form function and possible to change

End of file

{last change 13.V.2002}