字串複習

2022-08-01 07:33:14 字數 1667 閱讀 5606

package

com.hanqi;

public

class

bird1

;

string str2=new

string(a);

system.out.println("str2=" +str2);

//2.字串比較

//==運算,比較的是記憶體的位址是否相等,而new則是開闢了新的記憶體空間,所以字串不能用==判斷

system.out.println("str1與str2是否相等:" +str1.equals(str2));

//3.字串長度

表示方法,不帶()表示屬性

system.out.println("「字串常量字串常量」的長度是:" +str.length());

//4.查詢字串中子字串的位置

從前往後查詢字串中子字串的位置,返回找到之後的首字的索引值

system.out.println("串的位置在第" +str.indexof("串")+"位");

system.out.println("「串常」的位置在第" +str.indexof("串常") +"位");//

當判斷多個字元時,只返回首個字元的位置

system.out.println("當查詢的子字串首字在其中,其後不在時:" +str.indexof("串字"));//

返回-1

從後面找首字的位置

system.out.println("倒數第乙個串的位置在第" +str.lastindexof("串"));

查詢不到,返回-1,可以用來判斷字串中是否包含某乙個字串

system.out.println("「我」是不是在str中:" +str.indexof("我"));

//5.獲取字元,獲取字串str中第四位的字元

str="字串常量字串常量";

system.out.println("字串str中第四位的字元是:" +str.charat(4));

"超字串長度的結果:" +str.charat(str.length()));超字串長度時報錯

//6.判斷字串的開始和結束

判斷字串的開始

system.out.println("字串str是不是以「字元」開始:" +str.startswith("字元"));//

方法1system.out.println("字串str是不是以「字元」開始:" +(str.indexof("字元")==0) );//

方法2system.out.println("字串str是不是以「字元」開始:" +(str.charat(0)=='字'&& str.charat(1)=='符'));//

方法3

判斷字串的結束

system.out.println("字串str是不是以「常量」結束:" +str.endswith("常量"));//

方法1system.out.println("字串str是不是以「常量」結束:" +(str.lastindexof("常量")==str.length()-2));//

方法2}

c 變數 字串複習

1.c 中變數分為值型別和引用型別,值型別包含 整形,浮點型別,和布林型別,其他都是引用型別。2.定義區域性變數時,需要對其進行初始之後才能使用 這點和c 不同,比c 安全 3.如果不做任何設定,包含小數點的數值都被認為是double型別,所以如果數值要以float型別來處理就應當強制通過使用f或f...

C語言字串(複習)

字串 用雙引號括起來的就是字串 字元常量 用單引號括起來的就是字元常量 abc 字串 abac 字元常量輸入輸出函式 字串的輸入輸出可以呼叫庫函式 1 gets和puts gets 函式輸入字元時,只有遇到回車符才認為字串入結束 puts 函式輸出時,遇到第乙個 0 就結束輸出 2 scanf和pr...

字串操作複習(一)

最近複習c語言,發現字串操作這塊都快忘光了。順便做了幾道題,複習一下。複習函式 1.include void memset void buffer,int ch,size t count 功能 函式拷貝ch 到buffer 從頭開始的count 個字元裡,並返回 buffer指標。memset 可以...