記得加入uses StrUtils
把數個字串加在一起
var str_data, str1, str2, str3: String;
str_data := str + str2 + ',' + str3;
str_data := concat(str1, str2, ',', str3);
檢查 str_data 第 i 位是否在 '0'..'9','a'..'z','A'..'Z','.' 之間
var str_data: String
if (str_data in ['0'..'9','a'..'z','A'..'Z','.']) then
把Str_data 從位置 str_begin 開始抽出長度 str_length 的 8位元文字放到 str_extract
var str_extract. str_data: String;
str_begin, str_length: Integer;
str_extract := Copy(str_data,str_begin,str_length);
在 str_data 指定位置插入 str_insert
var str_insert, str_data: String;
insert_point: integer;
insert (str_data, str_insert, insert_point);
在 str_data 前面補上指定 char (例如 '0') 令字串達到指定長度
for repeatno := 1 to 10-length(str_data) do
str_data := '0' + str_data ;
在 str_data 後面補上指定 char (例如 space) 令字串達到指定長度
for repeatno := 1 to 10-length(str_data) do
str_data := str_data + ' ' ;
在 string 中加入回車
string1 := string1 + #13 + #10;
在 str_data 指定位置移走指定數目的字符
var str_data: String;
delete_point, delete_length: integer;
delete(str_data, delete_point, delete_length);
找出 str_data 中 '.' 的位置
var s: integer;
s := Pos('.',str_data);
找出 str_data 中 'meeting' 的位置
var str_data : string;
i : integer;
str_data :='It is nice to meeting you';
i:=PosEx('meeting', str_data , 2); //由第二個字元開始找
//i=14;
把 str_data 中的前後空白位移走
var str_extract. str_data: String;
str_extrat := Trim(str_data);
把 str_data 中的所有指定的 char (例如 space) 移走
i := Pos(' ',str_data);
j := length(str_data );
while ((j>0) and (i>0)) do
begin
str_data := Copy(str_data ,1,i-1)+ Copy(str_data ,i+1,j);
i := Pos(' ',str_data );
j := length(str_data );
end;
檢查字串 dummyword 中是否含小數的數字, 把非數字移走
// replace non numeric and extra dot to space
firstdot := false;
for i := 1 to j do
begin
if not (dummyword in ['0'..'9','.']) then dummyword :=' '
else
if (dummyword = '.') then
if (not firstdot) then firstdot := true else dummyword := ' '
end;
// get rid of all space
i := Pos(' ',dummyword);
j := length(dummyword);
while ((j>0) and (i>0)) do
begin
dummyword := Copy(dummyword,1,i-1)+ Copy(dummyword,i+1,j);
i := Pos(' ',dummyword);
j := length(dummyword);
end;
抽出字串右面起指定數目字符
var str_data, str_extract:String;
str_length: integer;
str_extract := RightStr(str_data, str_length);
抽出字串左面起指定數目字符
var str_data, str_extract:String;
str_length: integer;
str_extract := LeftStr(str_data, str_length);
找出 str_data 的長度
var str_data: String;
str_length: Integer;
str_length := Length(str_data);
str_length := strlen(PAnsiChar(str_data);
找出 EditBox1 的文字長度
NumberOfChars = Length(EditBox1.Text);
把光標放在editbox 顯示文字之後
EditBox1.SetFocus;
EditBox1.SelStart:= length(editAPname.Text);
從句子 'word1 word2 word3 word4' 中把 word1, word2, word3, word4 每個獨立字抽出來處理 (以下例子是把文字讀出來)
procedure TfrmMain.findword(s:String);
var strhead, strtail, strpointer, strfulllength: integer;
strdummy:String;
begin
strdummy:=s+' ';
strpointer :=1;
strhead :=1;
strtail :=1;
strfulllength :=StrLen(PAnsiChar(strdummy));
While strpointer < (strfulllength+1) do
begin
if Copy(strdummy,strpointer,1) = ' ' then
begin
strtail := strpointer;
playsound(Copy(strdummy,strhead,strtail-strhead));
strhead := strpointer + 1;
end;
strpointer := strpointer +1;
end;
end;
把 str_data 中的英文全變為大寫, 放入 str_extract
var str_extract. str_data: String;
str_extrat := uppercase(str_data);
把 str_data 中的英文全變為小寫, 放入 str_extract
var str_extract. str_data: String;
str_extrat := lowercase(str_data);
把日期變為文字放入 str_data
var str_data: String;
Mydate: TDateTime;
Mydate:= EncodeDate(2010,10,25); //year, month, day
str_data := dateToStr(Mydate);
把時間變為文字放入 str_data
var str_data: String;
Mytime: TDateTime;
Mytime:= EncodeTime(15,33,35,3); //hour, min, sec, msec
str_data := TimeToStr(Mytime);
把文字變為整數
var str_data: String;
i: integer;
i := StrtoInt(str_data);
把HEX 文字變為整數
function TForm1.hexaToInt(s : string) : integer;
begin
if (s <> '') and (s[1] <> '$') then
result := strToInt('$' + s )
else
result := strToInt(s);
end;
把文字變為數字
var str_data: String;
i: real;
i := StrtoFloat(str_data);
把整數變為文字
var str_data: String;
i: integer;
str_data := InttoStr(i);
把數字變為文字
var str_data: String;
i: real;
str_data := FloattoStr(i);
把文字中部份內容更換
StringReplace(Str, 'Original','New', [rfReplaceAll] )
但不支持Unicode,可改用其它方法,例如
for i:=1 to Length(Str) do
if copy(Str,i,1) = '先' then replaceString := replaceString + '後'
以下的function 會稍後補充例子
AdjustLineBreaks: Returns the string with all line breaks converted to true CR/LF sequences.
CompareStr: Compares two strings.
CompareText: Compares two strings without case sensitivity.
DupeString: Returns a string repeated a specified number of times.
ExtractStrings: Fills a string list with substrings parsed from a delimited list.
IsDelimiter: Returns True if a specified character in a string matches one of a set of delimiters starting from some position.
LastDelimiter: Returns the index of the last occurence in a string of the characters cpecified.
QuotedStr: Returns the quoted version of a string.
ReverseString: Returns a string in which the character order of a specified string is reversed.
SetLenght: Changes the size of a dynamic array or a string.
SetString: Sets the lenghth and contents of a given string.
Str: Formats a string from an integer or floating point variable.
StringOfChar: Returns a string containing a repeating character string of the length specified.
TrimLeft: Returns a string containing a copy of a specified string without leading spaces and control characters.
TrimRight: Returns a string containing a copy of a specified string without trailing spaces and control characters.
Val: Converts a string to a numeric value.
WrapText: Returns a string broken into multiple lines. |