第一:
package com.javami.kudy.javaapi1.StringStudy;
public class StringDemo {
/*面試題:
* 1.Stirng s = "abc" String s = new String("abc"); //有什麼區別?
* 2.String s = new String("abc"); 創建了幾個對象 2個(池裏沒有)或1個(池裏有了),
* 3.String s = "a" + "b" + "c" + "d"; 創建了幾個對象? 創建了1個對象
* 4.String s1 = "a"; String s2 = "b"; String s3 = s1 + s2; s3=="ab"?
*/
public static void main(String[]args)
{
/*
* 1.String池,由於字符串操作很頻繁.java針對String對象提供了緩衝池
* s1首先會在緩衝池裏面看一下有沒有"abc" 如果沒有的.那麼把對象創建的引用返回給s1
* 當s2開始執行.會檢查緩衝池裏面有沒有.如果有的.直接把對象的地址返回給s2 .所以是相等的.。
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);
*/
/*
String s1 = "abc";
String s2 = new String(s1);
outcome : false
s2在堆內存中創建了一個對象(把地址返回來~),並且在堆內存裏面的緩衝池裏面創建一個"abc"(這個是對象裏面的一個拷貝副本)
做java的日常維護.所以那麼這兩個地址一比較.就會不相等..
*/
/*
String s1 = "a"+"b"+"c";
String s2 ="abc";
outCome = "true";
因爲java編譯器有個合併已知量的功能,在編譯階段"a"+"b"+"c" 合併爲"abc"並且保存在緩衝池裏面
所以s2在檢測緩衝池裏面.檢測到已經有了.直接把對象的引用所返回來.那麼證明了s1 s2 是同一個地址/所以比較出來爲true
*/
/*
String s1 = new String("abc");
String s2 = new String("abc");
這兩個裏面創建了多少個對象呢?
解答: 首先會在堆內存中創建一個對象.在檢測池裏面有沒有,沒有也在了緩衝池裏面創建了一個.做日常的維護
s2也會在堆內存中創建一個對象.這時候檢測到池裏面已經有了.所以就不再創建/
說百了也就是3個..
一般面試簡單的就直接說:2個就可以.
*/
/*
* String 是一個常量,是不能改變的量
* 內部實現:
* StringBuilder sb = new StringBuilder(s1);
* sb.append(s2);
* s3 = sb.toString(); //轉換回字符串
* 返回調用方法的都是創建一個對象的
*
*/
String s1 = "a";
String s2 = "b";
String s3 = s1 + s2;
System.out.println(s3=="ab"); //直接返回一個flase
}
}
第二:
package com.javami.kudy.javaapi1.StringStudy;
import java.io.UnsupportedEncodingException;
public class DecodeTest {
/*
* 解碼: 用錯誤的碼再變回來.然後用正確的編碼方式就可以
* 編碼錯誤: 沒辦法,神仙也救不了
*/
public static void main(String[]args) throws UnsupportedEncodingException
{
String str1 = "中國人"; //使用gb2312編碼
// 使用平臺的默認字符集將此 String 編碼爲 byte 序列,並將結果存儲到一個新的 byte 數組中。
byte[] newbyte = str1.getBytes();
String str2 = new String(newbyte);
System.out.println(str2);
String str3 = new String(newbyte,"iso-8859-1");
//System.out.println(str3);編碼的信息出錯
newbyte = str3.getBytes("iso-8859-1");
String str4 = new String(newbyte);
System.out.println(str4);
byte[]new1 = str1.getBytes("iso-8859-1");
//一開始編碼的信息就出錯!因爲我的計算機根本就沒有這個編碼
String s6 = new String(new1);
new1 = s6.getBytes("iso-8859-1");
String st7 = new String(new1);
System.out.println(st7);
}
}
第三:
package com.javami.kudy.javaapi1.StringStudy;
public class StringTest {
/*String 爲什麼要重寫toString()方法呢?
解答:因爲在String如果不重寫Object的toString方法.那麼直接複用父類的方法.打印出的是對象.
這顯然不是String想要得到的結果
*/
public static void main(String[]args)
{
String s1 = "kudy";
System.out.println(s1.charAt(3)); //索引是從零開始\
String s2 ="abcde";
String s3 = "aBcddddd";
System.out.println(s2.compareToIgnoreCase(s3));
String s4 = "abc";
String s5 = "def";
System.out.println((s4.concat(s5)).toString());
boolean is = s4.contains(s5);
System.out.println(is);
String fileName = "Demo.jdk";
if(fileName.endsWith(".java"))
System.out.println("您是java文件");
String s6 = "cnabcdbvklabcjdhfabcdhla";
int newint = s6.indexOf("abc",0); //注意:索引是從零開始
System.out.println(newint);
String s7 = "abc";
String s8 = new String("abc");
s8.intern();
System.out.println(s7 == s8);
String s9 = "cascascacvdfdjkgksd";
int num = s9.lastIndexOf("k",s9.length()-1);
System.out.println(num);
String s10 = "abc";
String s11 = s10.replace("abc","***");
System.out.println(s11);
String s12 = "hello.java";
String s13 = s12.replace("java","jdk" );
System.out.println(s13);
/*
* 把有逗號的內容去掉。
*/
String s14 = "ab,cd,ef";
String [] s15 = s14.split(",");
for(String num1 : s15)
System.out.print(num1);
String s16 = "abcbsbdasdas";
char[] newchar = s16.toCharArray();
for(char num2 : newchar)
System.out.print(num2);
System.out.println();
/*
* 但注意中間的是不能去掉的~~
*/
String s17 = " abc def ";
String s18 = s17.trim();
System.out.println(s18);
}
}
第四:
package com.javami.kudy.javaapi1.StringStudy;
import java.io.IOException;
public class StringUtil {
private StringUtil(){} //私有化
/* 查找子字符串在字符串中出現的位置 例:“ab”在"habjklabljhlabhjdjfla"出現的位置爲:1 6 12
* 思路:
* 1.定義一個StringBuilder容器,用於存儲位置,定義一個pos變量記住出現的位置,初始值爲0
* 2.定義一個while循環,查找子串出現的位置
* 3.調用indexOf方法,從起始位置開始查找出子串首次出現的位置
* 4.如果pos爲-1,說明沒找到,應該結束循環
* 5.將位置存入StringBuilder容器,再加一個逗號分隔
* 6.從找到的位置加上子串的長度作爲新的起始位置,
* 7.循環結束,將StringBuffer轉爲String字符串,調用split方法以逗號進行分隔,獲得了String數組
* 8.準備一個新的int數組,長度和String數組一樣,遍歷String數組,將每個元素轉爲int存入int數組
*/
public static int[]findIndexOfSubstr(String str, String substr)
{
StringBuilder sb = new StringBuilder();
int pos = 0;
while(true)
{
pos = str.indexOf(substr,pos);
if(pos ==-1)
break;
sb.append(pos+",");
pos+=substr.length(); //pos假設找ab 找到1 + 2 等於三.剛好從第三個開始找~
}
if(sb.length() == 0) //容器什麼都沒有!
return null; //一個都找不到
String s = sb.toString(); //轉換成字符串形式
String[] parts = s.split(",");
int[] arr = new int[parts.length];
for(int i=0; i<parts.length; i++)
{
arr[i] = Integer.parseInt(parts[i]);
}
return arr;
}
/*
* 設計一個方法,讀取鍵盤輸入的一行(只支持英文字母)
* 提示:System.in.read()讀取鍵盤上的一個字符
* 思路:
* 1.定義一個容器StringBuilder容器
* 2.定義一個while循環,循環讀取鍵盤.
* 3.如果讀到回車或者\n 就跳出循環
* 4.存入StringBuilfer容器中
* 5.循環結束,返回一個toString轉換爲字符串
*/
public static String readLine() throws IOException
{
StringBuilder sb = new StringBuilder();
while(true)
{
int ch = System.in.read();
if(ch == '\r') //如果回車 跳出去
continue;
if(ch == '\n') //等上面的執行完畢.再來
break;
sb.append((char)ch);
}
return sb.toString(); //轉換成字符串
}
/*
* *將給定的字節數組截取一部分,進行解碼,要求輸出完整的漢字,例如:"abc中國人"截取6個 輸出的是abc中
* 思路:
* 1.遍歷數組被截取的部分,
* 2.判斷正負數,統計負數個數爲count
* 3.判斷count的奇偶,奇數減去1,偶數不變
* 4.解碼
* 漢字是佔了2個字節?我們怎麼判斷它是漢字呢?
* 漢字的第一位字節保證是負數!第二個是正數
*
*/
public static String decode(byte[]buf, int len)
{
int count = 0;
for(int i=0; i<buf.length;i++)
if(buf[i]<0)
count++; //如果第一個字節是負數的.是代表漢字馬上來到.咱們+1
if(count % 2 == 1)
len--;
return new String(buf,0,len);
/*程序流程圖: 咱們輸入一個buf len = 5
* for 執行5次
* 因爲一個漢字是佔了2個字節.而第一個字節保證是負數
* 如果 buf[i]裏面的是負數的 count +1 = 1
* 如果 % 2 等於1代表的是: 咱們len 傳遞進來的就減去一個字節.
*
*/
}
public static String decodeBYGBK(byte[]buf,int len)
{
boolean b = false;
for(int i=0; i<buf.length; i++)
if(b)
b = false;
else if(buf[i]<0)
b = true;
if(b)
len --;
return new String(buf,0,len);
}
/*
* 查找一個字符串出現次數最多的字符 abvjdabahabballfja 出現次數最多的爲:a 6
* 思路:
* 1.調用toCharArray字符串把字符串轉換成字符數組
* 2.定義max,用於記住出現最多字符的次數,定義一個maxChar記住出現最多的字符
* 3.定義一個循環,每次循環都統計一個字符出現最多的次數,每次讓次數和max比.誰最大就換,結束條件。當長度的數組
* 爲零
* 4.定義一個count,記住本次統計字符的次數
* 5.循環遍歷數組,統計第一個字符出現的次數
* 6.讓次數和max比,如果max大,讓max記住它的次數,讓maxchar記住當前的字符
* 7.將當前的字符從數組中刪除
* ------
* 將此字符串轉換爲一個新的字符數組。
*/
public static char searchMaxChar(String str)
{
char[] buf = str.toCharArray();
int max = 0;
char maxChar = 0;
while(buf.length>0)
{
int count = 0; //個數等於0
char firstChar = buf[0];
for(char c : buf)
if(c ==firstChar)
count++;
if(count>max) //max 已經是默認記住上一個的值!
{
max = count;
maxChar = firstChar; //如果你是最多的就讓給你~~
}
//把當前的字符刪除!
buf = deleteChar(buf,firstChar,count);
}
return maxChar;
}
private static char[] deleteChar(char[] buf, char firstChar, int count)
{
char[]arr = new char[buf.length-count];
int pos = 0;
for(int i=0; i<buf.length; i++)
{
if(buf[i]==firstChar)
continue;
arr[pos++] = buf[i];
}
return arr;
}
/* 查找兩個字符串中的最大子串 jflwertyakjfa;l fjdlkwertyjf;ajf 最大子串:werty
* 1.判斷出兩個字符串的長短,找出較長的和較短的
* 2.循環遍歷較短的字符串的所有子串,由長到短遍歷
* 定義一個循環,每次遍歷一種長度的子串,將i作爲長度,i的取值: length~1
* 定義一個內層循環,具體遍歷某一種長度的子串,j做爲子串的開始位置, j取值:0~length-i
* 截取子串,substring 起始位置:j 結束位置:j+i
* 3.每次變量,都判斷子串是否爲較長的字符串的子串,如果是,直接返回
* 4.循環結束,依然沒有返回,說明沒有相同子串,返回null
*/
public static String searchMaxSubStr(String s1, String s2)
{
String longStr = s1.length() > s2.length() ? s1 : s2;
String shortStr = s1.length() > s2.length() ? s2 : s1;
for(int i=shortStr.length(); i>0; i--)
{
for(int j=0;j<shortStr.length()-i+1;j++)
{
String sub = shortStr.substring(j, j+i); //例如: abcd 放到sub裏面去
//abcd abc ab a 和它裏面的做一個對比
//當且僅當此字符串包含指定的 char 值序列時,返回 true。
if(longStr.contains(sub))
//判斷長的字符串裏面是否包含這個
return sub; //如果包含直接返回.沒有包含
}
}
return null;
}
}
----------------------
package com.javami.kudy.javaapi1.StringStudy;
public class StringUtilTest {
public static void main(String[]args) throws Exception
{
/*
String str = "dsdcvcvxvczxbedfaacff";
String substr = "y";
//遍歷a 出現的位置
int[]arr = StringUtil.findIndexOfSubstr(str, substr);
if(arr ==null)
{
System.out.println("找不到!");
return ;
}
for(int num : arr)
{
System.out.print(num+" ");
}
System.out.println();
*/
/*
* 再輸入byte的時候就結束,不斷的去讀
while(true)
{
String str2 = StringUtil.readLine();
if("bye".equals(str2))
break;
System.out.println(str2);
}
System.out.println("程序結束");
*/
byte[] buf = "abc中國人".getBytes();
//String s3 = StringUtil.decode(buf, 3);
String s3 = StringUtil.decodeBYGBK(buf, 5);
System.out.println(s3);
char maxChar = StringUtil.searchMaxChar("dasfasdfaaaaaaaasfgdagfafgsad");
System.out.println(maxChar);
String s33 = "jflwertyakjfa;l";
String s44 = "fjdlkwertyjf;ajf";
String s55 =StringUtil.searchMaxSubStr(s33,s44);
System.out.print(s55);
}
}