c 字串string的操作

2022-06-06 23:36:10 字數 849 閱讀 3042

1 #include 2 #include 3 #include 45

using

namespace

std;67

intmain()879

else;82

83 cout << "

->方法2,string::npos可強制轉換為-1\n";

84int

local;

85 local = static_cast(s.find("

make

", 20

));86

87if(local != -1)

90else;93

94 cout << "

——————字串的常用函式————————

"<9596 cout << s1.erase(2,2) << endl << endl; //

【刪除】第2個位置的後2個字元,返回刪除後的結果

97 cout << s1.insert(2, "

st") << endl; //

在第2個位置【插入】字串「st」

尾部新新增的

") << endl; //

99 cout << s1.replace(5, 3, "

修改值") << endl; //

索引5開始的3個字元【替換】成"修改值"

100 cout << s1.substr(1, 4) << endl; //

【擷取】字串,s1[1:4]

101102

return0;

103 }

C 字串string操作

相比於c語言而言,c 提供了太多的寫好了的型別和方法,其中string型別就是用起來特別方便的一種。那麼問題來了,既然有c語言的char型,為什麼還要學習string型別呢?我碰到過的也是最主要的乙個原因就是string型別更節省空間,用多少開多少,而char型別的陣列就不是了,必須開最大值。其次還...

c 字串string的操作

1.字串的宣告 include include include using namespace std int main else cout 方法2,string npos可強制轉換為 1 n int local local static cast s.find make 20 if local 1...

String字串操作

char chars string s new string chars int len s.length 字串長度 system.out.println chars ab system.out.println s abc system.out.println len 3 char ch zhang...