字符串函数大全
【字符串函数大全】首部function AnsiResemblesText(const AText, AOther: string): Boolean;$[StrUtils.pas功能返回两个字符串是否相似说明ANSI(American National Standards Institute)美国国家标准协会;不区分大小写参考function StrUtils.SoundexProc; var StrUtils.AnsiResemblesProc例子CheckBox1.Checked := AnsiResemblesText(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiContainsText(const AText, ASubText: string): Boolean;$[StrUtils.pas功能返回字符串AText是否包含子串ASubText说明不区分大小写参考function StrUtils.AnsiUppercase; function StrUtils.AnsiPos例子CheckBox1.Checked := AnsiContainsText(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiStartsText(const ASubText, AText: string): Boolean;$[StrUtils.pas功能返回字符串AText是否以子串ASubText开头说明不区分大小写参考function pareString例子CheckBox1.Checked := AnsiStartsText(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiEndsText(const ASubText, AText: string): Boolean;$[StrUtils.pas功能返回字符串AText是否以子串ASubText结尾说明不区分大小写参考function pareString例子CheckBox1.Checked := AnsiEndsText(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiReplaceText(const AText, AFromText, AToText: string):string; $[StrUtils.pas功能返回字符串AText中用子串AFromText替换成子串AToText的结果说明不区分大小写参考function SysUtils.StringReplace; type SysUtils.TReplaceFlags例子Edit4.Text := AnsiReplaceText(Edit1.Text, Edit2.Text, Edit3.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiMatchText(const AText: string; const AValues: array ofstring): Boolean; $[StrUtils.pas功能返回字符串数组AValues中是否包含字符串AText说明不区分大小写参考function StrUtils.AnsiIndexText例子CheckBox1.Checked := AnsiMatchText(Edit1.Text, [a1, a2, a3,a4]);━━━━━━━━━━━━━━━━━━━━━首部function AnsiIndexText(const AText: string; const AValues: array of string): Integer; $[StrUtils.pas功能返回字符串AText在字符串数组AValues中的位置说明不区分大小写;如果不包含则返回-1参考function SysUtils.AnsiSameText例子SpinEdit1.Value := AnsiIndexText(Edit1.Text, [a1, a2, a3, a4]);━━━━━━━━━━━━━━━━━━━━━首部function AnsiContainsStr(const AText, ASubText: string): Boolean; $[StrUtils.pas功能返回字符串AText是否包含子串ASubText说明区分大小写参考function StrUtils.AnsiPos例子CheckBox1.Checked := AnsiContainsStr(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiStartsStr(const ASubText, AText: string): Boolean; $[StrUtils.pas功能返回字符串AText是否以子串ASubText开头说明区分大小写参考function SysUtils.AnsiSameStr例子CheckBox1.Checked := AnsiStartsStr(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiEndsStr(const ASubText, AText: string): Boolean; $[StrUtils.pas功能返回字符串AText是否以子串ASubText结尾说明区分大小写参考function SysUtils.AnsiSameStr例子CheckBox1.Checked := AnsiEndsStr(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiReplaceStr(const AText, AFromText, AToText: string): string; $[StrUtils.pas功能返回字符串AText中用子串AFromText替换成子串AToText的结果说明区分大小写参考function SysUtils.StringReplace; type SysUtils.TReplaceFlags例子Edit4.Text := AnsiReplaceStr(Edit1.Text, Edit2.Text, Edit3.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiMatchStr(const AText: string; const AValues: array ofstring): Boolean; $[StrUtils.pas功能返回字符串数组AValues中是否包含字符串AText说明区分大小写参考function StrUtils.AnsiIndexStr例子CheckBox1.Checked := AnsiMatchStr(Edit1.Text, [a1, a2, a3,a4]);━━━━━━━━━━━━━━━━━━━━━首部function AnsiIndexStr(const AText: string; const AValues: array ofstring): Integer; $[StrUtils.pas功能返回字符串AText在字符串数组AValues中的位置说明区分大小写参考function SysUtils.AnsiSameStr例子SpinEdit1.Value := AnsiIndexStr(Edit1.Text, [a1, a2, a3, a4]);━━━━━━━━━━━━━━━━━━━━━首部function DupeString(const AText: string; ACount: Integer): string;$[StrUtils.pas功能返回字符串AText的ACount个复本说明当ACount为0时返回参考function System.SetLength例子Edit3.Text := DupeString(Edit1.Text, SpinEdit1.Value);━━━━━━━━━━━━━━━━━━━━━首部function ReverseString(const AText: string): string; $[StrUtils.pas功能返回字符串AText的反序说明ReverseString(1234) = 4321参考function System.SetLength例子Edit3.Text := ReverseString(Edit1.Text);━━━━━━━━━━━━━━━━━━━━━首部function StuffString(const AText: string; AStart, ALength: Cardinal;const ASubText: string): string; $[StrUtils.pas功能返回嵌套字符串说明AStart:嵌套开始位置;ALength:嵌套长度;StuffString(abcd, 2, 0, 12) = a12bcd 参考function System.Copy例子Edit3.Text := StuffString(Edit1.Text, SpinEdit1.Value, SpinEdit2.Value,Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function RandomFrom(const AValues: array of string): string; overload; $[StrUtils.pas功能随机返回字符串数组AValues中的一个元素说明之前建议执行Randomize参考function System.Random例子Randomize; Edit3.Text := RandomFrom([a1, a2, a3, a4]);━━━━━━━━━━━━━━━━━━━━━首部function IfThen(AValue: Boolean; const ATrue: string; AFalse: string = ): string; overload; $[StrUtils.pas功能返回指定的逻辑字符串说明IfThen(True, 是, 否) = 是;IfThen(False, 是, 否) = 否参考<NULL>例子Edit3.Text := IfThen(CheckBox1.Checked, Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function LeftStr(const AText: string; const ACount: Integer): string; $[StrUtils.pas功能返回字符串AText左边的ACount个字符说明LeftStr(123456, 3) = 123参考function System.Copy例子Edit3.Text := LeftStr(Edit1.Text, SpinEdit1.Value);━━━━━━━━━━━━━━━━━━━━━首部function RightStr(const AText: string; const ACount: Integer): string; $[StrUtils.pas功能返回字符串AText右边的ACount个字符说明RightStr(123456, 3) = 456参考function System.Copy例子Edit3.Text := RightStr(Edit1.Text, SpinEdit1.Value);━━━━━━━━━━━━━━━━━━━━━首部function MidStr(const AText: string; const AStart, ACount: Integer): string; $[StrUtils.pas功能返回字符串AText从AStart开始的ACount个字符说明其实就是Copy参考function System.Copy例子Edit3.Text := MidStr(Edit1.Text, SpinEdit1.Value, SpinEdit2.Value);━━━━━━━━━━━━━━━━━━━━━首部function SearchBuf(Buf: PChar; BufLen: Integer; SelStart, SelLength: Integer; SearchString: String; Options: TStringSearchOptions = [soDown]):PChar; $[StrUtils.pas功能返回第一个搜索到的指针位置说明这函数常用于文本中搜索字符串参考<NULL>例子///////Begin SearchBuffunction SearchEdit(EditControl: TCustomEdit; const SearchString: String; SearchOptions: TStringSearchOptions; FindFirst: Boolean = False): Boolean; varBuffer, P: PChar;Size: Word;beginResult := False;if (Length(SearchString) = 0) then Exit;Size := EditControl.GetTextLen;if (Size = 0) then Exit;Buffer := StrAlloc(Size + 1);tryEditControl.GetTextBuf(Buffer, Size + 1);P := SearchBuf(Buffer, Size, EditControl.SelStart, EditControl.SelLength, SearchString, SearchOptions);if P <> nil then beginEditControl.SelStart := P - Buffer;EditControl.SelLength := Length(SearchString);Result := True;end;finallyStrDispose(Buffer);end;end;procedure TForm1.Button1Click(Sender: TObject);varSearchOptions: TStringSearchOptions;beginSearchOptions := [];if CheckBox1.Checked thenInclude(SearchOptions, soDown);if CheckBox2.Checked thenInclude(SearchOptions, soMatchCase);if CheckBox3.Checked thenInclude(SearchOptions, soWholeWord);SearchEdit(Memo1, Edit1.Text, SearchOptions);Memo1.SetFocus;end;///////End SearchBuf━━━━━━━━━━━━━━━━━━━━━首部function Soundex(const AText: string; ALength: TSoundexLength = 4): string; $[StrUtils.pas功能返回探测字符串说明根据探测法(Soundex)可以找到相进的字符串;/genealogy/coding.html参考<NULL>例子Edit2.Text := Soundex(Edit1.Text, SpinEdit1.Value);━━━━━━━━━━━━━━━━━━━━━首部function SoundexInt(const AText: string; ALength: TSoundexIntLength =4): Integer; $[StrUtils.pas功能返回探测整数说明ALength的值越大解码准确率越高参考<NULL>例子SpinEdit2.Value := SoundexInt(Edit1.Text, SpinEdit1.Value);━━━━━━━━━━━━━━━━━━━━━首部function DecodeSoundexInt(AValue: Integer): string; $[StrUtils.pas功能返回探测整数的解码说明DecodeSoundexInt(SoundexInt(hello)) 相当于Soundex(hello)参考<NULL>例子Edit2.Text := DecodeSoundexInt(SpinEdit2.Value);━━━━━━━━━━━━━━━━━━━━━首部function SoundexWord(const AText: string): Word; $[StrUtils.pas功能返回探测文字数值说明没有参数ALength已经固定为4参考<NULL>例子SpinEdit2.Value := SoundexWord(Edit1.Text);━━━━━━━━━━━━━━━━━━━━━首部function DecodeSoundexWord(AValue: Word): string; $[StrUtils.pas功能返回探测文字数值的解码说明DecodeSoundexWord(SoundexWord(hello)) 相当于Soundex(hello)参考<NULL>例子Edit2.Text := DecodeSoundexWord(SpinEdit2.Value);━━━━━━━━━━━━━━━━━━━━━首部function SoundexSimilar(const AText, AOther: string; ALength: TSoundexLength = 4): Boolean; $[StrUtils.pas功能返回两个字符串的探测字符串是否相同说明Result := Soundex(AText, ALength) = Soundex(AOther, ALength)参考<NULL>例子CheckBox1.Checked := SoundexSimilar(Edit1.Text, Edit2.Text, SpinEdit1.Value);━━━━━━━━━━━━━━━━━━━━━首部function SoundexCompare(const AText, AOther: string; ALength: TSoundexLength = 4): Integer; $[StrUtils.pas功能返回比较两个字符串的探测字符串的结果说明Result := AnsiCompareStr(Soundex(AText, ALength), Soundex(AOther, ALength))参考function SysUtils.AnsiCompareStr例子SpinEdit2.Value := SoundexCompare(Edit1.Text, Edit2.Text,SpinEdit1.Value);━━━━━━━━━━━━━━━━━━━━━首部function SoundexProc(const AText, AOther: string): Boolean; $[StrUtils.pas功能调用SoundexSimilar返回两个字符串的探测字符串是否相同说明系统变量AnsiResemblesProc的默认值参考function StrUtils.AnsiResemblesText例子[var AnsiResemblesProc: TCompareTextProc = SoundexProc;]━━━━━━━━━━━━━━━━━━━━━首部function NewStr(const S: string): PString; deprecated; $[SysUtils.pas功能返回一个新的字符串指针地址说明字符串S为空时返回NullStr参考procedure System.New例子////////Begin NewStr,DisposeStrprocedure TForm1.Button1Click(Sender: TObject);varP: PString;beginP := NewStr(Edit1.Text);Edit2.Text := P^;DisposeStr(P);end;////////End NewStr,DisposeStr━━━━━━━━━━━━━━━━━━━━━首部procedure DisposeStr(P: PString); deprecated; $[SysUtils.pas功能释放字符串指针P资源说明配合函数NewStr使用参考procedure System.Dispose例子<如上参见,如下参见>━━━━━━━━━━━━━━━━━━━━━首部procedure AssignStr(var P: PString; const S: string); deprecated; $[SysUtils.pas功能将字符串S更新给字符串指针P说明更新值时会释放以前字符串指针的资源参考function SysUtils.NewStr;function SysUtils.DisposeStr例子////////Begin AssignStrprocedure TForm1.Button1Click(Sender: TObject);varP: PString;beginP := nil;AssignStr(P, Edit1.Text);Edit2.Text := P^;DisposeStr(P);end;////////End AssignStr━━━━━━━━━━━━━━━━━━━━━首部procedure AppendStr(var Dest: string; const S: string); deprecated; $[SysUtils.pas功能在字符串Dest后追加字符串S说明相当于Dest := Dest + S;Delphi6已经不建议使用参考<NULL>例子////////Begin AppendStrprocedure TForm1.Button1Click(Sender: TObject);varS: string;beginS := Edit2.Text;AppendStr(S, Edit1.Text);Edit2.Text := S;end;////////End AppendStr━━━━━━━━━━━━━━━━━━━━━首部function UpperCase(const S: string): string; $[SysUtils.pas功能返回字符串S的大写形式说明非小写字符不处理参考procedure System.SetLength例子Edit2.Text := UpperCase(Edit1.Text);━━━━━━━━━━━━━━━━━━━━━首部function LowerCase(const S: string): string; $[SysUtils.pas功能返回字符串S的小写形式说明非大写字符不处理参考procedure System.SetLength例子Edit2.Text := LowerCase(Edit1.Text);━━━━━━━━━━━━━━━━━━━━━首部function CompareStr(const S1, S2: string): Integer; $[SysUtils.pas功能返回比较两个字符说明当S1>S2返回值>0;当S1<S2返回值<0;当S1=S2返回值=0;区分大小写参考<NULL>例子SpinEdit1.Value := CompareStr(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function CompareMem(P1, P2: Pointer; Length: Integer): Boolean; assembler; $[SysUtils.pas功能返回比较两个内存指针说明CompareMem(PChar(12a), PChar(12c), 2)=True;CompareMem(PChar(12a), PChar(12c), 3)=False参考<NULL>例子CheckBox1.Checked := CompareMem(Self, Form1, 8);━━━━━━━━━━━━━━━━━━━━━首部function CompareText(const S1, S2: string): Integer; $[SysUtils.pas功能返回比较两个字符串说明不区分大小写参考<NULL>例子SpinEdit1.Value := CompareText(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function SameText(const S1, S2: string): Boolean; $[SysUtils.pas功能返回两个字符串是否相等说明不区分大小写参考<NULL>例子CheckBox1.Checked := SameText(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiUpperCase(const S: string): string; $[SysUtils.pas功能返回字符串S的大写形式说明ANSI(American National Standards Institute)美国国家标准协会;非小写的字符不变参考function Windows.CharUpperBuff例子Edit2.Text := AnsiUpperCase(Edit1.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiLowerCase(const S: string): string; $[SysUtils.pas功能返回字符串S的小写形式说明非大写字符不处理参考function Windows.CharLowerBuff例子Edit2.Text := AnsiLowerCase(Edit1.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiCompareStr(const S1, S2: string): Integer; $[SysUtils.pas功能反回比较两个字符串说明当S1>S2返回值>0;当S1<S2返回值<0;当S1=S2返回值=0;区分大小写参考function pareString例子SpinEdit1.Value := AnsiCompareStr(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiSameStr(const S1, S2: string): Boolean; $[SysUtils.pas功能返回两个字符串是否相等说明区分大小写参考function SysUtils.AnsiCompareStr例子CheckBox1.Checked := AnsiSameStr(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiCompareText(const S1, S2: string): Integer; $[SysUtils.pas功能反回比较两个字符串说明当S1>S2返回值>0;当S1<S2返回值<0;当S1=S2返回值=0;不区分大小写参考function pareString例子SpinEdit1.Value := AnsiCompareText(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiSameText(const S1, S2: string): Boolean; $[SysUtils.pas功能返回两个字符串是否相等说明不区分大小写参考function SysUtils.AnsiCompareText例子CheckBox1.Checked := AnsiSameText(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiStrComp(S1, S2: PChar): Integer; $[SysUtils.pas功能返回比较两个指针字符串说明当S1>S2返回值>0;当S1<S2返回值<0;当S1=S2返回值=0;区分大小写参考function pareString例子SpinEdit1.Value := AnsiStrComp(PChar(Edit1.Text), PChar(Edit2.Text)) ━━━━━━━━━━━━━━━━━━━━━首部function AnsiStrIComp(S1, S2: PChar): Integer; $[SysUtils.pas功能返回比较两个指针字符串说明当S1>S2返回值>0;当S1<S2返回值<0;当S1=S2返回值=0;不区分大小写;Ignore(忽略)参考function pareString例子SpinEdit1.Value := AnsiStrIComp(PChar(Edit1.Text), PChar(Edit2.Text)) ━━━━━━━━━━━━━━━━━━━━━首部function AnsiStrLComp(S1, S2: PChar; MaxLen: Cardinal): Integer;$[SysUtils.pas功能返回比较两个指针字符串指定长度说明当S1>S2返回值>0;当S1<S2返回值<0;当S1=S2返回值=0;区分大小写;Length(长度)参考function pareString例子SpinEdit1.Value := AnsiStrLComp(PChar(Edit1.Text), PChar(Edit2.Text), SpinEdit2.Value)━━━━━━━━━━━━━━━━━━━━━首部function AnsiStrLIComp(S1, S2: PChar; MaxLen: Cardinal): Integer;$[SysUtils.pas功能返回比较两个指针字符串指定长度说明当S1>S2返回值>0;当S1<S2返回值<0;当S1=S2返回值=0;不区分大小写参考function pareString例子SpinEdit1.Value := AnsiStrLIComp(PChar(Edit1.Text), PChar(Edit2.Text), SpinEdit2.Value)━━━━━━━━━━━━━━━━━━━━━首部function AnsiStrLower(Str: PChar): PChar; $[SysUtils.pas功能返回指针字符串小写形式说明非大写字符不处理参考function Windows.CharLower例子Edit2.Text := AnsiStrLower(PChar(Edit1.Text));━━━━━━━━━━━━━━━━━━━━━首部function AnsiStrUpper(Str: PChar): PChar; $[SysUtils.pas功能返回指针字符串大写形式说明非小写字符不处理参考function Windows.CharUpper例子Edit2.Text := AnsiStrUpper(PChar(Edit1.Text));━━━━━━━━━━━━━━━━━━━━━首部function AnsiLastChar(const S: string): PChar; $[SysUtils.pas功能返回字符串S的最后一个指针字符说明当字符串S为空串则返回空指针参考function SysUtils.ByteType例子Edit2.Text := AnsiLastChar(Edit1.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiStrLastChar(P: PChar): PChar; $[SysUtils.pas功能返回指针字符串P的最后一个指针字符说明当字符串P为空空指针则返回空指针参考function SysUtils.ByteType例子Edit2.Text := AnsiLastChar(PChar(Edit1.Text));━━━━━━━━━━━━━━━━━━━━━首部function WideUpperCase(const S: WideString): WideString; $[SysUtils.pas 功能返回双字节字符串的大写形式说明WideChar双字节字符参考function Windows.CharUpperBuffW例子Edit2.Text := WideUpperCase(Edit1.Text);━━━━━━━━━━━━━━━━━━━━━首部function WideLowerCase(const S: WideString): WideString; $[SysUtils.pas 功能返回双字节字符串的小写形式说明我怎么就测试不出来呢参考function Windows.CharLowerBuffW例子Edit2.Text := WideLowerCase(Edit1.Text);━━━━━━━━━━━━━━━━━━━━━首部function WideCompareStr(const S1, S2: WideString): Integer; $[SysUtils.pas功能返回比较两个双字节字符串说明当S1>S2返回值>0;当S1<S2返回值<0;当S1=S2返回值=0;区分大小写参考function pareStringW例子SpinEdit1.Value := WideCompareStr(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function WideSameStr(const S1, S2: WideString): Boolean; $[SysUtils.pas 功能返回两个双字节字符串是否相同说明区分大小写参考function SysUtils.WideCompareStr例子CheckBox1.Checked := WideSameStr(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function WideCompareText(const S1, S2: WideString): Integer;$[SysUtils.pas功能返回比较两个双字节字符串说明当S1>S2返回值>0;当S1<S2返回值<0;当S1=S2返回值=0;不区分大小写参考function pareStringW例子SpinEdit1.Value := WideCompareText(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function WideSameText(const S1, S2: WideString): Boolean; $[SysUtils.pas功能返回两个双字节字符串是否相同说明不区分大小写参考function SysUtils.WideCompareText例子CheckBox1.Checked := WideSameText(Edit1.Text, Edit2.Text);━━━━━━━━━━━━━━━━━━━━━首部function Trim(const S: string): string; overload; $[SysUtils.pas首部function Trim(const S: WideString): WideString; overload; $[SysUtils.pas功能返回除去字符串S左右不可见字符说明小于#32的字符看作不可见字符参考function System.Copy例子Edit2.Text := Trim(Edit1.Text);━━━━━━━━━━━━━━━━━━━━━首部function TrimLeft(const S: string): string; overload; $[SysUtils.pas首部function TrimLeft(const S: WideString): WideString; overload;$[SysUtils.pas功能返回除去字符串S左边不可见字符说明小于#32的字符看作不可见字符参考function System.Copy例子Edit2.Text := TrimLeft(Edit1.Text);━━━━━━━━━━━━━━━━━━━━━首部function TrimRight(const S: string): string; overload; $[SysUtils.pas 首部function TrimRight(const S: WideString): WideString; overload; $[SysUtils.pas功能返回除去字符串S右边不可见字符说明小于#32的字符看作不可见字符参考function System.Copy例子Edit2.Text := TrimRight(Edit1.Text);━━━━━━━━━━━━━━━━━━━━━首部function QuotedStr(const S: string): string; $[SysUtils.pas功能返回字符串S在pascal中的表现形式说明单引号中的一个单引号将转成两个参考procedure System.Insert例子Edit2.Text := QuotedStr(Edit1.Text);━━━━━━━━━━━━━━━━━━━━━首部function AnsiQuotedStr(const S: string; Quote: Char): string; $[SysUtils.pas功能返回字符串S以字符Quote为引号的表现形式说明AnsiQuotedStr(hello"world,@)=@hello"world@;AnsiQuotedStr(hello"world, ")="hello""world"参考function SysUtils.AnsiStrScan例子Edit2.Text := AnsiQuotedStr(Edit1.Text, ");━━━━━━━━━━━━━━━━━━━━━首部function AnsiExtractQuotedStr(var Src: PChar; Quote: Char): string; $[SysUtils.pas功能返回以字符Quote为引号的表现形式原形说明表现形式非法时Src不变否则为空参考function SysUtils.AnsiStrScan例子///////Begin AnsiExtractQuotedStrprocedure TForm1.Button1Click(Sender: TObject);varP: PChar;beginP := PChar(Edit1.Text);Edit2.Text := AnsiExtractQuotedStr(P, ");Edit3.Text := P;end;///////End AnsiExtractQuotedStr━━━━━━━━━━━━━━━━━━━━━首部function AnsiDequotedStr(const S: string; AQuote: Char): string;$[SysUtils.pas功能返回以字符AQuote为引号的表现形式原形说明表现形式非法时则返回S参考function SysUtils.AnsiExtractQuotedStr例子Edit2.Text := AnsiDequotedStr(Edit1.Text, ");━━━━━━━━━━━━━━━━━━━━━首部function AdjustLineBreaks(const S: string; Style: TTextLineBreakStyle ={$IFDEF LINUX} tlbsLF {$ENDIF} {$IFDEF MSWINDOWS} tlbsCRLF {$ENDIF}): string; $[SysUtils.pas功能返回将给定字符串的行分隔符调整为CR/LF序列说明AdjustLineBreaks(1#132#13)=1#13#102#13#10;AdjustLineBreaks(1#102#10)=1#13 #102#13#10参考function SysUtils.StrNextChar例子<NULL>━━━━━━━━━━━━━━━━━━━━━首部function IsValidIdent(const Ident: string): Boolean; $[SysUtils.pas功能返回字符串Ident是否是正确的标识符说明标识符::字母|下划线[字母|下划线|数字]...参考<NULL>例子CheckBox1.Checked := IsValidIdent(Edit1.Text);━━━━━━━━━━━━━━━━━━━━━首部function IntToStr(Value: Integer): string; overload; $[SysUtils.pas首部function IntToStr(Value: Int64): string; overload; $[SysUtils.pas功能返回整数Value转换成字符串说明Format(%d, [Value])参考function SysUtils.FmtStr例子Edit2.Text := IntToStr(SpinEdit1.Value);━━━━━━━━━━━━━━━━━━━━━首部function IntToHex(Value: Integer; Digits: Integer): string; overload;$[SysUtils.pas首部function IntToHex(Value: Int64; Digits: Integer): string; overload;$[SysUtils.pas功能返回整数Value转换成十六进制表现结果;Format(%.*x, [Digits, Value])说明参数Digits指定字符最小宽度;最小宽度不足时将用0填充参考function SysUtils.FmtStr例子Edit2.Text := IntToHex(SpinEdit1.Value, SpinEdit2.Value);━━━━━━━━━━━━━━━━━━━━━首部function StrToInt(const S: string): Integer; $[SysUtils.pas功能返回字符串S转换成整数说明字符串非整数表达时将引起异常参考procedure System.Val例子SpinEdit1.Value := StrToInt(Edit1.Text);━━━━━━━━━━━━━━━━━━━━━首部function StrToIntDef(const S: string; Default: Integer): Integer; $[SysUtils.pas功能返回字符串S转换成整数说明字符串非整数表达时则返回默认值Default参考procedure System.Val例子SpinEdit1.Value := StrToIntDef(Edit1.Text, 0);━━━━━━━━━━━━━━━━━━━━━首部function TryStrToInt(const S: string; out Value: Integer): Boolean; $[SysUtils.pas功能返回字符串S转换成整数Value是否成功说明字符串非整数表达时返回False并且Value将输出为0参考procedure System.Val例子///////Begin TryStrToIntprocedure TForm1.Button1Click(Sender: TObject);varI: Integer;beginCheckBox1.Checked := TryStrToInt(Edit1.Text, I);SpinEdit1.Value := I;end;///////End TryStrToInt━━━━━━━━━━━━━━━━━━━━━首部function StrToInt64(const S: string): Int64; $[SysUtils.pas功能返回字符串S转换成六十四位整数说明字符串非六十四位整数表达时将引起异常参考procedure System.Val例子SpinEdit1.Value := StrToInt64(Edit1.Text);━━━━━━━━━━━━━━━━━━━━━首部function StrToInt64Def(const S: string; const Default: Int64): Int64; $[SysUtils.pas功能返回字符串S转换成六十四位整数说明字符串非六十四位整数表达时则返回默认值Default参考procedure System.Val例子SpinEdit1.Value := StrToInt64Def(Edit1.Text, 0);━━━━━━━━━━━━━━━━━━━━━首部function TryStrToInt64(const S: string; out Value: Int64): Boolean; $[SysUtils.pas功能返回字符串S转换成六十四位整数Value是否成功说明字符串非六十四位整数表达时返回False并且Value将输出为0参考procedure System.Val例子///////Begin TryStrToInt64procedure TForm1.Button1Click(Sender: TObject);varI: Int64;beginCheckBox1.Checked := TryStrToInt64(Edit1.Text, I);SpinEdit1.Value := I;end;///////End TryStrToInt64━━━━━━━━━━━━━━━━━━━━━首部function StrToBool(const S: string): Boolean; $[SysUtils.pas功能返回字符串S转换成逻辑值说明字符非逻辑表达时将引起异常参考function SysUtils.TryStrToBool例子CheckBox1.Checked := StrToBool(Edit1.Text);━━━━━━━━━━━━━━━━━━━━━首部function StrToBoolDef(const S: string; const Default: Boolean): Boolean; $[SysUtils.pas功能返回字符串S转换成逻辑值说明字符非逻辑表达时则返回默认值Default参考function SysUtils.TryStrToBool例子CheckBox1.Checked := StrToBoolDef(Edit1.Text, False);━━━━━━━━━━━━━━━━━━━━━首部function TryStrToBool(const S: string; out Value: Boolean): Boolean;$[SysUtils.pas功能返回字符串S转换成逻辑值Value是否成功说明[注意]0为假非0为真;不是True和False;Delphi6 Bug 如下修正参考function SysUtils.AnsiSameText;var SysUtils.TrueBoolStrs;var SysUtils.FalseBoolStrs例子///////Begin TryStrToBoolprocedure TForm1.Button1Click(Sender: TObject);varB: Boolean;beginSetLength(TrueBoolStrs, 2);SetLength(FalseBoolStrs, 2);TrueBoolStrs[0] := True;FalseBoolStrs[0] := False;TrueBoolStrs[1] := Yes;FalseBoolStrs[1] := No;CheckBox1.Checked := TryStrToBool(Edit1.Text, B);CheckBox2.Checked := B;end;///////End TryStrToBool附加///////Begin TryStrToBoolfunction TryStrToBool(const S: string; out Value: Boolean): Boolean; function CompareWith(const aArray: array of string): Boolean;varI: Integer;beginResult := False;for I := Low(aArray) to High(aArray) doif AnsiSameText(S, aArray[I]) thenbeginResult := True;Break;end;end;varLResult: Extended;beginResult := TryStrToFloat(S, LResult);if Result thenValue := LResult <> 0elsebeginResult := True; //修正处VerifyBoolStrArray;if CompareWith(TrueBoolStrs) thenValue := Trueelse if CompareWith(FalseBoolStrs) thenValue := FalseelseResult := False;end;end;///////End TryStrToBool━━━━━━━━━━━━━━━━━━━━━首部function BoolToStr(B: Boolean; UseBoolStrs: Boolean = False): string; $[SysUtils.pas功能返回逻辑值B转换成字符串说明BoolToStr(False, False)=0;BoolToStr(False, True)=-1参考var SysUtils.TrueBoolStrs;var SysUtils.FalseBoolStrs例子Edit1.Text := BoolToStr(CheckBox1.Checked, CheckBox2.Checked); ━━━━━━━━━━━━━━━━━━━━━首部function LoadStr(Ident: Integer): string; $[SysUtils.pas功能返回根据标识Ident的字符串资源说明字符串资源是指程序的内部资源参考function SysUtils.FindStringResource例子Edit2.Text := LoadStr(StrToIntDef(Edit1.Text, 0));━━━━━━━━━━━━━━━━━━━━━首部function FmtLoadStr(Ident: Integer; const Args: array of const): string; $[SysUtils.pas功能返回格式化的字符串资源说明字符串资源是指程序的内部资源参考function SysUtils.FmtStr;function SysUtils.FindStringResource例子<NULL>;━━━━━━━━━━━━━━━━━━━━━首部function StrLen(const Str: PChar): Cardinal; $[SysUtils.pas功能返回指针字符串的长度说明当指针字符串Str为nil时将触发异常参考<NULL>例子SpinEdit2.Value := StrLen(PChar(Edit1.Text));━━━━━━━━━━━━━━━━━━━━━首部function StrEnd(const Str: PChar): PChar; $[SysUtils.pas功能返回指针字符串的结尾说明当指针字符串Str为nil时将触发异常参考<NULL>例子Edit2.Text := StrEnd(PChar(Edit1.Text)) - SpinEdit1.Value;━━━━━━━━━━━━━━━━━━━━━首部function StrMove(Dest: PChar; const Source: PChar; Count: Cardinal): PChar; $[SysUtils.pas功能返回将指针字符串Source指定内存数量Count复制覆盖到指针字符串Dest中说明Dest没有分配资源将触发异常s参考function System.Move例子///////Begin StrMoveprocedure TForm1.Button1Click(Sender: TObject);varvBuffer: PChar;beginvBuffer := 0123456789;StrMove(vBuffer, PChar(Edit1.Text), SpinEdit1.Value);Edit2.Text := vBuffer;end;///////End StrMove━━━━━━━━━━━━━━━━━━━━━首部function StrCopy(Dest: PChar; const Source: PChar): PChar; $[SysUtils.pas功能返回将指针字符串Source复制到指针字符串Dest中说明Dest应已经分配足够的空间非则将触发异常参考<NULL>例子///////Begin StrCopyprocedure TForm1.Button1Click(Sender: TObject);varvBuffer: PChar;beginGetMem(vBuffer, Length(Edit1.Text) + 1);StrCopy(vBuffer, PChar(Edit1.Text));Edit2.Text := vBuffer;FreeMem(vBuffer);end;///////End StrCopy━━━━━━━━━━━━━━━━━━━━━首部function StrECopy(Dest:PChar; const Source: PChar): PChar; $[SysUtils.pas功能返回将指针字符串Source复制到指针字符串Dest中的结尾说明可以连接指针字符串参考<NULL>例子///////Begin StrECopyprocedure TForm1.Button1Click(Sender: TObject);varvBuffer: array[0..255] of Char;beginStrECopy(StrECopy(vBuffer, PChar(Edit1.Text)), PChar(Edit2.Text));Edit3.Text := vBuffer;end;///////End StrECopy━━━━━━━━━━━━━━━━━━━━━首部function StrLCopy(Dest: PChar; const Source: PChar; MaxLen: Cardinal): PChar; $[SysUtils.pas功能返回将指针字符串Source指定长度MaxLen复制到指针字符串Dest中说明Dest应已经分配足够的空间非则将触发异常参考<NULL>例子///////Begin StrLCopyprocedure TForm1.Button1Click(Sender: TObject);varvBuffer: array[0..255] of Char;beginStrLCopy(vBuffer, PChar(Edit1.Text), SpinEdit1.Value);Edit2.Text := vBuffer;end;///////End StrLCopy━━━━━━━━━━━━━━━━━━━━━首部function StrPCopy(Dest: PChar; const Source: string): PChar;$[SysUtils.pas功能返回将指针字符串Source复制到指针字符串Dest中说明StrLCopy(Dest, PChar(Source), Length(Source))参考function SysUtils.StrLCopy例子///////Begin StrPCopyprocedure TForm1.Button1Click(Sender: TObject);varvBuffer: array[0..255] of Char;beginStrPCopy(vBuffer, PChar(Edit1.Text));Edit2.Text := vBuffer;end;///////End StrPCopy━━━━━━━━━━━━━━━━━━━━━首部function StrPLCopy(Dest: PChar; const Source: string; MaxLen: Cardinal): PChar; $[SysUtils.pas功能返回将字符串Source指定长度MaxLen复制到指针字符串Dest中说明StrLCopy(Dest, PChar(Source), MaxLen)参考function SysUtils.StrLCopy例子///////Begin StrPLCopyprocedure TForm1.Button1Click(Sender: TObject);varvBuffer: array[0..255] of Char;beginStrPLCopy(vBuffer, Edit1.Text, SpinEdit1.Value);Edit2.Text := vBuffer;end;///////End StrPLCopy━━━━━━━━━━━━━━━━━━━━━首部function StrCat(Dest: PChar; const Source: PChar): PChar; $[SysUtils.pas功能返回连接指针字符串Dest和指针字符串Source。
C语言字符串处理函数大全
不错不错.
long strtol( const char *nptr, char **endptr, int base );
long wcstol( const wchar_t *nptr, wchar_t **endptr, int base );
unsigned long strtoul( const char *nptr, char **endptr, int base );
void *memchr( const void *buf, int c, size_t count );
在内存中寻找字符c并返回其地址,如果没有找到,返回NULL
C语言中的字符串操作
char *strcat( char *strDestination, const char *strSource );
int strcmp( const char *string1, const char *string2 );
int wcscmp( const wchar_t *string1, const wchar_t *string2 );
int _mbscmp(const unsigned char *string1, const unsigned char *string2 );
在一个字符串里查找一个字串,返回不包含目标串的长度。注意,好像MSDN上说是第一个strCharSet中任意原子的地方。不知道这样的话还有什么意义。
size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr );
size_t wcsftime( wchar_t *strDest, size_t maxsize, const wchar_t *format, const struct tm *timeptr );
C#字符串函数大全
C#字符串函数大全C#字符串函数大全将包括Len Len(string|varname) 、Trim Trim(string) 、Ltrim Ltrim(string)等多项内容,希望本文能对大家有所帮助。
LenLen(string|varname)返回字符串内字符的数目,或是存储一变量所需的字节数。
TrimTrim(string)将字符串前后的空格去掉LtrimLtrim(string)将字符串前面的空格去掉RtrimRtrim(string)将字符串后面的空格去掉MidMid(string,start,length)从string字符串的start字符开始取得length长度的字符串,如果省略第三个参数表示从start字符开始到字符串结尾的字符串LeftLeft(string,length)从string字符串的左边取得length长度的字符串RightRight(string,length)从string字符串的右边取得length长度的字符串LCaseLCase(string)将string字符串里的所有大写字母转化为小写字母UCaseUCase(string)将string字符串里的所有大写字母转化为大写字母StrCompStrComp(string1,string2[,compare])返回string1字符串与string2字符串的比较结果,如果两个字符串相同,则返回0,如果小于则返回-1,如果大于则返回1 InStrInStr(string1,string2[,compare])返回string1字符串在string2字符串中第一次出现的位置SplitSplit(string1,delimiter[,count[,start]])将字符串根据delimiter拆分成一维数组,其中delimiter用于标识子字符串界限。
如果省略,使用空格("")作为分隔符。
string[] rq1=new string[3]; //此方法比较简洁(分割字符串)rq1 = rq.Split('-'); //单个字符作为分隔符// rq1 = rq.Split(new char[] {'-','-'}); 多个字符作为分隔符count返回的子字符串数目,-1指示返回所有子字符串。
C#字符串函数大全
C#字符串函数大全默认分类2010-06-29 21:58:24 阅读315 评论0 字号:大中小订阅C#字符串函数大全将包括Len Len(string|varname) 、TrimTrim(string) 、Ltrim Ltrim(string)等多项内容,希望本文能对大家有所帮助。
LenLen(string|varname)返回字符串内字符的数目,或是存储一变量所需的字节数。
TrimTrim(string)将字符串前后的空格去掉LtrimLtrim(string)将字符串前面的空格去掉RtrimRtrim(string)将字符串后面的空格去掉MidMid(string,start,length)从string字符串的start字符开始取得length 长度的字符串,如果省略第三个参数表示从start字符开始到字符串结尾的字符串LeftLeft(string,length)从string字符串的左边取得length长度的字符串RightRight(string,length)从string字符串的右边取得length长度的字符串LCaseLCase(string)将string字符串里的所有大写字母转化为小写字母UCaseUCase(string)将string字符串里的所有大写字母转化为大写字母StrCompStrComp(string1,string2[,compare])返回string1字符串与string2字符串的比较结果,如果两个字符串相同,则返回0,如果小于则返回-1,如果大于则返回1InStrInStr(string1,string2[,compare])返回string1字符串在string2字符串中第一次出现的位置SplitSplit(string1,delimiter[,count[,start]])将字符串根据delimiter拆分成一维数组,其中delimiter用于标识子字符串界限。
SQL Server字符串处理函数大全
SQL Server字符串处理函数大全可以在SELECT 语句的SELECT 和WHERE 子句以及表达式中使用字符串函数。
常用的字符串函数有:一、字符转换函数1、ASCII()返回字符表达式最左端字符的ASCII 码值。
在ASCII()函数中,纯数字的字符串可不用…‟括起来,但含其它字符的字符串必须用…‟括起来使用,否则会出错。
2、CHAR()将ASCII 码转换为字符。
如果没有输入0 ~ 255 之间的ASCII 码值,CHAR()返回NULL 。
3、LOWER()和UPPER()LOWER()将字符串全部转为小写;UPPER()将字符串全部转为大写。
4、STR()把数值型数据转换为字符型数据。
STR (<float_expression>[,length[,<decimal>]])length 指定返回的字符串的长度,decimal 指定返回的小数位数。
如果没有指定长度,缺省的length 值为10,decimal 缺省值为0。
当length 或者decimal 为负值时,返回NULL;当length 小于小数点左边(包括符号位)的位数时,返回length 个*;先服从length ,再取decimal ;当返回的字符串位数小于length ,左边补足空格。
二、去空格函数1、LTRIM() 把字符串头部的空格去掉。
2、RTRIM() 把字符串尾部的空格去掉。
三、取子串函数1、left()LEFT (<character_expression>,<integer_expression>)返回character_expression 左起integer_expression 个字符。
2、RIGHT()RIGHT (<character_expression>,<integer_expression>)返回character_expression 右起integer_expression 个字符。
C语言字符串函数大全
今天总结了下C语言字符串函数。
C语言字符串函数总结:1.字符处理库(ctype)中的函数2.stdio中的字符串和字符输入/输出的函数3.通用实用库stdlib中的字符串转换函数4.字符串处理库string中的字符串处理函数C语言的字符串实际上是存储单个字符的数组,结尾包含一个结束该字符串的特别的字符("空字符",用'\0'表示)。
char string1[]="first"实际上有6个元素。
char color="blue" char * p="blue"注意p[i]不能修改,若需修改应用字符数组。
一、.字符处理库(ctype)中的函数#include<ctype.h>函数原型:int f(int c)isdigit, isalpha, isalnum, isxdigit, islower, isupper, tolower, toupper, isspace,空白字符:新行符\n, 空格,回车''\r",水平制表符"\t", 垂直制表符"\v" isctrl, ispunct, isprint, isalpha二、stdio中的字符串和字符输入/输出的函数int getchar(void) 从标准输入设备读取字符以整数返回char * get(char * s) 从标准输入设备读入字符到数组s直到遇到新行符和文件结束符为止,然后再数组后追加NULL字符int putchar(int c) 打印字符int puts(const char * s) 打印字符串s和新行符int sprintf(char * s, const char * format) 与printf区别在于输出结果存放在s中int sscanf(char * s, const char * format); 与scanf区别在于从数组s读取数据示例1 字符串反转#include <stdio.h> void reverse(char *s) {if(s[0] == '\0') return;else{reverse(&s[1]); putchar(s[0]);}}int main(){char s[100];gets(s);reverse(s);return 0;}输入:sf输出:fs示例2 sscanf和sprintf #include<stdio.h>int main(){int x=1;double y=2.1;char s[100];sprintf(s,"Hello!%d, %f", x, y);puts(s);sscanf(s,"%d%f",&x,&y);printf("x:%d, y:%f", x, y);return 0;}输出:Hello!1, 2.100000x:1, y:2.100000三、stdlib中的字符串转换函数#include<stdlib.h>1. atoi(将字符串转换成整型数)定义函数int atoi(const char *nptr); 函数说明atoi()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时('\0')才结束转换,并将结果返回。
字符串输入函数
字符串输入函数
这里提供几种常用的字符串输入函数:
1. scanf函数:scanf函数可以用来输入字符串,格式为"%s"。
例如:
c
char str[20];
scanf("%s", str);
2. gets函数:gets函数可以读取一行字符串,但不推荐使用,因为它无法防止缓冲区溢出。
例如:
c
char str[20];
gets(str);
3. fgets函数:fgets函数可以读取一行字符串,且安全可靠,因为它可以指定缓冲区的大小。
例如:
c
char str[20];
fgets(str, 20, stdin);
4. getline函数:getline函数可以动态分配内存空间,以适应不同的字符串长度。
例如:
c
char* str = NULL;
size_t len = 0;
getline(&str, &len, stdin);
注意:使用getline函数时,需要手动释放分配的内存空间,否则会发生内存泄漏。
可以使用free函数释放内存空间:
c
free(str);。
字符串函数大全
函数名: stpcpy功能: 拷贝一个字符串到另一个用法: char *stpcpy(char *destin, char *source); 程序例:#include <stdio.h>#include <string.h>int main(void){char string[10];char *str1 = "abcdefghi";stpcpy(string, str1);printf("%s\n", string);return 0;}函数名: strcat功能: 字符串拼接函数用法: char *strcat(char *destin, char *source); 程序例:#include <string.h>#include <stdio.h>int main(void){char destination[25];char *blank = " ", *c = "C++", *Borland = "Borland"; strcpy(destination, Borland);strcat(destination, blank);strcat(destination, c);printf("%s\n", destination);return 0;}函数名: strchr功能: 在一个串中查找给定字符的第一个匹配之处\用法: char *strchr(char *str, char c);程序例:#include <string.h>#include <stdio.h>{char string[15];char *ptr, c = 'r';strcpy(string, "This is a string");ptr = strchr(string, c);if (ptr)printf("The character %c is at position: %d\n", c, ptr-string);elseprintf("The character was not found\n");return 0;}函数名: strcmp功能: 串比较用法: int strcmp(char *str1, char *str2);看Asic码,str1>str2,返回值 > 0;两串相等,返回0程序例:#include <string.h>#include <stdio.h>{char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";int ptr;ptr = strcmp(buf2, buf1);if (ptr > 0)printf("buffer 2 is greater than buffer 1\n");elseprintf("buffer 2 is less than buffer 1\n");ptr = strcmp(buf2, buf3);if (ptr > 0)printf("buffer 2 is greater than buffer 3\n");elseprintf("buffer 2 is less than buffer 3\n");return 0;}函数名: strncmpi功能: 将一个串中的一部分与另一个串比较, 不管大小写用法: int strncmpi(char *str1, char *str2, unsigned maxlen); 程序例:#include <string.h>#include <stdio.h>int main(void){char *buf1 = "BBB", *buf2 = "bbb";int ptr;ptr = strcmpi(buf2, buf1);if (ptr > 0)printf("buffer 2 is greater than buffer 1\n"); if (ptr < 0)printf("buffer 2 is less than buffer 1\n");if (ptr == 0)printf("buffer 2 equals buffer 1\n");return 0;}函数名: strcpy功能: 串拷贝用法: char *strcpy(char *str1, char *str2); 程序例:#include <stdio.h>#include <string.h>int main(void){char string[10];char *str1 = "abcdefghi";strcpy(string, str1);printf("%s\n", string);return 0;}函数名: strcspn功能: 在串中查找第一个给定字符集内容的段用法: int strcspn(char *str1, char *str2); 程序例:#include <stdio.h>#include <string.h>#include <alloc.h>int main(void){char *string1 = "1234567890";char *string2 = "747DC8";int length;length = strcspn(string1, string2);printf("Character where strings intersect is at position %d\n", length);return 0;}函数名: strdup功能: 将串拷贝到新建的位置处用法: char *strdup(char *str);程序例:#include <stdio.h>#include <string.h>#include <alloc.h>int main(void){char *dup_str, *string = "abcde";dup_str = strdup(string);printf("%s\n", dup_str);free(dup_str);return 0;}函数名: stricmp功能: 以大小写不敏感方式比较两个串用法: int stricmp(char *str1, char *str2);程序例:#include <string.h>#include <stdio.h>int main(void){char *buf1 = "BBB", *buf2 = "bbb";int ptr;ptr = stricmp(buf2, buf1);if (ptr > 0)printf("buffer 2 is greater than buffer 1\n");if (ptr < 0)printf("buffer 2 is less than buffer 1\n"); if (ptr == 0)printf("buffer 2 equals buffer 1\n"); return 0;}函数名: strerror功能: 返回指向错误信息字符串的指针用法: char *strerror(int errnum);程序例:#include <stdio.h>#include <errno.h>int main(void){char *buffer;buffer = strerror(errno);printf("Error: %s\n", buffer);return 0;}函数名: strcmpi功能: 将一个串与另一个比较, 不管大小写用法: int strcmpi(char *str1, char *str2);程序例:#include <string.h>#include <stdio.h>int main(void){char *buf1 = "BBB", *buf2 = "bbb";int ptr;ptr = strcmpi(buf2, buf1);if (ptr > 0)printf("buffer 2 is greater than buffer 1\n"); if (ptr < 0)printf("buffer 2 is less than buffer 1\n");if (ptr == 0)printf("buffer 2 equals buffer 1\n");return 0;}函数名: strncmp功能: 串比较用法: int strncmp(char *str1, char *str2, int maxlen);程序例:#include <string.h>#include <stdio.h>int main(void){char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc"; int ptr;ptr = strncmp(buf2,buf1,3);if (ptr > 0)printf("buffer 2 is greater than buffer 1\n");elseprintf("buffer 2 is less than buffer 1\n");ptr = strncmp(buf2,buf3,3);if (ptr > 0)printf("buffer 2 is greater than buffer 3\n");elseprintf("buffer 2 is less than buffer 3\n");return(0);}函数名: strncmpi功能: 把串中的一部分与另一串中的一部分比较, 不管大小写用法: int strncmpi(char *str1, char *str2);程序例:#include <string.h>#include <stdio.h>int main(void){char *buf1 = "BBBccc", *buf2 = "bbbccc";int ptr;ptr = strncmpi(buf2,buf1,3);if (ptr > 0)printf("buffer 2 is greater than buffer 1\n");if (ptr < 0)printf("buffer 2 is less than buffer 1\n");if (ptr == 0)printf("buffer 2 equals buffer 1\n");return 0;}函数名: strncpy功能: 串拷贝用法: char *strncpy(char *destin, char *source, int maxlen); 程序例:#include <stdio.h>#include <string.h>int main(void){char string[10];char *str1 = "abcdefghi";strncpy(string, str1, 3);string[3] = '\0';printf("%s\n", string);return 0;}函数名: strnicmp功能: 不注重大小写地比较两个串用法: int strnicmp(char *str1, char *str2, unsigned maxlen); 程序例:#include <string.h>#include <stdio.h>int main(void){char *buf1 = "BBBccc", *buf2 = "bbbccc";int ptr;ptr = strnicmp(buf2, buf1, 3);if (ptr > 0)printf("buffer 2 is greater than buffer 1\n");if (ptr < 0)printf("buffer 2 is less than buffer 1\n");if (ptr == 0)printf("buffer 2 equals buffer 1\n");return 0;}函数名: strnset功能: 将一个串中的所有字符都设为指定字符用法: char *strnset(char *str, char ch, unsigned n); 程序例:#include <stdio.h>#include <string.h>int main(void){char *string = "abcdefghijklmnopqrstuvwxyz";char letter = 'x';printf("string before strnset: %s\n", string);strnset(string, letter, 13);printf("string after strnset: %s\n", string);return 0;}函数名: strpbrk功能: 在串中查找给定字符集中的字符用法: char *strpbrk(char *str1, char *str2);程序例:#include <stdio.h>#include <string.h>int main(void){char *string1 = "abcdefghijklmnopqrstuvwxyz";char *string2 = "onm";char *ptr;ptr = strpbrk(string1, string2);if (ptr)printf("strpbrk found first character: %c\n", *ptr); elseprintf("strpbrk didn't find character in set\n"); return 0;}函数名: strrchr功能: 在串中查找指定字符的最后一个出现用法: char *strrchr(char *str, char c);程序例:#include <string.h>#include <stdio.h>int main(void){char string[15];char *ptr, c = 'r';strcpy(string, "This is a string");ptr = strrchr(string, c);if (ptr)printf("The character %c is at position: %d\n", c, ptr-string); elseprintf("The character was not found\n");return 0;}函数名: strrev功能: 串倒转用法: char *strrev(char *str);程序例:#include <string.h>#include <stdio.h>int main(void){char *forward = "string";printf("Before strrev(): %s\n", forward); strrev(forward);printf("After strrev(): %s\n", forward); return 0;}函数名: strset功能: 将一个串中的所有字符都设为指定字符用法: char *strset(char *str, char c);程序例:#include <stdio.h>#include <string.h>int main(void){char string[10] = "123456789";char symbol = 'c';printf("Before strset(): %s\n", string);strset(string, symbol);printf("After strset(): %s\n", string);return 0;}函数名: strspn功能: 在串中查找指定字符集的子集的第一次出现用法: int strspn(char *str1, char *str2);程序例:#include <stdio.h>#include <string.h>#include <alloc.h>int main(void){char *string1 = "1234567890";char *string2 = "123DC8";int length;length = strspn(string1, string2);printf("Character where strings differ is at position %d\n", length); return 0;}函数名: strstr功能: 在串中查找指定字符串的第一次出现用法: char *strstr(char *str1, char *str2);程序例:#include <stdio.h>#include <string.h>int main(void){char *str1 = "Borland International", *str2 = "nation", *ptr; ptr = strstr(str1, str2);printf("The substring is: %s\n", ptr);return 0;}函数名: strtod功能: 将字符串转换为double型值用法: double strtod(char *str, char **endptr);程序例:#include <stdio.h>#include <stdlib.h>int main(void){char input[80], *endptr;double value;printf("Enter a floating point number:");gets(input);value = strtod(input, &endptr);printf("The string is %s the number is %lf\n", input, value); return 0;}函数名: strtok功能: 查找由在第二个串中指定的分界符分隔开的单词用法: char *strtok(char *str1, char *str2);程序例:#include <string.h>#include <stdio.h>int main(void){char input[16] = "abc,d";char *p;/* strtok places a NULL terminatorin front of the token, if found */p = strtok(input, ",");if (p) printf("%s\n", p);/* A second call to strtok using a NULLas the first parameter returns a pointerto the character following the token */p = strtok(NULL, ",");if (p) printf("%s\n", p);return 0;}函数名: strtol功能: 将串转换为长整数用法: long strtol(char *str, char **endptr, int base); 程序例:#include <stdlib.h>#include <stdio.h>int main(void){char *string = "87654321", *endptr;long lnumber;/* strtol converts string to long integer */lnumber = strtol(string, &endptr, 10);printf("string = %s long = %ld\n", string, lnumber); return 0;}函数名: strupr功能: 将串中的小写字母转换为大写字母用法: char *strupr(char *str);程序例:#include <stdio.h>#include <string.h>int main(void){char *string = "abcdefghijklmnopqrstuvwxyz", *ptr;/* converts string to upper case characters */ptr = strupr(string);printf("%s\n", ptr);return 0;}函数名: swab功能: 交换字节用法: void swab (char *from, char *to, int nbytes); 程序例:#include <stdlib.h>#include <stdio.h>#include <string.h>char source[15] = "rFna koBlrna d";char target[15];int main(void){swab(source, target, strlen(source));printf("This is target: %s\n", target);return 0;}。
8种C语言基本常用的字符串处理函数
8种C语言基本常用的字符串处理函数8种C语言基本常用的字符串处理函数本文是店铺搜索整理的8种基本的常用的字符串处理函数,所有的C语言编译系统中一般都提供这些函数,以下是店铺为大家整理的8种C语言基本常用的字符串处理函数,仅供参考,希望能够帮助到大家。
1、puts函数——输出字符串的函数一般的形式为puts(字符串组)作用:将一个字符串输出到终端。
如,char一个string,并赋予初值。
调用puts(string);进行字符串的输出。
2、gets函数——输入字符串的函数一般的形式:gets(字符数组)作用:从终端输入一个字符串到字符数组,并且得到一个函数值成为字符数组的起始地址。
gets(str);键盘输入,,,,你懂得。
注意:puts和gets函数只能输出或者输入一个字符串。
3、strcat函数——字符串连接函数一般的形式:strcat(字符数组1,字符数组2);作用:把两个字符串数组中字符串连接起来,把字符串2连接到字符串1的后面。
说明:字符数组1必须足够大,以便容纳连接后的新字符串。
4、strcpy/strncpy函数——字符串复制函数一般形式:strcpy(字符数组1,字符串2);作用:将字符串2复制到字符数组1中去。
如:char str1[10],str2[]="DongTeng";strcpy(str1,str2);执行后的结果为:你懂得注意:1. 不能用赋值语句直接将一个字符串常量或者字符数组直接给一个字符数组。
2. 用strncpy可以赋值指定的位置的字符。
strncpy(str1,str2,3);将str2中的第3个字符复制到str1中。
5、strcmp函数——字符串比较函数一般形式:strcmp(字符串1,字符串2);作用:用来比较两个字符串的差异。
具有不同的比较规则。
6、strlen函数——测字符串长度的函数一般形式:strlen(字符数组);如:char str[10]="DongTeng";printf("%d",strlen(str));得到的结果是:57、strlwr函数——转换为小写的函数一般形式:strlwr(字符串);8、strupr函数——转换为大写的函数一般形式:strupr(字符串)。
(完整版)vb_字符串处理函数大全
mid(字符串,从第几个开始,长度)ByRef在[字符串]中[从第几个开始]取出[长度个字符串]例如mid("坦然面对",1,3) 则返回"坦然面"instr(从第几个开始,字符串1,字符串2)ByVal从规定的位置开始查找,返回字符串2在字符串1中的位置例如instr(1,"坦然面对","坦") 则返回1,instr(2,"坦然面对","坦"),则返回0 。
0 表示未找到InStrRev(字符串1,字符串2,从第几个开始) ByVal从规定的位置开始,从后住前查找,返回字符串2在字符串1中的位置,此处注意,虽是从后住前查找,但是返回的值还是从前往后算的。
例如instrRev("坦然面对","坦",2) 则返回2 ; instrRev("坦然面对","然",1) 则返回0 ,因为它从"坦然面对"的第1个字开始往前查找,所以找不到。
0 表示未找到left(字符串,长度) ByVal 从[字符串]的左边开始返回[长度]个字符例如Left("坦然面对",3) 则返回"坦然面"right(字符串,长度) ByVal 从[字符串]的右边开始返回[长度]个字符例如Right("坦然面对",3) 则返回"然面对"ucase(字符串) ByVal 返回[字符串]的大写形式,只对英文字符有效例如ucase("tanRANmiAnDui") 则返回"TANRANMIANDUI"lcase(字符串) ByVal 返回[字符串]的小写形式,只对英文字符有效例如lcase("tanRANmiAnDui") 则返回"tanranmiandui"asc(字符) Byval返回[字符]的ascii编码,若有多个字符,则只返回首字符的ascii编码,和Chr()函数是一个可逆的过程例如asc("坦") 则返回-13127; asc("坦然面对") 也返回-13127chr(ASCii编码) Byval 返回[Ascii]编码所代表的字符,和Chr()函数是一个可逆的过程例如chr(-13127) 则返回"坦" ;chr(asc("坦")) 则返回"坦"(这里是为了说明asc和chr的可逆性,例用此特性可以加密文本)trim(字符串) Byval 返回去掉了前、后之后的[字符串]例如trim("坦然面对") 则返回"坦然面对" ,中间的空格不受任何影响string(个数,字符) Byval 返回[个数]个[字符]例如string(3,"坦") 则返回"坦坦坦" , 而string(3,"坦然面对") 也返回"坦坦坦",只有首字符才有效space(个数) Byval 返回[个数]个空格例如space(5) 则返回""strconv(字符串,转换的类型) Byval 将字符串转成指定的类型。
C++中字符串常用的函数
C++中字符串常⽤的函数⼀、字符串函数strlen原型:extern int strlen(char *s);⽤法:#include <string.h> 功能:计算字符串s的长度说明:返回s的长度,不包括结束符NULL。
strcmp 原型:extern int strcmp(char *s1,char * s2);⽤法:#include <string.h> 功能:⽐较字符串s1和s2⼤⼩(ascii码值),区分⼤⼩写。
说明:当s1<s2时,返回值<0当s1=s2时,返回值=0当s1>s2时,返回值>0strcpy 原型:extern char *strcpy(char *dest,char *src);⽤法:#include <string.h>功能:把src所指由NULL结束的字符串复制到dest所指的数组中。
说明:src和dest所指内存区域不可以重叠且dest必须有⾜够的空间来容纳src的字符串。
返回指向dest的指针。
fabs(x) --------- 计算x的绝对值。
sqrt(x) --------- 计算x的开⽅。
isalpha(c) 检查字符c是否为字母。
是则函数返回值为1;不是则函数返回值为0。
alpha是单词“alphabet”的前5个字母。
isalnum(c) 检查字符c是否为字母或数字。
是则函数返回值为1;不是则函数返回值为0。
alnum是单词alphabet 和单词number 的缩写。
memset原型:extern void *memset(void *buffer, int c, int count);⽤法:#include <string.h>功能:把buffer所指内存区域的前count个字节设置成字符c。
说明:返回指向buffer的指针。
stricmp,strcmpi 原型:extern int stricmp(char *s1,char * s2);⽤法:#include <string.h>功能:⽐较字符串s1和s2,但不区分字母的⼤⼩写。
