熟悉訓練之string基礎操作

2021-10-03 21:43:47 字數 2006 閱讀 3926

//string 專項練習

#include

#include

#include

using

namespace std;

intmain()

cout<

begin()

;it!=str.

end();

++it)

puts(""

);str.

insert

(str.

size()

," mia san mia");

//在字串長度下標處開始,插入新的字串

cout

erase(0

,12);

//從下標0處開始,刪除12個

cout

insert(0

,"champion ");

//在下標0處插入字串

cout

erase(8

);//從下標8處(含8)開始全刪除

cout

clear()

;//字串清空

cout

"hala madrid"

; string str1=

"to be"

; string str2=

"not to be"

; string str3=

"that is a question"

; string str4=str1+

",";

//"to be ,"

str4=str4+str2+

';';

//"to be , not to be; "

str4+

=str3;

cout

"abc"

; string str6=

"abd"

; string str7=

"bbc";if

(str5<=str6) cout<"<="

if(str5<=str7) cout<"<="

find

("madrid");

//在字串中查詢指定字元用find(),若找到返回起始對應下標,如找不到返回string::npos

cout

if(index!=string::npos) cout<" ";

index=str0.

find

('b');

if(index==string::npos) cout<<

"404 not found"

substr(3

,5);

//substr()返回字串的子串函式。從第3個開始往後數5個:3.4.5.6.7

cout

}/*hala madrid

11h a l a m a d r i d a

h a l a m a d r i d

hala madrid mia san mia

mia san mia

champion mia san mia

champion

to be,not to be;that is a question

abc<=abd

abc<=bbc

55 404 not found

a mad

process returned 0 (0x0) execution time : 3.121 s

press any key to continue.

*/

C 中string基礎操作

s.substr pos,n 擷取s中從pos開始 包括0 的n個字元的子串,並返回 s.substr pos 擷取s中從從pos開始 包括0 到末尾的所有字元的子串,並返回 s.replace pos,n,s1 用s1替換s中從pos開始 包括0 的n個字元的子串 s.find s1 查詢s中第一...

JAVA基礎練習之String

需求 練習string class stringdemo int beginindex 0,endindex str.length 1 while beginindex endindex str.charat beginindex if beginindex endindex while begin...

Java 基礎之String類

1.string類初始化方法 public class main string str3 new string chars string str4 new string chars,1,4 system.out.println str1 str1 system.out.println str2 st...