str.charAt(int index),根據(jù)下標(biāo)找到指定的字符派昧,返回char
str.toCharArry(),以字符數(shù)組的形式返回全部的字符串內(nèi)容
String s1 = new String(cs);將全部的字符數(shù)組char [] cs變?yōu)樽址?String s2=new String(cs,offset,count) 弧岳,將指定范圍內(nèi)的字符數(shù)組變?yōu)樽址み海籧s為某一數(shù)組洪鸭,offset為下標(biāo)的初始位置皮仁,count為長(zhǎng)度
str.getBytes()知市,將字符串變?yōu)樽止?jié)數(shù)組量瓜;byte [] bytes = str.getBytes();
String s3 = new String(byte[] bytes)打毛,將字節(jié)數(shù)組bytes變?yōu)樽址?String s4 = new String(byte[] bytes,offset,length)晦雨,將指定范圍內(nèi)的字節(jié)數(shù)組bytes變?yōu)樽址?String s5 = new String(byte[] bytes,String charsetName),通過(guò)使用指定的charset解碼指定的bytes數(shù)組,構(gòu)造一個(gè)新的String
str.startsWith(String str)隘冲,從第一個(gè)位置開(kāi)始判斷是否以指定的內(nèi)容開(kāi)頭闹瞧,返回boolean
str.startsWith(String str,int toffset),從指定的的位置開(kāi)始判斷是否以指定的內(nèi)容開(kāi)頭展辞,返回boolean
str.endsWith(String suffix)奥邮,判斷是否以指定的內(nèi)容結(jié)尾,返回boolean
str.replace(char oldChar,char newChar)罗珍,替換指定的字符洽腺,str.replace("a","*")
str.replace(CharSequence target,CharSequence replacement),替換指定字符串覆旱,str.replace("asd","999")
str.replaceAll(String regex,String replacement)蘸朋,替換指定的字符串System.out.println(str.replaceAll("[0-9]","*")); //asdfqwerwfsadf****;System.out.println(str.replaceAll("\\d","*")); //asdfqwerwfsadf****
str.replaceFirst(String regex,String replacement)扣唱,替換第一個(gè)滿足條件的字符串System.out.println(str.replaceAll("\\d","MM")); //asdfqwerwfsadfMMMMMMMM
str.substring(int beginIndex)藕坯,從指定位置開(kāi)始一直截取到末尾System.out.println(str.substring(4)); //結(jié)果:qwerwfsadf2432
str.substring(int beginIndex,int endIndex),截取指定范圍內(nèi)的字符串System.out.println(str.substring(2,4)); //結(jié)果:df
str.split(String regex)噪沙,按照指定的字符串拆分炼彪,返回的是:字符串?dāng)?shù)組System.out.println(Arrays.toString(str.split("qw"))); //[asdf, erwfsadf2432]
str.split(String regex,int limit),拆分字符串正歼,并執(zhí)行拆分的個(gè)數(shù)
System.out.println(Arrays.toString(str.split("qw", 3))); //limit=1時(shí)[asdfqwerwfsadf2432]辐马,limit=2時(shí)[asdf, erwfsadf2432],limit=3時(shí)[asdf, erwfsadf2432]
str.contains(String s)局义,判斷一個(gè)字符串是否存在喜爷,返回boolean
str.indexOf(int ch),從頭查找指定的字符是否存在萄唇,如果存才返回位置index檩帐,如果不存在返回-1
str.indexOf(int ch,int fromIndex),從指定位置查找指定的字符是否存在穷绵,如果存在返回位置index轿塔,如果不存在返回-1
str.indexOf(String str)特愿,從頭查找指定的字符串是否存在仲墨,如果存在返回位置index勾缭,如果不存在返回-1
str.indexOf(String str,int fromIndex),從指定位置查找指定的字符串是否存在目养,如果存在返回位置index俩由,如果不存在返回-1
str.lastIndex(int ch),從字符串的最后向前查找癌蚁,指定的字符是否存在幻梯,如果存在則返回位置,如果不存在則返回-1
str.lastIndex(int ch,int fromIndex)努释,從字符串的指定的末尾向前查找碘梢,指定的字符是否存在,如果存在則返回位置伐蒂,如果不存在則返回-1
str.lastIndex(String str)煞躬,從字符串的最后向前查找,指定的字符串是否存在逸邦,如果存在則返回位置恩沛,如果不存在則返回-1
str.lastIndexOf(String str,int fromIndex),從字符串的指定的末尾向前查找缕减,指定的字符串是否存在雷客,如果存在返回位置index,如果不存在返回-1
str.isEmpty()桥狡,判斷是否為空搅裙,指定是內(nèi)容為空“""”,null是不算的裹芝,返回boolean
str.length()呈宇,取得字符串的長(zhǎng)度,返回int
str.toLowerCase()局雄,字符串的字母轉(zhuǎn)小寫(xiě)
str.toUpperCase()甥啄,字符串的字母轉(zhuǎn)大寫(xiě)
str.trim(),去掉字符串開(kāi)頭和結(jié)尾的空格炬搭,中間的空格不去
str.concat(String str1)蜈漓,將字符串str1連接在str的后面
System.out.println(str.isEmpty());
System.out.println(str.length()); //數(shù)組的length是屬性不是方法所有數(shù)組的length沒(méi)有括號(hào)
System.out.println(str.trim());
System.out.println(str.concat("****"));
System.out.println(String.valueOf(true)); //將boolean值true轉(zhuǎn)換成字符串,所有的其它也都可以轉(zhuǎn)包括對(duì)象也行
package com.string;
import java.util.Arrays;
public class StringDemo2 {
public static void main(String[] args) {
//String類(lèi)字符 與 字符串操作方法:
//方法1:str.charAt(int index),根據(jù)下標(biāo)找到指定的字符
String str = " asdfqwerwf sadf2432 ";
char c = str.charAt(2);
System.out.println(c);
//方法2:str.toCharArry()宫盔,以字符數(shù)組的形式返回全部的字符串內(nèi)容
System.out.println(str.toCharArray());
//方法3:new String(cs) 將全部的字符數(shù)組 cs 變?yōu)樽址? char [] cs = {'a','b','c','d','e'};
String s1 = new String(cs);
System.out.println(s1);
//方法3:new String(cs,offset,count) 融虽,將指定范圍內(nèi)的字符數(shù)組變?yōu)樽址? //cs為某一數(shù)組,offset為下標(biāo)的初始位置灼芭,count為長(zhǎng)度
String s2 = new String(cs,2,1);
System.out.println(s2);
//Striing類(lèi)字節(jié) 與 字符串操作方法
//方法1:str.getBytes()有额,將字符串變?yōu)樽止?jié)數(shù)組
byte [] bytes = str.getBytes();
System.out.println(str.getBytes());
System.out.println(Arrays.toString(str.getBytes()));
//方法2:new String(byte[] bytes),將字節(jié)數(shù)組bytes變?yōu)樽址? String s3 = new String(bytes);
System.out.println(s3);
//方法3:new String(byte[] bytes,offset,length),將指定范圍內(nèi)的字節(jié)數(shù)組bytes變?yōu)樽址? String s4 = new String(bytes,0,2);
System.out.println(s4);
//方法4:new String(byte[] bytes,String charsetName),通過(guò)使用指定的charset解碼指定的bytes數(shù)組巍佑,構(gòu)造一個(gè)新的String
//String類(lèi)判斷是否以指定內(nèi)容開(kāi)頭或結(jié)尾
//方法1:str.startsWith(String str)茴迁,從第一個(gè)位置開(kāi)始判斷是否以指定的內(nèi)容開(kāi)頭
System.out.println(str.startsWith("as")); //返回boolean
//方法2:str.startsWith(String str,int toffset),從指定的的位置開(kāi)始判斷是否以指定的內(nèi)容開(kāi)頭
System.out.println(str.startsWith("as",0)); //返回boolean
//方法3:str.endsWith(String suffix)萤衰,判斷是否以指定的內(nèi)容結(jié)尾堕义,返回boolean
//String類(lèi)替換操作
//方法1:str.replace(char oldChar,char newChar),替換指定的字符
System.out.println(str.replace("a","*"));
//方法2:str.replace(CharSequence target,CharSequence replacement)脆栋,替換指定字符串
System.out.println(str.replace("asd","999"));
//方法3:str.replaceAll(String regex,String replacement)倦卖,替換指定的字符串
System.out.println(str.replaceAll("[0-9]","*")); //asdfqwerwfsadf****
System.out.println(str.replaceAll("\\d","*")); //asdfqwerwfsadf****
//方法4:str.replaceFirst(String regex,String replacement),替換第一個(gè)滿足條件的字符串
System.out.println(str.replaceAll("\\d","MM")); //asdfqwerwfsadfMMMMMMMM
//String類(lèi)字符串截取操作
//方法1:str.substring(int beginIndex)椿争,從指定位置開(kāi)始一直截取到末尾
System.out.println(str.substring(4)); //結(jié)果:qwerwfsadf2432
//方法2:str.substring(int beginIndex,int endIndex)怕膛,截取指定范圍內(nèi)的字符串
System.out.println(str.substring(2,4)); //結(jié)果:df
//String類(lèi)字符串拆分操作
//方法1:str.split(String regex),按照指定的字符串拆分秦踪,返回的是:字符串?dāng)?shù)組
System.out.println(Arrays.toString(str.split("qw"))); //[asdf, erwfsadf2432]
//方法2:str.split(String regex,int limit)嘉竟,拆分字符串,并執(zhí)行拆分的個(gè)數(shù)
System.out.println(Arrays.toString(str.split("qw", 3))); //limit=1時(shí)[asdfqwerwfsadf2432]洋侨,limit=2時(shí)[asdf, erwfsadf2432]舍扰,limit=3時(shí)[asdf, erwfsadf2432]
//String類(lèi)字符串查找操作
//方法1:str.contains(String s),判斷一個(gè)字符串是否存在希坚,返回boolean
System.out.println(str.contains("s"));
//方法2:str.indexOf(int ch)边苹,從頭查找指定的字符是否存在,如果存才返回位置index裁僧,如果不存在返回-1
System.out.println(str.indexOf("0"));
//方法3:str.indexOf(int ch,int fromIndex)个束,從指定位置查找指定的字符是否存在,如果存在返回位置index聊疲,如果不存在返回-1
//方法4:str.indexOf(String str)茬底,從頭查找指定的字符串是否存在,如果存在返回位置index获洲,如果不存在返回-1
//方法5:str.indexOf(String str,int fromIndex)阱表,從指定位置查找指定的字符串是否存在,如果存在返回位置index贡珊,如果不存在返回-1
//方法6:str.lastIndex(int ch)最爬,從字符串的最后向前查找,指定的字符是否存在门岔,如果存在則返回位置爱致,如果不存在則返回-1
System.out.println(str.lastIndexOf("f"));
//方法7:str.lastIndex(int ch,int fromIndex),從字符串的指定的末尾向前查找寒随,指定的字符是否存在糠悯,如果存在則返回位置帮坚,如果不存在則返回-1
//方法8:str.lastIndex(String str),從字符串的最后向前查找互艾,指定的字符串是否存在试和,如果存在則返回位置,如果不存在則返回-1
//方法9:str.lastIndexOf(String str,int fromIndex)忘朝,從字符串的指定的末尾向前查找灰署,指定的字符串是否存在判帮,如果存在返回位置index局嘁,如果不存在返回-1
//String類(lèi)其它操作方法:
//方法1:str.isEmpty(),判斷是否為空晦墙,指定是內(nèi)容為空“""”悦昵,null是不算的,返回boolean
//方法2:str.length()晌畅,取得字符串的長(zhǎng)度但指,返回int
//方法3:str.toLowerCase(),字符串的字母轉(zhuǎn)小寫(xiě)
//方法4:str.toUpperCase()抗楔,字符串的字母轉(zhuǎn)大寫(xiě)
//反法5:str.trim()棋凳,去掉字符串開(kāi)頭和結(jié)尾的空格,中間的空格不去
//方法6:str.concat(String str1)连躏,將字符串str1連接在str的后面
System.out.println(str.isEmpty());
System.out.println(str.length()); //數(shù)組的length是屬性不是方法所有數(shù)組的length沒(méi)有括號(hào)
System.out.println(str.trim());
System.out.println(str.concat("****"));
System.out.println(String.valueOf(true)); //將boolean值true轉(zhuǎn)換成字符串剩岳,所有的其它也都可以轉(zhuǎn)包括對(duì)象也行
}
}