字串的操作

2022-02-22 07:49:46 字數 2175 閱讀 6939

1、標準數字格式化字串

標準數字格式化字串      含義

c或c              貨幣

d或d              小數

e或e              指數

f或f               固定點

g或g              常規

n或n              數字

p或p              百分比

r或r              往返(確保將浮點轉換為字串,或將字串轉換為浮點)

x或x              十六進製制

2、自定義數字格式化字串

自定義數字格式化字串      含義

0                0佔位符

#                數字佔位符

.                 小數點

,                 千位分隔符

%                百分比佔位符

e/e +/- 0 (比如,e+0)      科學記數法

\                 轉義符

"xyz" 或 "xyz"            文字字串

;                 段分隔符

其他                按其顯示的文字

3、

1

//比較字串

2int

intresult;34

string

str1 ="

string 1";

5string

str2 ="

string 2";

67//使用compare方法8/*

str1 < str2 = 負數

9* str1 == str2 = 零

10* str1 > str2 = 正數

11*

*/12

intresult

=string

.compare(str1,str2);

13console.writeline(

"string.compare(,) = \n

",str1,str2,intresult);

1415

//使用compareordinal方法

16/*

str1 < str2 = 負數

17* str1 == str2 = 零

18* str1 > str2 = 正數

19*

*/20

intresult

=string

.compareordinal(str1,str2);

21console.writeline(

"string.compareordinal(,) = \n

",str1,str2,intresult);

2223

//使用compareto方法

24/*

this < string = 負數

25* this == string = 零

26* str1 > str2 = 正數

27* this is null = 1

28*

*/29

intresult

=str1.compareto(str2);

30console.writeline(

".compareto() = \n

",str1,str2,intresult);

1

//複製字串

2char

chararr

=new

char

[str1.length];

3str1.copyto(

0,chararr,

0,str1.length);

4console.writeline(

".copyto(0,chararr,0,str1.length) =

",str1);

5foreach

(char

character

inchararr) 6

",character);8}

9

字串操作 靠字串分割字串

字串分解函式。注意strtok比較複雜。要妥善運用!也可以不用strtok函式,但要實現字串靠字串分割比較困難!注意str指向的空間必須是可讀可寫的 如陣列或動態分配的空間 不能為字串常量的指標,因為strtok改變了其中的內容。include include 功能 將str中的字串按照elemon...

字串的操作

strcpy,sprintf,memcpy的區別 對於字串拷貝來說,其實現的效率和使用的方便程度不同 strcpy 無疑是最合適的選擇 效率高且呼叫方便。snprintf 要額外指定格式符並且進行格式轉化,麻煩且效率不高。memcpy 雖然高效,但是需要額外提供拷貝的記憶體長度這一引數,易錯且使用不...

字串的操作

pragma once define string h include include using namespace std define maxsize 255 typedef struct sstring void initstring sstring s 給字串賦值 void strassi...