String comparison: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 1,865: | Line 1,865: | ||
<syntaxhighlight lang="Delphi"> |
<syntaxhighlight lang="Delphi"> |
||
{String pack with control and extened chars} |
|||
const TestStr ='N'+#$01 +'o'+#$02 +'w'+#$03 +' '+#$04 +'i'+#$05 +'s'+#$06 +' '+#$07 +'t'+#$08 +'h'+#$09 +'e'+#$0A +' '+#$0B +'t'+#$0C +'i'+#$0D +'m'+#$0E +'e'+#$0F +' '+#$10 +'f'+#$11 +'o'+#$12 +'r'+#$13 +' '+#$14 +'a'+#$15 +'l'+#$16 +'l'+#$17 +' '+#$18 +'g'+#$19 +'o'+#$1A +'o'+#$1B +'d'+#$1C +' '+#$1D +'m'+#$1E +'e'+#$1F +'n'+#$80 +' '+#$81 +'t'+#$82 +'o'+#$83 +' '+#$84 +'c'+#$85 +'o'+#$86 +'m'+#$87 +'e'+#$88 +' '+#$89 +'t'+#$8A +'o'+#$8B +' '+#$8C +'t'+#$8D +'h'+#$8E +'e'+#$8F +' '+#$90 +'a'+#$91 +'i'+#$92 +'d'+#$93 +' '+#$94 +'o'+#$95 +'f'+#$96 +' '+#$97 +'t'+#$98 +'h'+#$99 +'e'+#$9A +' '+#$9B +'p'+#$9C +'a'+#$9D +'r'+#$9E +'t'+#$9F +'y'+#$A0; |
|||
procedure ShowCompares(Memo: TMemo; S1,S2: string); |
|||
{Strip control characters from string} |
|||
var I: integer; |
|||
begin |
begin |
||
if S1=S2 then Memo.Lines.Add(Format('"%s" is exactly equal to "%s"',[S1,S2])); |
|||
Result:=''; |
|||
if S1<>S2 then Memo.Lines.Add(Format('"%s" is not equal to "%s"',[S1,S2])); |
|||
for I:=1 to Length(S) do |
|||
if S1<S2 then Memo.Lines.Add(Format('"%s" is less than "%s"',[S1,S2])); |
|||
if byte(S[I])>=$20 then Result:=Result+S[I]; |
|||
if S1<=S2 then Memo.Lines.Add(Format('"%s" is less than or equal to "%s"',[S1,S2])); |
|||
end; |
|||
if S1>S2 then Memo.Lines.Add(Format('"%s" is greater than "%s"',[S1,S2])); |
|||
if S1>=S2 then Memo.Lines.Add(Format('"%s" is greater than or equal to "%s"',[S1,S2])); |
|||
function StripExtended(S: string): string; |
|||
if AnsiSameText(S1, S2) then Memo.Lines.Add(Format('"%s" is case insensitive equal to "%s"',[S1,S2])); |
|||
{Strip extended characters from string} |
|||
Memo.Lines.Add(Format('"%s" "%s" case sensitive different = %d',[S1,S2,AnsiCompareStr(S1,S2)])); |
|||
var I: integer; |
|||
Memo.Lines.Add(Format('"%s" "%s" case insensitive different = %d',[S1,S2,AnsiCompareText(S1,S2)])); |
|||
begin |
|||
Memo.Lines.Add(Format('"%s" is found at Index %d in "%s"',[S1,Pos(S1,S2),S2])); |
|||
Result:=''; |
|||
for I:=1 to Length(S) do |
|||
if byte(S[I])<$80 then Result:=Result+S[I]; |
|||
end; |
end; |
||
procedure |
procedure ShowStringCompares(Memo: TMemo); |
||
begin |
begin |
||
ShowCompares(Memo,'Equal', 'Equal'); |
|||
Memo.Lines.Add('String full of controls and extended chars: '); |
|||
ShowCompares(Memo,'Case', 'CASE'); |
|||
Memo.Lines.Add(TestStr); |
|||
ShowCompares(Memo,'91', '1234'); |
|||
Memo.Lines.Add('String stripped of controls chars: '); |
|||
ShowCompares(Memo,'boy', 'cowboy'); |
|||
Memo.Lines.Add(StripControls(TestStr)); |
|||
Memo.Lines.Add('String stripped of extended chars: '); |
|||
Memo.Lines.Add(StripExtended(TestStr)); |
|||
Memo.Lines.Add('String stripped of both control and extended chars: '); |
|||
Memo.Lines.Add(StripControls(StripExtended(TestStr))); |
|||
end; |
end; |
||
Line 1,904: | Line 1,894: | ||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
"Equal" is exactly equal to "Equal" |
|||
String full of controls and extended chars: |
|||
"Equal" is less than or equal to "Equal" |
|||
N�o�w� �i�s� �t�h e |
|||
"Equal" is greater than or equal to "Equal" |
|||
�t�i |
|||
"Equal" is case insensitive equal to "Equal" |
|||
m�e� �f�o�r� �a�l�l� �g�o�o�d� �m�e�n€ t‚oƒ „c…o†m‡eˆ ‰tŠo‹ ŒthŽe a‘i’d“ ”o•f– —t˜h™eš ›pœaržtŸy |
|||
"Equal" "Equal" case sensitive different = 0 |
|||
String stripped of controls chars: |
|||
"Equal" "Equal" case insensitive different = 0 |
|||
Now is the time for all good men€ t‚oƒ „c…o†m‡eˆ ‰tŠo‹ ŒthŽe a‘i’d“ ”o•f– —t˜h™eš ›pœaržtŸy |
|||
"Equal" is found at Index 1 in "Equal" |
|||
String stripped of extended chars: |
|||
"Case" is not equal to "CASE" |
|||
N�o�w� �i�s� �t�h e |
|||
"Case" is greater than "CASE" |
|||
�t�i |
|||
"Case" is greater than or equal to "CASE" |
|||
m�e� �f�o�r� �a�l�l� �g�o�o�d� �m�e�n to come to the aid of the party |
|||
"Case" is case insensitive equal to "CASE" |
|||
String stripped of both control and extended chars: |
|||
"Case" "CASE" case sensitive different = -1 |
|||
Now is the time for all good men to come to the aid of the party |
|||
"Case" "CASE" case insensitive different = 0 |
|||
⚫ | |||
"Case" is found at Index 0 in "CASE" |
|||
"91" is not equal to "1234" |
|||
"91" is greater than "1234" |
|||
"91" is greater than or equal to "1234" |
|||
"91" "1234" case sensitive different = 1 |
|||
"91" "1234" case insensitive different = 1 |
|||
"91" is found at Index 0 in "1234" |
|||
"boy" is not equal to "cowboy" |
|||
"boy" is less than "cowboy" |
|||
"boy" is less than or equal to "cowboy" |
|||
"boy" "cowboy" case sensitive different = -1 |
|||
"boy" "cowboy" case insensitive different = -1 |
|||
"boy" is found at Index 4 in "cowboy" |
|||
⚫ | |||
</pre> |
</pre> |
||
=={{header|Dyalect}}== |
=={{header|Dyalect}}== |