delphi技術 常用的幾個字串處理函式

2021-04-01 15:57:31 字數 2459 閱讀 1072

unit strsub;

inte***ce

uses

windows, sysutils, strutils;

//獲取substr在str中左邊的部分字串,從左起搜尋

function getleftstr(substr, str: string): string;

//獲取substr在str中右邊的部分字串,從左起搜尋

function getrightstr(substr, str: string): string;

//獲取substr在str中左邊的部分字串,從右起搜尋

function getleftendstr(substr, str: string): string;

//獲取substr在str中右邊的部分字串,從右起搜尋

function getrightendstr(substr, str: string): string;

//取得在leftstr和rightstr中間的字串,從左起搜尋

function getstr(leftstr, rightstr, str: string): string;

//取得在leftstr和rightstr中間的字串,從右起搜尋

function getendstr(leftstr, rightstr, str: string): string;

implementation

function getleftstr(substr, str: string): string;

begin

result := copy(str, 1, pos(substr, str) - 1);

end;

//-------------------------------------------

function getrightstr(substr, str: string): string;

vari: integer;

begin

i := pos(substr, str);

if i > 0 then

result := copy(str

, i + length(substr)

, length(str) - i - length(substr) + 1)

else

result := '';

end;

//-------------------------------------------

function getleftendstr(substr, str: string): string;

vari: integer;

s: string;

begin

s := str;

result := '';

repeat

i := pos(substr, s);

if i > 0 then

begin

if result <> '' then

result := result + substr + getleftstr(substr, s)

else

result := getleftstr(substr, s);

s := getrightstr(substr, s);

end;

until i <= 0;

end;

//-------------------------------------------

function getrightendstr(substr, str: string): string;

vari: integer;

begin

result := str;

repeat

i := pos(substr, result);

if i > 0 then

begin

result := getrightstr(substr, result);

end;

until i <= 0;

end;

//-------------------------------------------

function getstr(leftstr, rightstr, str: string): string;

begin

result := getleftstr(rightstr, getrightstr(leftstr, str));

end;

//-------------------------------------------

function getendstr(leftstr, rightstr, str: string): string;

begin

result := getrightendstr(leftstr, getleftendstr(rightstr, str));

end;

end.

「不簡單的」刪除幾個字元

需要知道最少需要刪除幾個字元是的有連續四個字元是 tongji 輸入描述 多組資料 每組資料包含一個字串 1 n 100000 輸出描述 輸出一個整數表示最少需要刪除的字元數,若不存在則輸出 1 示例1輸入 tongji ijgnot ttoonnggjjii輸出0 14 include inclu...

strcpy memcpy幾個字串函式的完美版

strcpy函式 char strcpy char strdest,const char strsrc memcpy函式 void memcpy void pvto,void pvform,size t size assert pvto null pvfrom null assert pbto pv...

幾個字串相關的題目

今天整理了幾個老師講過的字串的題目 1.求一個字串中數字出現的個數和最大數 例如輸入ab12cd123ef1234 輸出結果 max 3,最大整數為1234 程式如下 include define max size 100 int main if count max str k 0 count 0 ...

幾個字串的處理函式

將字串中的小寫字母轉換為大寫 str 要轉換的字串 len 字串長度 void lowertocap u8 str,u8 len 對比字串str1和str2 str1 字串1指標 str2 字串2指標 返回值 0,相等 1,不相等 u8 usmart strcmp u8 str1,u8 str2 r...

幾個字串操作的彙編函式

pragma once namespace wch 求字串長度,等於位元組數 inline int strlen const char src 拷貝字串,注意源串與目標串不可重疊 declspec naked int stdcall strcpy const char src,char dst 連結...