java常見字串方法總結

2021-08-08 01:23:30 字數 2478 閱讀 8911

說到這裡的方法總結,其實也不是自己寫的啦,就是通過學習和工作中,總結了一些常用的方法,拿出來與大家分享。我不生產方法,我只是方法的搬運工。有什麼不足,請同行們多多指教,共同進步~

1.string類字元與字串操作方法

方法名稱                                      型別                                  描述

public char charat(int index)       普通                                 根據下標找到指定的字元

public char  tochararray()         普通                                 以字串陣列的形式返回全部的字元

public string (char  value)          構造                                 將全部的字元陣列變為字串

public string (char value,int offset,int account)                 通過使用指定的charset解碼指定

2.string 型別判斷是否以指定內容開頭或結尾

方法名稱      型別                                  描述

public boolean startwith(string prefix)      普通                  從第乙個位置開始判斷是否以指定的內容開頭

public boolean startwith(string prefix,int toffset) 普通      從指定的位置開始判斷是否以指定的內容開頭

public boolean endswith(string suffix)       普通                 判斷是否以指定的內容結尾

3.string 類替換操作

方法名稱                   型別描述

public string replace(char old char , char new char)     普通                         替換指定字元

public string replace(charsequence target , charsequence replacement)  普通    替換指定字串

public string replaceall(string regex , string replacement)    普通                替換第乙個滿足條件的字串

4.string 類字串擷取操作:

public string substring(int beginindex)             普通                 從指定位置開始一直擷取到末尾

public string substring(int beginindex , int endindex)   普通   擷取指定範圍的字串

注意:只包括起始位置,不包括結束位置!

5.string類字串拆分操作:

public string split(string regex)          普通           按照指定的字串拆分

public string split(string regex , int limit)   普通  拆分字串,並指定拆分的個數

注意:regex是正規表示式,需要對應著正規表示式表進行編寫

6.string 類字串查詢操作:

public boolean contains(string s)       普通      返回乙個字串是否存在

public int indexof(int ch)       普通      從頭檢查指定的字串是否存在,char->int,若存在則返回位置,若不存在則返回"-1"

public int indexof(string ch , int fromindex)普通;從指定的位置查詢字串是否存在,若存在則返回位置,若不存在則返回"-1"

public int indexof(string str , int fromindex)  普通   從指定位置查詢到字串是否存在,存在返所在位置,不存在返回"-1"

7.string 類其它操作方法

public boolean isempty()     普通          判斷是否為空,指定的內容為空「 "" 」

public int          length())       普通         取字串長度

public string tolowercase()  普通       轉小寫

public string touppercase()   普通       轉大寫

public string trim()                 普通       去掉開頭和結尾的空格,中間的空格不去

public string concat()             普通       字串連線操作 

python常見字串方法

字串.isalnum 所有字元都是數字或者字母,為真返回 ture,否則返回 false。字串.isalpha 所有字元都是字母,為真返回 ture,否則返回 false。字串.isdigit 所有字元都是數字,為真返回 ture,否則返回 false。字串.islower 所有字元都是小寫,為真返...

常見字串問題總結(三)

常見字串問題總結 三 字串問題中經常會有刪除某一字串的某乙個字元,可以是不同方式的刪除,比如將指定的字元刪除,刪除字元中的空格 包括字串的頭和尾 本文就列舉了部分有關字串刪除的問題。1.刪除字串中的空格,要求將字串前面和後面的所有空格都刪除,對字串中間,如果出現多個連續的空格,只保留乙個空格。inc...

常見字串試題集合總結

4 求所有碎片的平均長度 5 用於匹配模式串的kmp knuth morris pratt 演算法 5 滑動視窗 不論是左旋轉還是右旋轉,即把前面的子串往後放還是後面的子串往前放,最簡潔的方法都是先把原始串複製乙份並拼接起來,然後按需擷取。本題鏈結在此,如下,可以將前面那個字元放到末尾 public...