Wednesday, November 6, 2019
Delphi String Handling Routines
Delphi String Handling Routines The CompareTextà function compares two strings without case sensitivity. Declaration:functionà CompareText(constà S1, S2:à string):à integer; Description:Compares two strings without case sensitivity. The comparison is NOT case sensitive and does not consider the Windows locale settings. The return integer value is less than 0 if S1 is less than S2, 0 if S1 equals S2, or greater than 0 if S1 is greater than S2. This function is obsolete, i.e. it should not be used in new code - exists only for backward compatibility. Example: var s1,s2 : string; i : integer; s1:Delphi; s2:Programming; i: CompareText(s1,s2); //i Copy Function Returns a substring of a string or a segment of a dynamic array. Declaration:functionà Copy(S; Index, Count: Integer):à string;functionà Copy(S; Index, Count: Integer):à array; Description:Returns a substring of a string or a segment of a dynamic array.S is an expression of a string or dynamic-array type. Index and Count are integer-type expressions. Copy returns a string containing a specified number of characters from a string or sub array containing Count elements starting at S[Index]. If Index is greater than the length of S, Copy returns a zero-length string () or an empty array.à If Count specifies more characters or array elements than are available, only the characters or elements from S[Index] to the end of S are returned. To determine the number of characters in string, use the Length function. A convenient way to copy all the elements of S from the starting Index is to useà MaxIntà as Count. Example: var s : string; s:DELPHI; s : Copy(s,2,3); //sELP; Delete Procedure Removes a substring from a string. Declaration:procedureà Delete(varà S:à string; Index, Count : Integer) Description:Removes Count characters from a string S, starting at Index.à Delphi leaves the string unchanged if Index is not positive or greater than the number of characters after the Index. If Count is greater than the rest of the characters after the Index, the rest of the string is deleted. Example: var s : string; s:DELPHI; Delete(s,3,1) //sDEPHI; ExtractStrings Function Fills a string list with substrings parsed from a delimited list. Declaration:typeà TSysCharSet à set ofà Char;functionà ExtractStrings(Separators, WhiteSpace: TSysCharSet; Content: PChar; Strings: TStrings): Integer; Description:Fills a string list with substrings parsed from a delimited list. Separators are a set of characters that are used as delimiters, separating the substrings, where Carriage returns, newline characters, and quote characters (single or double) are always treated as separators. WhiteSpace is a set of characters to be ignored when parsing Content if they occur at the beginning of a string. Content is the null-terminated string to parse into substrings. Strings is a string list to which all substrings parsed from Content are added. The function returns the number of strings added to the Strings parameter. Example: //example 1 - requires TMemo named Memo1 ExtractStrings([;,,], [ ], about: delphi; pascal, programming , memo1.Lines); //would result in 3 strings added to memo: //about: delphi //pascal //programming //example 2 ExtractStrings([DateSeparator], [ ], PChar(DateToStr(Now)), memo1.Lines); //would result in 3 strings: day month and year of the currnet date //for example 06, 25 ,2003 LeftStr Function Returns a string containing a specified number of characters from the left side of a string. Declaration:functionà LeftStr(constà AString: AnsiString;à constà Count: Integer): AnsiString;overload;à functionà LeftStr(constà AString: WideString;à constà Count: Integer): WideString;à overload; Description:Returns a string containing a specified number of characters from the left side of a string. AString represents a string expression from which the leftmost characters are returned. Count indicates how many characters to return. If 0, a zero-length string () is returned. If greater than or equal to the number of characters in AString, the entire string is returned. Example: var s : string; s : ABOUT DELPHI PROGRAMMING; s : LeftStr(s,5); // s ABOUT Length Function Returns an integer containing the number of characters in a string or the number of elements in an array. Description:functionà Length(const S:à string): integerfunctionà Length(const S:à array): integer Declaration:Returns an integer containing the number of characters in a string or the number of elements in an array.à For an array, Length(S) always returns Ord(High(S))-Ord(Low(S))1 Example: var s : string; i : integer; s:DELPHI; i : Length(s); //i6; LowerCase Function Returns a string that has been converted to lowercase. Description:functionà LowerCase(constà S:à string):à string; Declaration:Returns a string that has been converted to lowercase.LowerCase only converts uppercase letters to lowercase; all lowercase letters and nonletter characters remain unchanged. Example: var s : string; s:DeLpHi; s : LowerCase(s); //sdelphi; Pos Function Returns an integer specifying the position of the first occurrence of one string within another. Declaration:functionà Pos(Str, Source:à string):à integer; Description:Returns an integer specifying the position of the first occurrence of one string within another. Pos looks for the first complete occurrence of Str in Source. If it finds one, it returns the character position in Source of the first character in Str as an integer value, otherwise, it returns 0.Pos is case sensitive. Example: var s : string; i : integer; s:DELPHI PROGRAMMING; i:Pos(HI PR,s); //i5; PosEx Function Returns an integer specifying the position of the first occurrence of one string within another, where the search starts at a specified position. Declaration:functionà PosEx(Str, Source :à string, StartFrom : cardinal 1):à integer; Description:Returns an integer specifying the position of the first occurrence of one string within another, where the search starts at a specified position. PosEx looks for the first complete occurrence of Str in Source, beginning the search at StartFrom. If it finds one, it returns the character position in Source of the first character in Str as an integer value, otherwise, it returns 0. PosEx also returns 0 if StartFrom is greater then Length(Source) or if StartPos is 0 Example: var s : string; i : integer; s:DELPHI PROGRAMMING; i:PosEx(HI PR, s, 4); //i1; QuotedStr Function Returns the quoted version of a string. Declaration:functionà QuotedStr(constà S:à string):à string; Description:Returns the quoted version of a string. A single quote character () is inserted at the beginning and end of string S, and each single quote character in the string is repeated. Example: var s : string; s:Delphis Pascal; //ShowMessage returns Delphis Pascal s : QuotedStr(s); //ShowMessage returns Delphis Pascal ReverseString Function Returns a string in which the character order of a specified string is reversed. Declaration:functionà ReverseString(constà AString :à string):à string; Description:à Returns a string in which the character order of a specified string is reversed Example: var s : string; s:ABOUT DELPHI PROGRAMMING; s:ReverseString(s); //sGNIMMARGORP IHPLED TUOBA RightStr Function Returns a string containing a specified number of characters from the right side of a string. Declaration:functionà RightStr(constà AString: AnsiString;à constà Count: Integer): AnsiString;overload;functionà RightStr(constà AString: WideString;à constà Count: Integer): WideString;overload; Description:Returns a string containing a specified number of characters from the right side of a string. AString represents a string expression from which the rightmost characters are returned. Count indicates how many characters to return. If greater than or equal to the number of characters in AString, the entire string is returned. Example: var s : string; s : ABOUT DELPHI PROGRAMMING; s : RightStr(s,5); // s MMING StringReplace Function Returns a string in which a specified substring has been replaced with another substring. Declaration:typeà TReplaceFlags à set ofà (rfReplaceAll, rfIgnoreCase); functionà StringReplace(constà S, OldStr, NewStr:à string; Flags: TReplaceFlags):à string; Description:Returns a string in which a specified substring has been replaced with another substring. If the Flags parameter does not include rfReplaceAll, only the first occurrence of OldStr in S is replaced. Otherwise, all instances of OldStr are replaced by NewStr.à If the Flags parameter includes rfIgnoreCase, the comparison operation is case insensitive. Example: var s : string; s:VB programmers love About VB Programming site; s : ReplaceStr(s,VB,Delphi, [rfReplaceAll]); //sDelphi programmers love About Delphi Programming site; Trim Function Returns a string containing a copy of a specified string without both leading and trailing spaces and control characters. Declaration:à functionà Trim(constà S:à string):à string; Description:à Returns a string containing a copy of a specified string without both leading and trailing spaces and non-printing control characters. Example: var s : string; s: Delphi ; s : Trim(s); //sDelphi; UpperCase Function Returns a string that has been converted to uppercase. Declaration:à functionà UpperCase(constà S:à string):à string; Description:à Returns a string that has been converted to uppercase.UpperCase only converts lowercase letters to uppercase; all uppercase letters and nonletter characters remain unchanged. Example: var s : string; s:DeLpHi; s : UpperCase(s); //sDELPHI; Val Procedure Converts a string to a numeric value. Declaration:à procedureà Val(constà S:à string;à varà Result;à varà Code: integer); Description:Converts a string to a numeric value. S is a string-type expression; it must be a sequence of characters that form a signed real number. The Result argument can be an Integer or floating-point variable. Code is zero if the conversion is successful. If the string is invalid, the index of the offending character is stored in Code. Val does not heed the local settings for the decimal separator. Example: var s : string; c,i : integer; s:1234; Val(s,i,c); //i1234; //c0
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.