字串操作集合

2021-06-18 01:56:27 字數 1670 閱讀 1167

開發中對字串操作太多了,有著一系列方法,

注:該文會隨著遇到問題而更新!

1.substring     擷取

string stringsta="hello word";

①substring(int x) //指定開始位置,到字串結束

system.out.printl(stringsta.substring(1));

//列印:ello word

②substring(int x,int x) //指定開始位置 結束位置

for(int i=0;i

2.replace  替換

string textname="hello word hello word 123456789";

①replace(oldchar, newchar) //把字串所有相同的字元替換

system.out.println(textname.replace("w", "w"));

//列印:hello word hello word

②replaceall(regex, replacement) //屬於正規表示式的替換

system.out.println(textname.replaceall("\\d", "*")); // \d表示數字1--9 換成 *

//列印:hello word hello word *********

3.split(" ")  將字串分成了陣列

// 從中乙個字串遇到空格,就分割為若干個子字串

listliststring = new arraylist();

string stringarray = textname.split(" ");

// 然後動態遍歷、再動態賦值給陣列(list/map)

for (string stemp : stringarray)

4.大小寫字母,轉換+判斷

string stringdata1 = "admin";

string stringdata2 = "admin";

char stringdata3 = 'a';

// 轉換成大寫

system.out.println(stringdata1.touppercase());

// 轉換成小寫

system.out.println(stringdata2.tolowercase());

/*** public static boolean isuppercase(char ch) 確定指定的字元是否為大寫字元

* 此方法返回true,如果字元為大寫,否則返回false。

* */

system.out.println(character.isuppercase(stringdata3));

5.matcher正則  出現位置

string stringsta = "hello word";

matcher matcher = pattern.compile("[o]").matcher(stringsta);

// 是否存在

if (matcher.find()) else

iOS字串常用操作集合

一。ios 字串擷取 1.定義乙個字串a,擷取a 的某乙個部分,複製給b,b必須是int型 nsstring a 1.2.30 intb a substringwithrange nsmakerange 4,2 intvalue nslog a n a nslog b d b output 2011...

js 字串操作函式集合

concat 將兩個或多個字元的文字組合起來,返回乙個新的字串。indexof 返回字串中乙個子串第一處出現的索引。如果沒有匹配項,返回 1 charat 返回指定位置的字元。lastindexof 返回字串中乙個子串最後一處出現的索引,如果沒有匹配項,返回 1 match 檢查乙個字串是否匹配乙個...

python 字串 檔案 集合操作

原本這幾個應該和列表 字典一起寫的,只是我不是一起學的,所以就分開寫了。字串操作 特性 不可修改 一些基本語法 name.find a 查詢a,找到返回其索引,找不到返回 1name.capitalize 首字母大寫 name.casefold 大寫全部變小寫 name.center 50,輸出 y...