c++ string replace_Java——String类的常用方法总结
一、String類String類在java.lang包中,java使用String類創建一個字符串變量,字符串變量屬于對象。java把String類聲明的final類,不能有類。String類對象創建后不能修改,由0或多個字符組成,包含在一對雙引號之間。二、String類對象的創建字符串聲明:String stringName;字符串創建:stringName = new String(字符串常量);或stringName = 字符串常量;
三、String類構造方法
1、public String()
無參構造方法,用來創建空字符串的String對象。
2、public String(String value)用已知的字符串value創建一個String對象。
String?str2?=?new?String("IT咸魚");??String?str3?=?new?String(str2);3、public String(char[] value)用字符數組value創建一個String對象。char[] value = {'a','b','c','d'}; String str4 = new String(value);//相當于String str4 = new String("abcd");4、public String(char chars[], int startIndex, int numChars)用字符數組chars的startIndex開始的numChars個字符創建一個String對象。?char[]?value?=?{'a','b','c','d'};??String?str5?=?new?String(value,?1,?2);//相當于String?str5?=?new?String("bc");5、public String(byte[] values)用比特數組values創建一個String對象。byte[]?strb?=?new?byte[]{65,66};String?str6?=?new?String(strb);//相當于String?str6?=?new?String("AB");四、String類常用方法1、求字符串長度public int length()//返回該字符串的長度String?str?=?new?String("asdfzxc");int?strlength?=?str.length();//strlength?=?72、求字符串某一位置字符public char charAt(int index)//返回字符串中指定位置的字符;注意字符串中第一個字符索引是0,最后一個是length()-1。String?str?=?new?String("asdfzxc");char?ch?=?str.charAt(4);//ch?=?z3、提取子串用String類的substring方法可以提取字符串中的子串,該方法有兩種常用參數:1)public String substring(int beginIndex)//該方法從beginIndex位置起,從當前字符串中取出剩余的字符作為一個新的字符串返回。2)public String substring(int beginIndex, int endIndex)//該方法從beginIndex位置起,從當前字符串中取出到endIndex-1位置的字符作為一個新的字符串返回。String?str1?=?new?String("asdfzxc");String?str2?=?str1.substring(2);//str2?=?"dfzxc"String?str3?=?str1.substring(2,5);//str3?=?"dfz"4、字符串比較1)public int compareTo(String anotherString)//該方法是對字符串內容按字典順序進行大小比較,通過返回的整數值指明當前字符串與參數字符串的大小關系。若當前對象比參數大則返回正整數,反之返回負整數,相等返回0。2)public int compareToIgnore(String anotherString)//與compareTo方法相似,但忽略大小寫。3)public boolean equals(Object anotherObject)//比較當前字符串和參數字符串,在兩個字符串相等的時候返回true,否則返回false。4)public boolean equalsIgnoreCase(String anotherString)//與equals方法相似,但忽略大小寫。String?str1?=?new?String("abc");String?str2?=?new?String("ABC");int?a?=?str1.compareTo(str2);//a>0int?b?=?str1.compareToIgnoreCase(str2);//b=0boolean?c?=?str1.equals(str2);//c=falseboolean?d?=?str1.equalsIgnoreCase(str2);//d=true5、字符串連接public String concat(String str)//將參數中的字符串str連接到當前字符串的后面,效果等價于"+"。String?str?=?"aa".concat("bb").concat("cc");相當于String?str?=?"aa"+"bb"+"cc";6、字符串中單個字符查找1)public int indexOf(int ch/String str)//用于查找當前字符串中字符或子串,返回字符或子串在當前字符串中從左邊起首次出現的位置,若沒有出現則返回-1。2)public int indexOf(int ch/String str, int fromIndex)//改方法與第一種類似,區別在于該方法從fromIndex位置向后查找。3)public int lastIndexOf(int ch/String str)//該方法與第一種類似,區別在于該方法從字符串的末尾位置向前查找。4)public int lastIndexOf(int ch/String str, int fromIndex)//該方法與第二種方法類似,區別于該方法從fromIndex位置向前查找。String str = "I am a good student";int a = str.indexOf('a');//a = 2int b = str.indexOf("good");//b = 7int c = str.indexOf("w",2);//c = -1int d = str.lastIndexOf("a");//d = 5int e = str.lastIndexOf("a",3);//e = 27、字符串中字符的大小寫轉換1)public String toLowerCase()//返回將當前字符串中所有字符轉換成小寫后的新串2)public String toUpperCase()//返回將當前字符串中所有字符轉換成大寫后的新串String?str?=?new?String("asDF");String?str1?=?str.toLowerCase();//str1?=?"asdf"String?str2?=?str.toUpperCase();//str2?=?"ASDF"8、字符串中字符的替換1)public String replace(char oldChar, char newChar)//用字符newChar替換當前字符串中所有的oldChar字符,并返回一個新的字符串。2)public String replaceFirst(String regex, String replacement)//該方法用字符replacement的內容替換當前字符串中遇到的第一個和字符串regex相匹配的子串,應將新的字符串返回。3)public String replaceAll(String regex, String replacement)//該方法用字符replacement的內容替換當前字符串中遇到的所有和字符串regex相匹配的子串,應將新的字符串返回。String str = "asdzxcasd";String?str1?=?str.replace('a','g');//str1?=?"gsdzxcgsd"String?str2?=?str.replace("asd","fgh");//str2?=?"fghzxcfgh"String?str3?=?str.replaceFirst("asd","fgh");//str3?=?"fghzxcasd"String?str4?=?str.replaceAll("asd","fgh");//str4?=?"fghzxcfgh"9、其他類方法1)String trim()//截去字符串兩端的空格,但對于中間的空格不處理。String str = " a sd ";String?str1?=?str.trim();int?a?=?str.length();//a?=?6int?b?=?str1.length();//b?=?42)boolean statWith(String prefix)或boolean endWith(String suffix)//用來比較當前字符串的起始字符或子字符串prefix和終止字符或子字符串suffix是否和當前字符串相同,重載方法中同時還可以指定比較的開始位置offset。String str = "asdfgh";boolean?a?=?str.statWith("as");//a?=?trueboolean?b?=?str.endWith("gh");//b?=?true3)regionMatches(boolean b, int firstStart, String other, int otherStart, int length)//從當前字符串的firstStart位置開始比較,取長度為length的一個子字符串,other字符串從otherStart位置開始,指定另外一個長度為length的字符串,兩字符串比較,當b為true時字符串不區分大小寫。4)contains(Stringstr)//判斷參數s是否被包含在字符串中,并返回一個布爾類型的值。String?str?=?"student";str.contains("stu");//truestr.contains("ok");//false5)String[] split(String str)//將str作為分隔符進行字符串分解,分解后的字字符串在字符串數組中返回。String str = "asd!qwe|zxc#";String[]?str1?=?str.split("!|#");//str1[0]?=?"asd";str1[1]?=?"qwe";str1[2]?=?"zxc";五、字符串與基本類型的轉換1、字符串轉換為基本類型java.lang包中有Byte、Short、Integer、Float、Double類的調用方法:1)public static byte parseByte(String s)2)public static short parseShort(String s)3)public static short parseInt(String s)4)public static long parseLong(String s)5)public static float parseFloat(String s)6)public static double parseDouble(String s)例如:int n = Integer.parseInt("12");float?f?=?Float.parseFloat("12.34");double?d?=?Double.parseDouble("1.124");2、基本類型轉換為字符串類型String類中提供了String valueOf()方法,用作基本類型轉換為字符串類型。1)static String valueOf(char data[])2)static String valueOf(char data[], int offset, int count)3)static String valueOf(boolean b)4)static String valueOf(char c)5)static String valueOf(int i)6)static String valueOf(long l)7)static String valueOf(float f)8)static String valueOf(double d)例如:String?s1?=?String.valueOf(12);String?s1?=?String.valueOf(12.34);3、進制轉換使用Long類中的方法得到整數之間的各種進制轉換的方法:Long.toBinaryString(long l)Long.toOctalString(long l)Long.toHexString(long l)Long.toString(long l, int p)//p作為任意進制Java——Object類下面有幾種方法呢?
2020-10-13
說說hashCode() 和 equals() 之間的關系?
2020-10-12
什么情況用ArrayList 還是 LinkedList呢?
2020-10-11
如何選擇使用 HashMap 還是 TreeMap?
2020-10-10
java基礎——Java集合框架花樣問法!
2020-10-09
java基礎——Spring,SpringMVC,SpringBoot,SpringCloud有什么區別和聯系?
2020-10-08
END
如果看到這里,說明你喜歡這篇文章,請?轉發、點贊。同時?標星(置頂)本公眾號可以第一時間接受到博文推送。
我知道你 “在看”
總結
以上是生活随笔為你收集整理的c++ string replace_Java——String类的常用方法总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 5.7和8.0区别_前沿观察
- 下一篇: vhdl变量赋初值_5.5 C++自动变