------ Java教育訓練、Android教育訓練、iOS教育訓練、.Net教育訓練、期待與您交流! ------- String類:
package cn.fu._01string;
/**
* String StringBuffer StringBuilder
* String類的特點:
* 字元串對象一旦被初始化就不會改變.
*/
public class StringDemo1 {
public static void main(String[] args) {
String s = "abc";
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
s = "nba";
System.out.println("s = "+s);
//"abc"字元串對象并沒有改變,隻是引用變量s指向了新建立的字元串對象"nba".
System.out.println(s1==s2);
//字元串建立的時候,有一個字元串常量池,s1建立後,"abc"放入其中
//s2建立時,隻是把s1的"abc"引用賦給了s2,故s1==s2.
System.out.println(s1==s3);
//s1是一個對象,s2是一個對象,不是同一個對象位址不同,是以false;
System.out.println(s1.equals(s3));
//String類複寫了Object中的equals方法,建立了Sring類最積極的判斷字元串對象是否相同的依據,
//隻比較字元串内容,不比較位址.
}
}
運作結果: s = nba
true
false
true
package cn.fu._01string;
/**
* String類的構造函數
* 構造函數:String(bytes[] bytes)
* 構造函數:String(char[] value)
* 構造函數:String(char[] value,int offset,int count)
*/
public class StringDemo2 {
public static void main(String[] args) {
StringDemo2();
}
public static void StringDemo2(){
String s = new String();//等效于String s = "";不等效于String s = null;
byte[] arr = {65,66,67,68};
String s1 = new String(arr);
System.out.println("s1 = " + s1);//s1 = ABCD
char[] ch = {'w','a','p','q','x'};
String s2 = new String(ch);
String s3 = new String(ch,1,3);
System.out.println("s2 = "+s2);//s2 = wapqx
System.out.println("s3 = "+s3);//s3 = apq
}
}
運作結果: s1 = ABCD
s2 = wapqx
s3 = apq
擷取:
package cn.fu._01string;
/*
*String類部分方法
*1.擷取
*擷取字元串中字元的長度(個數)
*int length();
*
*根據位置擷取字元
*char charAt(int index);
*
*根據字元擷取字元串中的位置
*int indexOf(int ch);
*
*從指定位置開始查找ch第一出現的位置
*int indexOf(int ch,int formIndex);
*int indexOf(String str);
*int indexof(String str,int formIndex);
*
*根據字元串擷取在字元串中倒數第一次出現的位置
*int lastIndexOf(int ch);
*int lastIndexOf(int ch,int fromIndex);
*int lastIndexOf(String str);
*int lastIndexOf(String str,int forIndex)
*
*擷取字元串中的一部分字元串,也叫字串
*String substring(int beginIndex);
*String substring(int beginIndex,int endIndex);
*/
public class StringDemo3 {
public static void main(String[] args) {
method();
}
public static void method(){
String s = new String("alskjdlajsdlfjsdgj");
// 012345678901234567
System.out.println(s.length());//18
char c = s.charAt(2);
System.out.println(c);//s
System.out.println(s.indexOf('k'));//3
System.out.println(s.indexOf('j',5));//8
System.out.println(s.indexOf("js"));//8
System.out.println(s.indexOf("js",9));//13
System.out.println(s.lastIndexOf('s'));//14
System.out.println(s.lastIndexOf("dl"));//10
System.out.println(s.lastIndexOf("dl", 9));//5
String sub1 = s.substring(5);
String sub2 = s.substring(1,3);
System.out.println(sub1);//dlajsdlfjsdgj
System.out.println(sub2);//ls
//(1,3)標頭不包尾
//如果查找不到,傳回-1
}
}
運作結果: 18
s
3
8
8
13
14
10
5
dlajsdlfjsdgj
ls
轉換:
package cn.fu._01string;
/**
* 2.轉換
* 将字元串變成字元數組(字元串切割)
* String[] Split(String regex);涉及正規表達式.
*
* 将字元串變成字元數組
* char[] toCharArray();
*
* 将字元串變成位元組數組
* byte[] getBytes();
*
* 将字元串字母轉變成大小寫
* String toUpperCase();大寫
* String toLowerCase();小寫
*
* 将字元串中的内容進行替換
* String replace(char oldCh,char newCh);
* String replace(String s1,String s2);
*
* 去除字元串兩端的空格
* String trim();
*
* 将字元串經行連接配接
* String concat(String str);
*
* 其他類型資料轉換成字元串
*/
public class StringDemo4 {
public static void main(String[] args) {
method();
}
public static void method(){
String s = "張三,李四,王五";
String[] arr = s.split(",");
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
System.out.println("----------------------------");
String s1 = "張三.李四.王五";
String[] arr1 = s1.split("\\.");
//P.S. 點在正規表達式中是特殊符号,需要轉義
for (int j = 0; j < arr1.length; j++) {
System.out.println(arr1[j]);
}
System.out.println("----------------------------");
String s2 = "abcABC123";
char[] arr2 = s2.toCharArray();
for (int i = 0; i < arr2.length; i++) {
System.out.println(arr2[i]);
}
System.out.println("----------------------------");
byte[] arr3 = s2.getBytes();
for (int i = 0; i < arr3.length; i++) {
System.out.println(arr3[i]);
}
System.out.println("----------------------------");
String s4 = s2.toUpperCase();//轉大寫
String s5 = s2.toLowerCase();//轉小寫
System.out.println(s4);
System.out.println(s5);
System.out.println("----------------------------");
String s6 = "abcABCabcABC";
String s7 = s6.replace('a', 'd');//所有的'a'都會被替換
String s8 = s6.replace("abc", "def");
String s9 = s6.replace('2', '3');
System.out.println(s7);
System.out.println(s8);
System.out.println(s9);//找不到要替換的字元串,傳回原字元串
System.out.println(s9==s6);//找不到時,s9引用指向s6
System.out.println("----------------------------");
String s10 = " lkjsladkjla sdasd ";
System.out.println(s10.trim());
System.out.println("----------------------------");
System.out.println(s6.concat("game"));//連接配接
//P.S. concat()效果與"+"連接配接符一緻
System.out.println("----------------------------");
System.out.println(String.valueOf(5)+1);//将基本資料類型轉換為String
System.out.println(""+4+1);
}
}
輸出結果: 張三
李四
王五
----------------------------
張三
李四
王五
----------------------------
a
b
c
A
B
C
1
2
3
----------------------------
97
98
99
65
66
67
49
50
51
----------------------------
ABCABC123
abcabc123
----------------------------
dbcABCdbcABC
defABCdefABC
abcABCabcABC
true
----------------------------
lkjsladkjla sdasd
----------------------------
abcABCabcABCgame
----------------------------
51
41
判斷:
package cn.fu._01string;
/**
* 3.判斷
* 判斷兩個字元串内容是否相同
* boolean equals(Object obj);
* boolean equalsIgnoreCase(String str);忽略大小寫比較字元串内容.
*
* 判斷字元串中是否儲存指定字元串
* booleac contains(String str);
*
* 判斷字元串是否以指定字元串開頭,是否以指定字元串結尾
* boolean startWith(String str);
* boolean endsWith(String str);
*
* 4.比較
* Int compareTo(String str);
* 如果參數字元串等于此字元,則傳回0;
* 如果此字元串按照字典順序小魚字元串參數,則傳回一個小于0的值;
* 如果此字元串按照字典順序大于字元串參數,則傳回一個大于0的值;
*
* 5.傳回字元串對象的規範化表達形式
* String intern();
* 當調用intern方法時,如果池已經包含一個等于此String對象的字元串(用
* equals(Object obj)方法确定),則傳回池中的字元串,否則,将此Stirng對象
* 添加到池中,并傳回此String對象的引用.
*/
public class StringDemo5 {
public static void main(String[] args) {
method();
}
public static void method(){
String s = "abc";
System.out.println(s.equals("abc"));//true
System.out.println(s.equalsIgnoreCase("ABC"));//true
System.out.println(s.contains("ab"));//true
String s1 = "獨孤九劍.劍譜";
System.out.println(s1.startsWith("獨孤"));//true
System.out.println(s1.endsWith(".劍譜"));//true
System.out.println("----------------------------");
String s3 = "abc";
String s4 = "aqz";
System.out.println(s3.compareTo(s4));
//P.S."abc"和"aqz"兩個字元串比較,'a'和'a'相等,'b'-'q'=-15,
//'c'和'z'就沒有比較的必要,直接傳回-15,即s3-s4從左到右一個一個對比.
System.out.println("----------------------------");
String s5 = new String("123");
String s6 = s5.intern();
System.out.println(s6);
}
}
輸出結果: true
true
true
true
true
----------------------------
-15
----------------------------
123
練習:
package cn.fu._02stringtest;
/**
* 練習
* 1、給定一個字元串數組,按照字典順序進行從小到大的排序。
* {"nba","abc","cba","zz","qq","haha"}
*
* 思路:
* 1.字元串數組排序 選擇排序或者冒泡排序
* 2.周遊擷取
* 3.字典排序,可以使用compareTo()方法
*/
public class StringTest1 {
public static void main(String[] args) {
String[] str = {"nba","abc","cba","zz","qq","haha"};
printStr(str);//{nba,abc,cba,zz,qq,haha}
sort(str);
printStr(str);//{abc,cba,haha,nba,qq,zz}
}
public static void sort(String[] str){
for (int i = 0; i < str.length-1; i++) {
for (int j = i+1; j < str.length; j++) {
if(str[i].compareTo(str[j])>0){
String s = "";
s = str[i];
str[i] = str[j];
str[j] = s;
}
}
}
}
public static void printStr(String[] str){
System.out.print("[");
for (int i = 0; i < str.length; i++) {
if(i<str.length-1){
System.out.print(str[i]+",");
}else{
System.out.println(str[i]+"]");
}
}
}
}
輸出結果: [nba,abc,cba,zz,qq,haha]
[abc,cba,haha,nba,qq,zz]
package cn.fu._02stringtest;
/**
* 2、一個子串在整串中出現的次數 "nbaernbatynbauinbaopnba"
*
* 思路:
* 1.需要計數器count;
* 2.可以采用indexOf(String str,int fromIndex);來尋找子串
* 3.可以用length()來擷取字串的長度,然後疊加到fromIndex中
* 4.找不到就-1結束;
*
*/
public class StringTest2 {
public static void main(String[] args) {
String s1 = "nbaernbatynbauinbaopnba";
String s2 = "ba";
int a = foundString(s1,s2);
System.out.println(a);
}
public static int foundString(String s1,String s2){
int count = 0;
int index = 0;
while(index!=-1){
index = s1.indexOf(s2,index);
if(index!=-1){
index = index + s2.length();
count++;
}else{
break;
}
}
return count;
}
}
輸出結果: 5
package cn.fu._02stringtest;
/**
* 3、兩個字元串中最大相同的子串
* "qwerabcdtyuiop"
* "xcabcdvbn"
*
* 思路:
* 1.先比較兩個字元串的長度,确定包含與被包含的關系;
* 2.先判斷小的字元串,是否被大的包含;
* 3.小的字元串長度減一,從左自右組合後接着判斷包含關系,直到長度為0停止.
* xcabcdvb...
* 01234567 |a-b,即s1.constains(s2.subString(a,b+1))是否為true
* 0123456 1234567 |0-6,1-7
* 012345 123456 234567 |0-5,1-6,2-7
* 01234 12345 23456 34567 |0-4,1-5,2-6,3-7
* ... |0-3,1-4,2-5,3-6,4-7...類似于乘法表
*/
public class StringTest3 {
public static void main(String[] args) {
String s1 = "qwerabcdtyuiop";
String s2 = "xcabcdvbn";
String ss1;
String ss2;
if(s1.length()>s2.length()){
ss1 = s1;
ss2 = s2;
}else{
ss1 = s2;
ss2 = s1;
}
System.out.println(ss1);
System.out.println(ss2);
String minsub = method(ss1,ss2);
System.out.println(minsub);
}
public static String method(String ss1,String ss2){
for(int i = 0 ; i< ss2.length();i++){
for(int a = 0 ,b = ss2.length()-i;b!=ss2.length()+1;a++,b++){
String sub = ss2.substring(a,b);
if(ss1.contains(sub)){
return sub;
}
}
}
return null;
}
}
輸出結果: qwerabcdtyuiop
xcabcdvbn
abcd
package cn.fu._02stringtest;
/**
* 4、模拟一個trim功能方法,去除字元串兩端的空白。
* 思路:
* 1.定義角标,判斷一個字元串頭尾是否為空格;
* 2,是空格,前面++,後面--;
* 3,用substring截取中間非空格部分
*/
public class StringTest4 {
public static void main(String[] args) {
String s = " sdfa d ";
System.out.println("-"+myTrim(s)+"-");
}
public static String myTrim(String s){
int start = 0;
int end = s.length()-1;
while(start<=end&&s.charAt(start)==' '){//避免全部為空格時出現異常
start++;
}
while(start<=end&&s.charAt(end)==' '){
end--;
}
return s.substring(start, end+1);
}
}
輸出結果: -sdfa d-