byte数组转为string_String类
生活随笔
收集整理的這篇文章主要介紹了
byte数组转为string_String类
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
API ----StringBuffer
java.lang.Object
繼承者 java.lang.String
YuSLi:補充:String和StringBuffer的相互轉(zhuǎn)換
String類概述及其構(gòu)造方法:
在實際開發(fā)中,字符串的操作是最常見的操作,沒有之一。 而Java沒有內(nèi)置的字符串類型,所以,就在Java類庫中提供了一個類String 供我們來使用。String 類代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作為此類的實例實現(xiàn)。注意:String s = “helloworld”;s也是一個對象。package cn.itcast_01;/** 字符串:就是由多個字符組成的一串?dāng)?shù)據(jù)。也可以看成是一個字符數(shù)組。* 通過查看API,我們可以知道* A:字符串字面值"abc"也可以看成是一個字符串對象。* B:字符串是常量,一旦被賦值,就不能被改變。* * 構(gòu)造方法:* public String():空構(gòu)造* public String(byte[] bytes):把字節(jié)數(shù)組轉(zhuǎn)成字符串* public String(byte[] bytes,int index,int length):把字節(jié)數(shù)組的一部分轉(zhuǎn)成字符串* public String(char[] value):把字符數(shù)組轉(zhuǎn)成字符串* public String(char[] value,int index,int count):把字符數(shù)組的一部分轉(zhuǎn)成字符串* public String(String original):把字符串常量值轉(zhuǎn)成字符串** 字符串的方法:* public int length():返回此字符串的長度。*/ public class StringDemo {public static void main(String[] args) {// public String():空構(gòu)造String s1 = new String();System.out.println("s1:" + s1);System.out.println("s1.length():" + s1.length());System.out.println("--------------------------");// public String(byte[] bytes):把字節(jié)數(shù)組轉(zhuǎn)成字符串byte[] bys = { 97, 98, 99, 100, 101 };String s2 = new String(bys);System.out.println("s2:" + s2);System.out.println("s2.length():" + s2.length());System.out.println("--------------------------");// public String(byte[] bytes,int index,int length):把字節(jié)數(shù)組的一部分轉(zhuǎn)成字符串// 我想得到字符串"bcd"String s3 = new String(bys, 1, 3);System.out.println("s3:" + s3);System.out.println("s3.length():" + s3.length());System.out.println("--------------------------");// public String(char[] value):把字符數(shù)組轉(zhuǎn)成字符串char[] chs = { 'a', 'b', 'c', 'd', 'e', '愛', '林', '親' };String s4 = new String(chs);System.out.println("s4:" + s4);System.out.println("s4.length():" + s4.length());System.out.println("--------------------------");// public String(char[] value,int index,int count):把字符數(shù)組的一部分轉(zhuǎn)成字符串String s5 = new String(chs, 2, 4);System.out.println("s5:" + s5);System.out.println("s5.length():" + s5.length());System.out.println("--------------------------");//public String(String original):把字符串常量值轉(zhuǎn)成字符串String s6 = new String("abcde");System.out.println("s6:" + s6);System.out.println("s6.length():" + s6.length());System.out.println("--------------------------");//字符串字面值"abc"也可以看成是一個字符串對象。String s7 = "abcde";System.out.println("s7:"+s7);System.out.println("s7.length():"+s7.length());} }字符串的特點:一旦被賦值,就不能改變。
package cn.itcast_02;/** 字符串的特點:一旦被賦值,就不能改變。* String s = “hello”; s += “world”; 問s的結(jié)果是多少?* 答案:helloworld(值不能變,但是s是可以變的)*/ public class StringDemo {public static void main(String[] args) {String s = "hello";s += "world";System.out.println("s:" + s); // helloworld} }String的特點一旦被賦值就不能改變.bmppackage cn.itcast_02;/** String s = new String(“hello”)和String s = “hello”;的區(qū)別?* 有。前者會創(chuàng)建2個對象,后者創(chuàng)建1個對象。* * ==:比較引用類型比較的是地址值是否相同* equals:比較引用類型默認(rèn)也是比較地址值是否相同,而String類重寫了equals()方法,比較的是內(nèi)容是否相同。*/ public class StringDemo2 {public static void main(String[] args) {String s1 = new String("hello");String s2 = "hello";System.out.println(s1 == s2);// falseSystem.out.println(s1.equals(s2));// true} }package cn.itcast_02;/** 看程序?qū)懡Y(jié)果* 字符串如果是變量相加,先開空間,在拼接。* 字符串如果是常量相加,是先加,然后在常量池找,如果有就直接返回,否則,就創(chuàng)建。*/ public class StringDemo4 {public static void main(String[] args) {String s1 = "hello";String s2 = "world";String s3 = "helloworld";System.out.println(s3 == s1 + s2);// falseSystem.out.println(s3.equals((s1 + s2)));// trueSystem.out.println(s3 == "hello" + "world");// false 這個我們錯了,應(yīng)該是trueSystem.out.println(s3.equals("hello" + "world"));// true// 通過反編譯看源碼,我們知道這里已經(jīng)做好了處理。// System.out.println(s3 == "helloworld");// System.out.println(s3.equals("helloworld"));} }String類的判斷功能:
package cn.itcast_03;/** String類的判斷功能:* boolean equals(Object obj):比較字符串的內(nèi)容是否相同,區(qū)分大小寫* boolean equalsIgnoreCase(String str):比較字符串的內(nèi)容是否相同,忽略大小寫* boolean contains(String str):判斷大字符串中是否包含小字符串* boolean startsWith(String str):判斷字符串是否以某個指定的字符串開頭* boolean endsWith(String str):判斷字符串是否以某個指定的字符串結(jié)尾* boolean isEmpty():判斷字符串是否為空。* * 注意:* 字符串內(nèi)容為空和字符串對象為空。* String s = "";* String s = null;*/ public class StringDemo {public static void main(String[] args) {// 創(chuàng)建字符串對象String s1 = "helloworld";String s2 = "helloworld";String s3 = "HelloWorld";// boolean equals(Object obj):比較字符串的內(nèi)容是否相同,區(qū)分大小寫System.out.println("equals:" + s1.equals(s2));System.out.println("equals:" + s1.equals(s3));System.out.println("-----------------------");// boolean equalsIgnoreCase(String str):比較字符串的內(nèi)容是否相同,忽略大小寫System.out.println("equals:" + s1.equalsIgnoreCase(s2));System.out.println("equals:" + s1.equalsIgnoreCase(s3));System.out.println("-----------------------");// boolean contains(String str):判斷大字符串中是否包含小字符串System.out.println("contains:" + s1.contains("hello"));System.out.println("contains:" + s1.contains("hw"));System.out.println("-----------------------");// boolean startsWith(String str):判斷字符串是否以某個指定的字符串開頭System.out.println("startsWith:" + s1.startsWith("h"));System.out.println("startsWith:" + s1.startsWith("hello"));System.out.println("startsWith:" + s1.startsWith("world"));System.out.println("-----------------------");// 練習(xí):boolean endsWith(String str):判斷字符串是否以某個指定的字符串結(jié)尾這個自己玩// boolean isEmpty():判斷字符串是否為空。System.out.println("isEmpty:" + s1.isEmpty());String s4 = "";String s5 = null;System.out.println("isEmpty:" + s4.isEmpty());// NullPointerException// s5對象都不存在,所以不能調(diào)用方法,空指針異常System.out.println("isEmpty:" + s5.isEmpty());} }package cn.itcast_03;import java.util.Scanner;/** 模擬登錄,給三次機會,并提示還有幾次。如果登錄成功,就可以玩猜數(shù)字小游戲了。* * 分析:* A:定義用戶名和密碼。已存在的。* B:鍵盤錄入用戶名和密碼。* C:比較用戶名和密碼。* 如果都相同,則登錄成功* 如果有一個不同,則登錄失敗* D:給三次機會,用循環(huán)改進(jìn),最好用for循環(huán)。*/ public class StringTest2 {public static void main(String[] args) {// 定義用戶名和密碼。已存在的。String username = "admin";String password = "admin";// 給三次機會,用循環(huán)改進(jìn),最好用for循環(huán)。for (int x = 0; x < 3; x++) {// x=0,1,2// 鍵盤錄入用戶名和密碼。Scanner sc = new Scanner(System.in);System.out.println("請輸入用戶名:");String name = sc.nextLine();System.out.println("請輸入密碼:");String pwd = sc.nextLine();// 比較用戶名和密碼。if (name.equals(username) && pwd.equals(password)) {// 如果都相同,則登錄成功System.out.println("登錄成功,開始玩游戲");//猜數(shù)字游戲GuessNumberGame.start();break;} else {// 如果有一個不同,則登錄失敗// 2,1,0// 如果是第0次,應(yīng)該換一種提示if ((2 - x) == 0) {System.out.println("帳號被鎖定,請與班長聯(lián)系");} else {System.out.println("登錄失敗,你還有" + (2 - x) + "次機會");}}}} }package cn.itcast_03;import java.util.Scanner;/** 這時猜數(shù)字小游戲的代碼*/ public class GuessNumberGame {private GuessNumberGame() {}public static void start() {// 產(chǎn)生一個隨機數(shù)int number = (int) (Math.random() * 100) + 1;while (true) {// 鍵盤錄入數(shù)據(jù)Scanner sc = new Scanner(System.in);System.out.println("請輸入你要猜的數(shù)據(jù)(1-100):");int guessNumber = sc.nextInt();// 判斷if (guessNumber > number) {System.out.println("你猜的數(shù)據(jù)" + guessNumber + "大了");} else if (guessNumber < number) {System.out.println("你猜的數(shù)據(jù)" + guessNumber + "小了");} else {System.out.println("恭喜你,猜中了");break;}}} }String類的獲取功能:
package cn.itcast_04;/** String類的獲取功能* int length():獲取字符串的長度。* char charAt(int index):獲取指定索引位置的字符* int indexOf(int ch):返回指定字符在此字符串中第一次出現(xiàn)處的索引。* 為什么這里是int類型,而不是char類型?* 原因是:'a'和97其實都可以代表'a'* int indexOf(String str):返回指定字符串在此字符串中第一次出現(xiàn)處的索引。* int indexOf(int ch,int fromIndex):返回指定字符在此字符串中從指定位置后第一次出現(xiàn)處的索引。* int indexOf(String str,int fromIndex):返回指定字符串在此字符串中從指定位置后第一次出現(xiàn)處的索引。* String substring(int start):從指定位置開始截取字符串,默認(rèn)到末尾。* String substring(int start,int end):從指定位置開始到指定位置結(jié)束截取字符串。*/ public class StringDemo {public static void main(String[] args) {// 定義一個字符串對象String s = "helloworld";// int length():獲取字符串的長度。System.out.println("s.length:" + s.length());System.out.println("----------------------");// char charAt(int index):獲取指定索引位置的字符System.out.println("charAt:" + s.charAt(7));System.out.println("----------------------");// int indexOf(int ch):返回指定字符在此字符串中第一次出現(xiàn)處的索引。System.out.println("indexOf:" + s.indexOf('l'));System.out.println("----------------------");// int indexOf(String str):返回指定字符串在此字符串中第一次出現(xiàn)處的索引。System.out.println("indexOf:" + s.indexOf("owo"));System.out.println("----------------------");// int indexOf(int ch,int fromIndex):返回指定字符在此字符串中從指定位置后第一次出現(xiàn)處的索引。System.out.println("indexOf:" + s.indexOf('l', 4));System.out.println("indexOf:" + s.indexOf('k', 4)); // -1System.out.println("indexOf:" + s.indexOf('l', 40)); // -1System.out.println("----------------------");// 自己練習(xí):int indexOf(String str,int// fromIndex):返回指定字符串在此字符串中從指定位置后第一次出現(xiàn)處的索引。// String substring(int start):從指定位置開始截取字符串,默認(rèn)到末尾。包含start這個索引System.out.println("substring:" + s.substring(5));System.out.println("substring:" + s.substring(0));System.out.println("----------------------");// String substring(int start,int// end):從指定位置開始到指定位置結(jié)束截取字符串。包括start索引但是不包end索引System.out.println("substring:" + s.substring(3, 8));System.out.println("substring:" + s.substring(0, s.length()));} }package cn.itcast_04;/** 需求:遍歷獲取字符串中的每一個字符* * 分析:* A:如何能夠拿到每一個字符呢?* char charAt(int index)* B:我怎么知道字符到底有多少個呢?* int length()*/ public class StringTest {public static void main(String[] args) {// 定義字符串String s = "helloworld";// 原始版本// System.out.println(s.charAt(0));// System.out.println(s.charAt(1));// System.out.println(s.charAt(2));// System.out.println(s.charAt(3));// System.out.println(s.charAt(4));// System.out.println(s.charAt(5));// System.out.println(s.charAt(6));// System.out.println(s.charAt(7));// System.out.println(s.charAt(8));// System.out.println(s.charAt(9));// 只需要我們從0取到9// for (int x = 0; x < 10; x++) {// System.out.println(s.charAt(x));// }// 如果長度特別長,我不可能去數(shù),所以我們要用長度功能for (int x = 0; x < s.length(); x++) {// char ch = s.charAt(x);// System.out.println(ch);// 僅僅是輸出,我就直接輸出了System.out.println(s.charAt(x));}} }package cn.itcast_04;/** 需求:統(tǒng)計一個字符串中大寫字母字符,小寫字母字符,數(shù)字字符出現(xiàn)的次數(shù)。(不考慮其他字符)* 舉例:* "Hello123World"* 結(jié)果:* 大寫字符:2個* 小寫字符:8個* 數(shù)字字符:3個* * 分析:* 前提:字符串要存在* A:定義三個統(tǒng)計變量* bigCount=0* smallCount=0* numberCount=0* B:遍歷字符串,得到每一個字符。* length()和charAt()結(jié)合* C:判斷該字符到底是屬于那種類型的* 大:bigCount++* 小:smallCount++* 數(shù)字:numberCount++* * 這道題目的難點就是如何判斷某個字符是大的,還是小的,還是數(shù)字的。* ASCII碼表:* 0 48* A 65* a 97* 雖然,我們按照數(shù)字的這種比較是可以的,但是想多了,有比這還簡單的* char ch = s.charAt(x);* * if(ch>='0' && ch<='9') numberCount++* if(ch>='a' && ch<='z') smallCount++* if(ch>='A' && ch<='Z') bigCount++* D:輸出結(jié)果。** 練習(xí):把給定字符串的方式,改進(jìn)為鍵盤錄入字符串的方式。*/ public class StringTest2 {public static void main(String[] args) {//定義一個字符串String s = "Hello123World";//定義三個統(tǒng)計變量int bigCount = 0;int smallCount = 0;int numberCount = 0;//遍歷字符串,得到每一個字符。for(int x=0; x<s.length(); x++){char ch = s.charAt(x);//判斷該字符到底是屬于那種類型的if(ch>='a' && ch<='z'){smallCount++;}else if(ch>='A' && ch<='Z'){bigCount++;}else if(ch>='0' && ch<='9'){numberCount++;}}//輸出結(jié)果。System.out.println("大寫字母"+bigCount+"個");System.out.println("小寫字母"+smallCount+"個");System.out.println("數(shù)字"+numberCount+"個");} }String的轉(zhuǎn)換功能:
package cn.itcast_05;/** String的轉(zhuǎn)換功能:* byte[] getBytes():把字符串轉(zhuǎn)換為字節(jié)數(shù)組。* char[] toCharArray():把字符串轉(zhuǎn)換為字符數(shù)組。* static String valueOf(char[] chs):把字符數(shù)組轉(zhuǎn)成字符串。* static String valueOf(int i):把int類型的數(shù)據(jù)轉(zhuǎn)成字符串。* 注意:String類的valueOf方法可以把任意類型的數(shù)據(jù)轉(zhuǎn)成字符串。* String toLowerCase():把字符串轉(zhuǎn)成小寫。* String toUpperCase():把字符串轉(zhuǎn)成大寫。* String concat(String str):把字符串拼接。*/ public class StringDemo {public static void main(String[] args) {// 定義一個字符串對象String s = "JavaSE";// byte[] getBytes():把字符串轉(zhuǎn)換為字節(jié)數(shù)組。byte[] bys = s.getBytes();for (int x = 0; x < bys.length; x++) {System.out.println(bys[x]);}System.out.println("----------------");// char[] toCharArray():把字符串轉(zhuǎn)換為字符數(shù)組。char[] chs = s.toCharArray();for (int x = 0; x < chs.length; x++) {System.out.println(chs[x]);}System.out.println("----------------");// static String valueOf(char[] chs):把字符數(shù)組轉(zhuǎn)成字符串。String ss = String.valueOf(chs);System.out.println(ss);System.out.println("----------------");// static String valueOf(int i):把int類型的數(shù)據(jù)轉(zhuǎn)成字符串。int i = 100;String sss = String.valueOf(i);System.out.println(sss);System.out.println("----------------");// String toLowerCase():把字符串轉(zhuǎn)成小寫。System.out.println("toLowerCase:" + s.toLowerCase());System.out.println("s:" + s);// System.out.println("----------------");// String toUpperCase():把字符串轉(zhuǎn)成大寫。System.out.println("toUpperCase:" + s.toUpperCase());System.out.println("----------------");// String concat(String str):把字符串拼接。String s1 = "hello";String s2 = "world";String s3 = s1 + s2;String s4 = s1.concat(s2);System.out.println("s3:"+s3);System.out.println("s4:"+s4);} } package cn.itcast_05;/** 需求:把一個字符串的首字母轉(zhuǎn)成大寫,其余為小寫。(只考慮英文大小寫字母字符)* 舉例:* helloWORLD* 結(jié)果:* Helloworld* * 分析:* A:先獲取第一個字符* B:獲取除了第一個字符以外的字符* C:把A轉(zhuǎn)成大寫* D:把B轉(zhuǎn)成小寫* E:C拼接D*/ public class StringTest {public static void main(String[] args) {// 定義一個字符串String s = "helloWORLD";// 先獲取第一個字符String s1 = s.substring(0, 1);// 獲取除了第一個字符以外的字符String s2 = s.substring(1);// 把A轉(zhuǎn)成大寫String s3 = s1.toUpperCase();// 把B轉(zhuǎn)成小寫String s4 = s2.toLowerCase();// C拼接DString s5 = s3.concat(s4);System.out.println(s5);// 優(yōu)化后的代碼// 鏈?zhǔn)骄幊蘏tring result = s.substring(0, 1).toUpperCase().concat(s.substring(1).toLowerCase());System.out.println(result);} }String類的其他功能:
package cn.itcast_06;/** String類的其他功能:* * 替換功能:* String replace(char old,char new)* String replace(String old,String new)** 去除字符串兩空格 * String trim()* * 按字典順序比較兩個字符串 * int compareTo(String str)* int compareToIgnoreCase(String str)*/ public class StringDemo {public static void main(String[] args) {// 替換功能String s1 = "helloworld";String s2 = s1.replace('l', 'k');String s3 = s1.replace("owo", "ak47");System.out.println("s1:" + s1);System.out.println("s2:" + s2);System.out.println("s3:" + s3);System.out.println("---------------");// 去除字符串兩空格String s4 = " hello world ";String s5 = s4.trim();System.out.println("s4:" + s4 + "---");System.out.println("s5:" + s5 + "---");// 按字典順序比較兩個字符串String s6 = "hello";String s7 = "hello";String s8 = "abc";String s9 = "xyz";System.out.println(s6.compareTo(s7));// 0System.out.println(s6.compareTo(s8));// 7System.out.println(s6.compareTo(s9));// -16} } private final char value[];字符串會自動轉(zhuǎn)換為一個字符數(shù)組。public int compareTo(String anotherString) {//this -- s1 -- "hello"//anotherString -- s2 -- "hel"int len1 = value.length; //this.value.length--s1.toCharArray().length--5int len2 = anotherString.value.length;//s2.value.length -- s2.toCharArray().length--3int lim = Math.min(len1, len2); //Math.min(5,3); -- lim=3;char v1[] = value; //s1.toCharArray()char v2[] = anotherString.value;//char v1[] = {'h','e','l','l','o'};//char v2[] = {'h','e','l'};int k = 0;while (k < lim) {char c1 = v1[k]; //c1='h','e','l'char c2 = v2[k]; //c2='h','e','l'if (c1 != c2) {return c1 - c2;}k++;}return len1 - len2; //5-3=2;}String s1 = "hello";String s2 = "hel";System.out.println(s1.compareTo(s2)); // 2課堂練習(xí):
package cn.itcast_07;/** 需求:把數(shù)組中的數(shù)據(jù)按照指定個格式拼接成一個字符串* 舉例:* int[] arr = {1,2,3}; * 輸出結(jié)果:* "[1, 2, 3]"* 分析:* A:定義一個字符串對象,只不過內(nèi)容為空* B:先把字符串拼接一個"["* C:遍歷int數(shù)組,得到每一個元素* D:先判斷該元素是否為最后一個* 是:就直接拼接元素和"]"* 不是:就拼接元素和逗號以及空格* E:輸出拼接后的字符串* * 把代碼用功能實現(xiàn)。*/ public class StringTest2 {public static void main(String[] args) {// 前提是數(shù)組已經(jīng)存在int[] arr = { 1, 2, 3 };// 寫一個功能,實現(xiàn)結(jié)果String result = arrayToString(arr);System.out.println("最終結(jié)果是:" + result);}/** 兩個明確: 返回值類型:String 參數(shù)列表:int[] arr*/public static String arrayToString(int[] arr) {// 定義一個字符串String s = "";// 先把字符串拼接一個"["s += "[";// 遍歷int數(shù)組,得到每一個元素for (int x = 0; x < arr.length; x++) {// 先判斷該元素是否為最后一個if (x == arr.length - 1) {// 就直接拼接元素和"]"s += arr[x];s += "]";} else {// 就拼接元素和逗號以及空格s += arr[x];s += ", ";}}return s;} } package cn.itcast_07;import java.util.Scanner;/** 字符串反轉(zhuǎn)* 舉例:鍵盤錄入”abc” * 輸出結(jié)果:”cba”* * 分析:* A:鍵盤錄入一個字符串* B:定義一個新字符串* C:倒著遍歷字符串,得到每一個字符* a:length()和charAt()結(jié)合* b:把字符串轉(zhuǎn)成字符數(shù)組* D:用新字符串把每一個字符拼接起來* E:輸出新串*/ public class StringTest3 {public static void main(String[] args) {// 鍵盤錄入一個字符串Scanner sc = new Scanner(System.in);System.out.println("請輸入一個字符串:");String line = sc.nextLine();/*// 定義一個新字符串String result = "";// 把字符串轉(zhuǎn)成字符數(shù)組char[] chs = line.toCharArray();// 倒著遍歷字符串,得到每一個字符for (int x = chs.length - 1; x >= 0; x--) {// 用新字符串把每一個字符拼接起來result += chs[x];}// 輸出新串System.out.println("反轉(zhuǎn)后的結(jié)果是:" + result);*/// 改進(jìn)為功能實現(xiàn)String s = myReverse(line);System.out.println("實現(xiàn)功能后的結(jié)果是:" + s);}/** 兩個明確: 返回值類型:String 參數(shù)列表:String*/public static String myReverse(String s) {// 定義一個新字符串String result = "";// 把字符串轉(zhuǎn)成字符數(shù)組char[] chs = s.toCharArray();// 倒著遍歷字符串,得到每一個字符for (int x = chs.length - 1; x >= 0; x--) {// 用新字符串把每一個字符拼接起來result += chs[x];}return result;} }package cn.itcast_07;/** 統(tǒng)計大串中小串出現(xiàn)的次數(shù)* 舉例:* 在字符串"woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun"* 結(jié)果:* java出現(xiàn)了5次* * 分析:* 前提:是已經(jīng)知道了大串和小串。* * A:定義一個統(tǒng)計變量,初始化值是0* B:先在大串中查找一次小串第一次出現(xiàn)的位置* a:索引是-1,說明不存在了,就返回統(tǒng)計變量* b:索引不是-1,說明存在,統(tǒng)計變量++* C:把剛才的索引+小串的長度作為開始位置截取上一次的大串,返回一個新的字符串,并把該字符串的值重新賦值給大串* D:回到B*/ public class StringTest5 {public static void main(String[] args) {// 定義大串String maxString = "woaijavawozhenaijavawozhendeaijavawozhendehenaijavaxinbuxinwoaijavagun";// 定義小串String minString = "java";// 寫功能實現(xiàn)int count = getCount(maxString, minString);System.out.println("Java在大串中出現(xiàn)了:" + count + "次");}/** 兩個明確: 返回值類型:int 參數(shù)列表:兩個字符串*/public static int getCount(String maxString, String minString) {// 定義一個統(tǒng)計變量,初始化值是0int count = 0;/*// 先在大串中查找一次小串第一次出現(xiàn)的位置int index = maxString.indexOf(minString);// 索引不是-1,說明存在,統(tǒng)計變量++while (index != -1) {count++;// 把剛才的索引+小串的長度作為開始位置截取上一次的大串,返回一個新的字符串,并把該字符串的值重新賦值給大串// int startIndex = index + minString.length();// maxString = maxString.substring(startIndex);maxString = maxString.substring(index + minString.length());// 繼續(xù)查index = maxString.indexOf(minString);}*/int index;//先查,賦值,判斷while((index=maxString.indexOf(minString))!=-1){count++;maxString = maxString.substring(index + minString.length());}return count;} }總結(jié)
以上是生活随笔為你收集整理的byte数组转为string_String类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++进制转换代码_轻松实现C/C++各
- 下一篇: ca证书 csr_linux下使用ope