public class StringDemo1 {
public static void main(String[] args) {
/*
* 直接指向常量池
*/
String s1 = "abc";
/*
* new String():相当于创建了一个空字符串
*/
String s2 = new String();
System.out.println("s2:" + s2);
System.out.println("".equals(s2));//true
/*
* new String(String original):
* 该对象字符串初始化。
*/
String s3 = new String("abc");
System.out.println(s1.equals(s3));
/*
* new String(byte[] bytes)
* 把传入的字节数组转换为字符串
*/
byte[] bytes = {97,98,99,100,101};
String s4 = new String(bytes);
System.out.println(s4);//abcde
/*
* new String(byte[] bytes,int offset,int length)
* 传入一个byte类型的数组,把下标从offset开始,length个字节
* 转换为字符串
*/
String s5 = new String(bytes, 2, 2);
System.out.println(s5);//cd
/*
* new String(char[] value):
* 把字符数组转换为字符串
*/
char[] value = {'端','午','节','快','乐'};
String s6 = new String(value);
System.out.println(s6);//端午节快乐
/*
* new String(char[] value,int offset,int count)
* 把char数组中的下标从offset开始,count个字符转换为字符串
*/
String s7 = new String(value, 3, 2);
System.out.println(s7);//快乐
/*
* new String(int[] codePoints,int offset,int count)
* codePoints:字符对应的Unicode编码
* 把int数组中的编码按照Unicode编码表,从下标从offset开始count个
* 个数字对应的字符转换为字符串。
*/
int[] codePoints = {97,98,99,100,101};
String s8 = new String(codePoints, 1, 2);
System.out.println(s8);//bc
String s = "abcdefg";
/*
* char | charAt(int index):
* 按照下标查找字符。
* 获得下标为index的字符
*/
char ch = s.charAt(3);
System.out.println(ch);//d
/*
* int | codePointAt(int index)
* 按照下标查找字符的编码号
*/
int codePoint = s.codePointAt(3);
System.out.println(codePoint);//100
/*
* String | concat(String str):基本不用。一般用StringBuffer
* 连接参数字符串,返回一个新的字符串。
* String对象一旦创建不可改变。
*/
String str = s.concat("hijk");
System.out.println(s);//abcdefg
System.out.println(str);//abcdefghijk
/*
* boolean | contains(Charsequence str):包含
* 判断字符串是否包含参数字符串
*/
boolean bool = s.contains("abc");
System.out.println(bool);//true
/*
* boolean | endsWith(String suffix)
* 是否以参数字符串suffix为结尾
*/
bool = s.endsWith("efg");
System.out.println(bool);//true
/*
* boolean | startsWith(String prefix)
* 是否以参数字符串prefix为开头
*/
bool = s.startsWith("abc");
System.out.println(bool);//true
/*
* byte[] | getBytes()
* 获得该字符串的字节数组
*/
byte[] bytes = "明天放假啦!".getBytes();
/*for (int i = 0; i < bytes.length; i++) {
System.out.println(bytes[i]);
}*/
String str2 = new String(bytes);
System.out.println(str2);
/*
* 1>int | indexOf(int ch)
* int | indexOf(char ch)
* 从左向右,获得ch首次出现的下标。如果不存在,返回-1
* 2>int | indexOf(Strinig str)
* 从左向右,获得str首次出现的位置,如果不存在返回-1
* 3>int | indexOf(int ch, int fromIndex)
* int | indexOf(char ch, int fromIndex)
* 从fromIndex开始(包含fromIndex),从左向右,获得ch首次出现的下标。
* 如果不存在,返回-1
* 4>int | indexOf(String str, int fromIndex)
* 从fromIndex开始(包含fromIndex),从左向右,获得str首次出现的下标。
* 如果不存在,返回-1
*/
String s2 = "abcbcbcdef";
int index = s2.indexOf('e');
System.out.println(index);//8
index = s2.indexOf("bc");
System.out.println(index);//1
index = s2.indexOf('b', 3);
System.out.println(index);//3
index = s2.indexOf("bc", 3);
System.out.println(index);//3
/*
*
* 1>int | lastIndexOf(int ch)
* int | lastIndexOf(char ch)
* 从右向左,获得ch首次出现的下标,如果不存在返回-1
* 2>int | lastIndexOf(String str)
* 从右向左,获得str首次出现的下标,如果不存在返回-1
* 3>int | lastIndexOf(int ch,int fromIndex):
* int | lastIndexOf(char ch,int fromIndex):
* 从fromIndex开始(包含fromIndex),从右向左,获得ch首次出现的位置。
* 如果不存在返回-1
* 4>int | lastIndexOf(String str,int fromIndex):
* 从fromIndex开始(包含fromIndex),从右向左,获得str首次出现的位置。
* 如果不存在返回-1
*/
//String s2 = "abcbcbcdef";
index = s2.lastIndexOf('b');
System.out.println(index);//5
index = s2.lastIndexOf("bc");
System.out.println(index);//5
index = s2.lastIndexOf('b',4);
System.out.println(index);//3
index = s2.lastIndexOf("bc", 3);
System.out.println(index);//3
/*
* boolean | isEmpty();
* 判断是否是空字符串
*/
bool = s.isEmpty();
System.out.println(bool);//false
System.out.println("".isEmpty());//true
/*
*
* int| length():获得字符串中字符的个数
* 字符的个数
*/
int len = s.length();
System.out.println(len);//7
/*
*
* String | replace(char oldChar,char newChar)
* 把所有的老字符替换成新字符,返回一个新的字符串
*/
// s2 = "abcbcbcdef"
str = s2.replace('b', 'B');
System.out.println(str);//aBcBcBcdef
/*
*
* String[] |split(String regex):
* 按参数字符串进行分割,获得字符串数组。
* .在正则表达式中有特殊的含义,代表了所有字符。
* 如果想使用.来对字符串进行切割的时候,需要转义,
* \\.
*
*/
str = "锄禾日当午.谁知盘中餐";
String[] strs = str.split("\\.");
for (int i = 0; i < strs.length; i++) {
System.out.println(strs[i]);
}
/*
*
* 1>String | substring(int beginIndex)
* 截取字符串从下标为beginIndex(包含beginIndex)开始到结尾。
* 返回一个新的字符串
* 2>String | substring(int beginIndex,int endIndex)
* 截取字符串从下标beginIndex 开始到 endIndex结束
* 返回一个新的字符串.
* 保头不保尾。
* 截取到的字符串的字符 个数为 endIndex - beginIndex
*/
//abcdefg
str = s.substring(3);
System.out.println(str);//defg
str = s.substring(3, 5);
System.out.println(str);//de
/*
*
* toCharArray():
* 转换为一个char类型的数组
*/
char[] chs = s.toCharArray();
for (int i = 0; i < chs.length; i++) {
System.out.println(chs[i]);
}
System.out.println("---------------------");
/*
*
* String | toLowerCase():全部转换为小写
* String | toUpperCase():全部转换为大写
*/
str = "abcDEF".toLowerCase();
System.out.println(str);
str = "abCDEfg".toUpperCase();
System.out.println(str);
/*
*
* boolean | equalsIgnoreCase(String str)
* 忽略大小写比较字符串是否相同。
*/
bool = "abcdEFG".equalsIgnoreCase("AbcdEfg");
System.out.println(bool);
/*
* String | trim():
* 去掉字符串两边的空格
*/
str = " 哈 哈 ";
System.out.println(str.length());//10
String s3 = str.trim();
System.out.println(s3);
System.out.println(s3.length());
/*
* String | valueOf(所有类型):
* 可以把参数转换为String类型字符串
* valueOf(char[] data,int offset,int count)
* 把char数组从下标为offset开始,count个字符转换为字符串
*/
s3 = String.valueOf(true);
System.out.println(s3);
char[] data = {'我','们','是','学','生'};
s3 = String.valueOf(data, 3, 2);
System.out.println(s3);
/*
* compareTo方法为 自然排序:比较对象的大小关系
* 在字符串中也成为字典排序
*
* int | compareTo(String anotherString)
* 正数:当前对象大于参数对象
* 负数:当前对象小于参数对象
* 0:两个对象相等
*
*/
s2 = "abcdef";
s3 = "abcdef";
int num = s2.compareTo(s3);
System.out.println(num);//0
}
}