1.區分大小寫的比較
**示例:public
boolean
equals
(object anobject)
2.不區分大小寫的比較public
class
test
}//列印結果
false
true
**示例:public
boolean
equalsignorecase
(string anotherstring)
3.比較兩個字串的大小關係public
class
test
}//列印結果
true
在string類中compareto()方法是乙個非常重要的方法,該方法返回乙個整型,該資料會根據大小關係返回三類內容:public
intcompareto
(string anotherstring)
①若兩個內容相等:返回0
②小於:返回內容小於0
③大於:返回內容大於0
**示例:
觀察上述**可以發現,compareto()方法是乙個可以區分大小關係的方法。因為string類已經實現了comparable介面,所以可以直接用compareto()方法.public
class
test
}//列印結果-32
320
1.判斷乙個子字串是否存在
**示例:public
boolean
contains
(charsequence s)
2.從頭開始查詢指定字串的位置,查到返回位置的開始索引,如果查不到返回-1(注意:返回首次出現的下標)public
class
test
}//列印結果
true
**示例:public
intindexof
(string str)
contains()方法比較方便,但是效率沒有indexof()方法效率高,看底層**可以看到contains()方法呼叫了indexof()方法public
class
test
}//列印結果-1
5
contains()底層原始碼:
3.從指定位置開始查詢子字串位置
**示例:public
intindexof
(string str,
int fromindex)
4.由後向前查詢子字串位置public
class
test
}//列印結果6-
1
**示例:public
intlastindexof
(string str)
5.從指定位置由後向前查詢public
class
test
}//列印結果
6
**示例:public
intlastindexof
(string str,
int fromindex)
6.判斷是否以指定字串開頭public
class
test
}//列印結果
46
7.從指定位置開始判斷是否以指定位置開頭public
boolean
startswith
(string prefix)
8.判斷是否以指定字串結尾public
boolean
startswith
(string prefix,
int toffset)
**示例:public
boolean
endswith
(string suffix)
1.替換所有的指定內容public
class
test
}//列印結果
false
true
false
true
true
2.替換首個內容public string replaceall
(string regex,string replacement)
**示例:public string replacefirst
(string regex,string replacement)
注意:字串是不可變物件,替換是不修改當前字串,而是產生乙個新的字串public
class
test
}//列印結果
imas--uden--
imas--udent
1.將字串全部拆分
**示例:public string[
]split
(string regex)
2.將字串部分拆分,該陣列長度就是limit極限public
class
test}}
//列印結果
拆分時要注意有一些特殊字元作為分隔符可能無法正確切分,需要加上轉義public string[
]split
(string regex,
int limit)
1.從指定索引擷取到結尾public
class
test}}
//列印結果
192168
12
2.擷取部分內容public string substring
(int beginindex)
**示例:public string substring
(int beginindex,
int endindex)
注意:substring(0,5)表示包含0號下標,不包含5號下public
class
test
}//列印結果
word
hello
字串常見操作
彙總c 常用函式和方法集 3 變數.tostring 字元型轉換 轉為字串 12345.tostring n 生成 12,345.00 12345.tostring c 生成 12,345.00 12345.tostring e 生成 1.234500e 004 12345.tostring f4 ...
字串常見操作
字串常見操作 1 取字串長度 length 2 字串轉為比 getbytes 4 截斷字串的一部分 變數.substring 起始位置,擷取位數 5 查指定位置是否為空字元 char.iswhitespace 字串變數,位數 6 查字元是否是標點符號 char.ispunctuation 字元 7 ...
字串常見操作
如有字串mystr hello world itcast and itcastcpp 以下是常見的操作 1 find 檢測 str 是否包含在 mystr中,如果是返回開始的索引值,否則返回 1 mystr.find str,start 0,end len mystr 2 index 跟find 方...