Menu

[r1442]: / branches / v4-dev / Src / UStrUtils.pas  Maximize  Restore  History

Download this file

186 lines (149 with data), 5.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
{
* UStrUtils.pas
*
* String utility routines.
*
* Some of the routines are simply wrappers around SysUtils and StrUtils
* routines with the purpose of (a) collecting them together in one place and
* (b) providing a standardised parameter order.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is UStrUtils.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2011 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UStrUtils;
interface
/// <summary>Checks if string Haystack contains string Needle. Case sensitive.
/// </summary>
function StrContainsStr(const Needle, Haystack: string): Boolean;
/// <summary>Returns index of string Needle in string Haystack or 0 if Needle
/// is not in Haystack. Case sensitive.</summary>
function StrPos(const Needle, Haystack: string): Integer; overload;
/// <summary>Returns index of string Needle in string Haystack, beginning the
/// search at Offset. Returns 0 if Haystack is not found or if Offset is less
/// than 1 or greater than length of Haystack. Case sensitive.</summary>
function StrPos(const Needle, Haystack: string; const Offset: Integer): Integer;
overload;
/// <summary>Returns index of last occurence of string Needle in string
/// Haystack or 0 if Needle is not in Haystack. Case sensitive.</summary>
function StrLastPos(const Needle, Haystack: string): Integer;
/// <summary>Compares Left and Right strings, ignoring case. Returns 0 if both
/// strings are the same, -ve if Left is less than Right or +ve if Left is
/// greater than Right.</summary>
function StrCompareText(const Left, Right: string): Integer;
/// <summary>Converts a string to upper case.</summary>
function StrToUpper(const Str: string): string;
/// <summary>Converts a string to lower case.</summary>
function StrToLower(const Str: string): string;
/// <summary>Returns a substring of Str starting at index StartIdx of length
/// Count.</summary>
/// <remarks>
/// <para>If Count specifies more than the available number of characters, the
/// substring from StartIdx to the end of the string is returned.</para>
/// <para>If StartIdx is beyond the end of Str the empty string is returned.
/// </para>
/// </remarks>
function StrSlice(const Str: string; const StartIdx, Count: Integer): string;
/// <summary>Returns the leading characters of Str up to a length of Count
/// characters.</summary>
/// <remarks>If Count is greater than the length of Str the whole string is
/// returned.</remarks>
function StrSliceLeft(const Str: string; const Count: Integer): string;
/// <summary>Returns the trailing characters of Str up to a length of Count
/// characters.</summary>
/// <remarks>If Count is greater than the length of Str the whole string is
/// returned.</remarks>
function StrSliceRight(const Str: string; const Count: Integer): string;
/// <summary>Checks if string Str begins with sub string SubStr. Case
/// sensitive.</summary>
function StrStartsStr(const SubStr, Str: string): Boolean;
/// <summary>Checks if string Str begins with sub string SubStr. Case
/// insensitive.</summary>
function StrStartsText(const SubStr, Str: string): Boolean;
implementation
uses
// Delphi
SysUtils, StrUtils;
function StrCompareText(const Left, Right: string): Integer;
begin
Result := SysUtils.AnsiCompareText(Left, Right);
end;
function StrContainsStr(const Needle, Haystack: string): Boolean;
begin
Result := StrUtils.AnsiContainsStr(Haystack, Needle);
end;
function StrLastPos(const Needle, Haystack: string): Integer;
var
Idx: Integer; // an index of Needle in Haystack
begin
Result := 0;
Idx := StrPos(Needle, Haystack);
if Idx = 0 then
Exit;
while Idx > 0 do
begin
Result := Idx;
Idx := StrPos(Needle, Haystack, Idx + 1);
end;
end;
function StrPos(const Needle, Haystack: string): Integer;
begin
Result := SysUtils.AnsiPos(Needle, Haystack);
end;
function StrPos(const Needle, Haystack: string; const Offset: Integer): Integer;
begin
Result := StrUtils.PosEx(Needle, Haystack, Offset);
end;
function StrSlice(const Str: string; const StartIdx, Count: Integer): string;
begin
Result := StrUtils.AnsiMidStr(Str, StartIdx, Count);
end;
function StrSliceLeft(const Str: string; const Count: Integer): string;
begin
Result := StrUtils.AnsiLeftStr(Str, Count);
end;
function StrSliceRight(const Str: string; const Count: Integer): string;
begin
Result := StrUtils.AnsiRightStr(Str, Count);
end;
function StrStartsStr(const SubStr, Str: string): Boolean;
begin
Result := StrUtils.AnsiStartsStr(SubStr, Str);
end;
function StrStartsText(const SubStr, Str: string): Boolean;
begin
Result := StrUtils.AnsiStartsText(SubStr, Str);
end;
function StrToUpper(const Str: string): string;
begin
Result := SysUtils.AnsiUpperCase(Str);
end;
function StrToLower(const Str: string): string;
begin
Result := SysUtils.AnsiLowerCase(Str);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.