package cn.itcast_01;
/*
- 字符串:就是由多個字符組成的一串數(shù)據(jù)。也可以看成是一個字符數(shù)組桩皿。
- 通過查看API股缸,我們可以知道
A:字符串字面值"abc"也可以看成是一個字符串對象耐朴。
B:字符串是常量借卧,一旦被賦值,就不能被改變筛峭。
- 構(gòu)造方法:
public String():空構(gòu)造
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("--------------------------");
//s1:
//s1.length():0
//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());
}
}
2.字符串的特點:一旦被賦值影晓,就不能改變镰吵。
但是引用可以改變
package cn.itcast_02;
/*
-
字符串的特點:一旦被賦值,就不能改變俯艰。
*/
public class StringDemo {
public static void main(String[] args) {
String s = "hello";
s += "world";
System.out.println("s:" + s); // helloworld
}
}
圖解:
String s = new String(“hello”)和 String s = “hello”;的區(qū)別?
String字面值對象和構(gòu)造方法創(chuàng)建對象的區(qū)別
package cn.itcast_02;
/*
String s = new String(“hello”)和String s = “hello”;的區(qū)別?
有捡遍。前者會創(chuàng)建2個對象,后者創(chuàng)建1個對象竹握。
==:比較引用類型比較的是地址值是否相同
-
equals:比較引用類型默認也是比較地址值是否相同画株,而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);// false System.out.println(s1.equals(s2));// true
}
}
圖解:
String s5 = "hello";
String s6 = "hello";
System.out.println(s5 == s6);// 字符串字面量谓传,直接從內(nèi)存找蜈项,所以true
System.out.println(s5.equals(s6));// 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);// false。字符串如果是變量相加功偿,先開空間盆佣,再拼接。
System.out.println(s3.equals((s1 + s2)));// trueSystem.out.println(s3 == "hello" + "world");//true械荷。字符串如果是常量相加共耍,是先加,然后在常量池找吨瞎,如果有就直接返回痹兜,否則,就創(chuàng)建颤诀。 System.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 = "";//對象存在熟尉,所以可以調(diào)方法
String s = null;//對象不存在归露,不能調(diào)方法
*/
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("-----------------------");
// 練習: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());
}
}
String類的獲取功能
package cn.itcast_04;
/*
String類的獲取功能
int length():獲取字符串的長度。
char charAt(int index):獲取指定索引位置的字符
int indexOf(int ch):返回指定字符在此字符串中第一次出現(xiàn)處的索引往果。
為什么這里是int類型疆液,而不是char類型?
原因是:'a'和97其實都可以代表'a'。如果里面寫char,就不能寫數(shù)字97了
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):從指定位置開始截取字符串,默認到末尾。
-
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());//10 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)); // -1 System.out.println("indexOf:" + s.indexOf('l', 40)); // -1 System.out.println("----------------------"); // 自己練習:int indexOf(String str,int // fromIndex):返回指定字符串在此字符串中從指定位置后第一次出現(xiàn)處的索引。 // String substring(int start):從指定位置開始截取字符串,默認到末尾搜囱。包含start這個索引 System.out.println("substring:" + s.substring(5)); System.out.println("substring:" + s.substring(0)); System.out.println("----------------------"); // String substring(int start,intend):從指定位置開始到指定位置結(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";
for (int x = 0; x < s.length(); x++) {
System.out.println(s.charAt(x));
}
}
}
統(tǒng)計大寫字母,小寫字母蜀肘,數(shù)字在字符串中的個數(shù)
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é)果萝玷。
-
練習:把給定字符串的方式,改進為鍵盤錄入字符串的方式昆婿。
*/
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); //判斷該字符到底是屬于那種類型的仓蛆,char類型會轉(zhuǎn)成int類型 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);
}
}
把一個字符串的首字母轉(zhuǎn)成大寫傻唾,其余為小寫。(只考慮英文大小寫字母字符)
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拼接D
String s5 = s3.concat(s4);
System.out.println(s5);
// 優(yōu)化后的代碼
// 鏈式編程
String 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));// 0 System.out.println(s6.compareTo(s8));// 7 System.out.println(s6.compareTo(s9));// -16
}
}
把數(shù)組中的數(shù)據(jù)按照指定個格式拼接成一個字符串
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;
}
}
字符串反轉(zhuǎn)
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 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;
}
}
統(tǒng)計大串中小串出現(xiàn)的次數(shù)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)計變量,初始化值是0
int count = 0;
int index;
//先查拂到,賦值痪署,判斷
while((index=maxString.indexOf(minString))!=-1){
count++;
maxString = maxString.substring(index + minString.length());
}
return count;
}
}