一、字符串
简单理解为:由一个签子将若干字符串起来的串儿,叫字符串
官方理解:
字符串是由多个字符组成的一串数据(字符序列)
字符串可以看成是字符数组**********
通过观察API发现:
1、String代表的是字符串。属于java.lang包下,所以在使用的时候不需要导包
2、String类代表字符串。Java程序中的所有字符串文字(例如"abc")都被实现为此类的实例(对象)
3、字符串不变;它们的值在创建后不能被更改
字符串是常量,一旦被赋值,字符串本身不能被修改
构造方法:
public String()
public String(byte[] bytes)
public String(byte[] bytes,int offset,int length)
public String(char[] value)
public String(char[] value,int offset,int count)
public String(String original)
-------------------------------------------------------------------------------------------------------------------
public class StringDemo1{
public static void main(String[] args){
//public String()
String s = new String();
System.out.println(s);//String类中重写toString()方法
//查看字符串的长度
//public int length()返回此字符串的长度
System.out.println("字符串的长度为:" + s.length());//如果字符串中没有字符,返回0
----------------------------------------------------------------------------------------------------------------------
//public String(byte[] bytes)//根据一个字节数组创建出一个字符串对象
byte[] bytes = {97,98,100,101};
String s2 = new String(bytes);
System.out.println("s2:" +s2);
System.out.println("字符串s2的长度为:"+s2.length());
--------------------------------------------------------------------------------------------------------------------------
//public String(byte[] bytes,int index,int length)
//将字节数组中的一部分转化成字符串
String s3 = new String(bytes,1,3);
System.out.println("s3:" + s3);
Ssytem.out.println("字符串s3的长度为:"+ s3.length());
------------------------------------------------------------------------------------------------------------------
//public String(char[] value,int index,int length)
//将字符数组的一部分转成字符串
String s5 = new String(c,4,5);
System.out.println("s5:" + s5);
System.out.println("字符串s5的长度为:" + s5.length());
-------------------------------------------------------------------------------------------------------------------
//StringIndexOutOfBoundException
// String s6 = new String(c,4,10);
// System.out.println("s6:" + s6);
// System.out.println("字符串s6的长度为:" + s6.length());
--------------------------------------------------------------------------------------------------------------------
//public String(String original)
String s7 = "你好";
String s8 = new String(s7);
System.out.println("s8:" + s8);
System.out.println("字符串s8的长度为:" + s8.length());
}
}
------------------------------------------------------------------------------------------------------------------------
字符串是常量,他的值在创建后不能被改变
String s= "hello";
s += "world";
请问s的值是什么?
public class StringDemo2{
public static void main(String[] args){
String s = "hello";
s += "world";
System.out.println(s);
}
}
---------------------------------------------------------------------------------------------------------------------------
String s = new String("hello")和String s = "hello";的区别?
字符串比较之看程序写结果
字符串拼接之看程序写结果
注意事项:
1、==比较引用数据类型的时候,比较的是地址值
2、String类中使用equals方法比较的是字符串的值,因为String类中重写了equals方法
public class StringDemo3{
public static void main(String[] args){
String s1 = new String("hello");
String s2 = "hello";
Syetrm.out.println(s1 == s2);
System.out.println(s1.equals(s2));//true
}
}
---------------------------------------------------------------------------------------------------------------------------
看程序写结果:
public class StringDemo4{
public static void main(String[] args){
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);//false
System.out.println(s1.equals(s2));//true
String s3 = new String("hello");
String s4 = "hello";
System.out.println(s3 ==s4);//false
System.out.println(s3.equals(s4));//true
String s5 = "hello";
String s6 = "hello";
System.out.println(s5==s6);//true
System.out.println(s5.equals(s6));//true
}
}
------------------------------------------------------------------------------------------------------------------------
1、字符串如果是变量相加,是先在常量池中开辟空间,然后再做拼接
2、字符串如果是常量相加,是先相加,然后再去常量池中去找,如果找到了,就返回,如果没有找到就开辟新的空间,存储拼接后的值
public class StringDemo5{
public static void main(String[] args){
String s1 = "hello";
String s2 = "world";
String s3 = "hellowrold";
String s4 = "hello" + "world";
System.out.println(s3==s4);//true
String s5 = s1 + s2;
System.out.println(s3==s5);false
System.out.println(s3==(s1 + s2));//false
System.out.println(s3.equals(s1 + s2));//true
}
}
-----------------------------------------------------------------------------------------------------------------------
String类的判断功能:
boolean equals(Object obj)
boolean equalsIgnoreCase(String str)
boolean contains(String str)
boolean startsWith(String str)
boolean endsWith(String str)
boolean isEmpty()
public class StringDemo6{
public static void main(String[] args){
String s1 = "helloworld";
String s2 = "Helloworld";
String s3 = "HelloWorld";
//boolean equals(Object obj)比较字符串中的内容是否相同,区分大小写比较的
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println("*******************************************");
//boolean equalsIgnoreCase(String str)比较字符串中的内容是否相同,忽略大小写
System.out.println(s1.equalsIgnoreCase(s2));
System.out.println(s1.equalsIgnoerCase(s3));
System.out.println("*************************************************************");
//boolean contains(String str)
//判断大的字符串中是否包含小的字符串,如果包含,返回true,反之返回false
//区分大小写
System.out.println(s1.contains("Hello"));//false
System.out.println(s1.contains("hel"));
System.out.println(s1.contains("owo"));
System.out.println("*******************************************************");
//boolean startsWith(String str)
//测试此字符串是否以指定字符串开头
//区分大小写
System.out.println(s1.startsWith("hel"));
System.out.println(s1.startsWith("Hel"));//false
System.out.println(s1.startsWith("hel34"));//false
System.out.println("**************************************************************");
//boolean endsWith(String str)
//测试此字符串是否以指定字符串结尾
//区分大小写
System.out.println(s1.endsWith("rld"));
System.out.println(s1.endsWith("rlD"));
System.out.println("*************************************************************");
//boolean iaEmpty()
//判断字符串是否是空字符串
Syetem.out.println(s1.isEmpty());
System.out.println("***********************************************************");
String s4 = " ";
String s5 = null;
System.out.println(s4==s5);
System.out.println(s5==s4);
System.out.println(s4.equals(s5));//false
// System.out.println(s5.equals(s4));//NullPointerException
注意:今后在做字符比较内容的时候,很容易出现NullPointerException空指针异常
前面调用方法的变量有可能是null值
所以今后,为了避免出现这样的问题,如果是变量1.equals(变量2)的时候,在做equals之前判断一下变量1是不是null
如果一个变量1与字符串常量1做equals作比较的时候,把字符串常量1放在前面调用方法,因为我们说过单独一个字符也是一个String对象
需求:比较s6的值是否和s7的值一样
String s6 = null;
String s7 = "hello";
if(s6!=null){
if(s6.equals(s7)){
System.out.println("是一样的");
}
}else {
System.out.println("s6是null值");
}
//需求2:判断s6的值是否是hello
//if(s6.equals("hello")){
// System.out.println("是一样的");
// }
if("hello".equals(s6)){
System.out.println("是一样的");
}else {
System.out.println("s6的值不是hello");
}
}
}
------------------------------------------------------------------------------------------------------------------------
String类的获取功能:
int length()
char charAt(int index)
int indexOf(int ch)
int idnexOf(String str)
int indexOf(int ch,int fromIndex)
int idnexOf(String str,int fromIndex)
String substring(int start)
String substring(int start,int end)
public class StringDemo7{
public static void main(String[] args){
String s = "helloworld";
//int length()获取字符串的长度
System.out.println("字符串s的长度为:" + s.length());
System.out.printlen("**************************************************************");
//char charAt(int index)返回指定的索引处的字符
// 0 <= indec<=length()-1
System.out.println(s.charAt(4));
System.out.println(s.charAt(0));
//StringIndexOutOfBoundsException
// System.out.println(s.charAt(100));
System.out.ptintln(s.charAt(9));
--------------------------------------------------------------------------------------------------------------
//public int indexOf(int ch)返回指定字符第一次出现的字符串内的索引
//需求:获取o在在字符串中第一次出现的位置
System.out.println(s.indexOf('o'));
//如果此字符串没有此类字符,则返回 -1
System.out.println(s.indecOf97);
-----------------------------------------------------------------------------------------------
public int indexOf(String str))返回指定子字符串第一次出现的字符串内的索引
//owo
System.out.println(s.indexOf("owo"));
//如果大串中不存在小串,返回 -1
System.out.println(s.indexOf("owe"));
System.out.println(s.indexOf("qwer"));
-----------------------------------------------------------------------------------------------------
//public int indexOf(int ch,int fromIndex)
//返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索
//如果找到了,返回的是字符在整个字符串中的索引
System.out.println(s.indexOf("|",4));//8
System.out.println(s.indexOf("|",1000));//-1
System.out.println(s.indexOf("p",4));//-1
System.out.println(s.indexOf("p",1000));// -1
System.out.println("=================================");
//int indexOf(String str,int fromIndex)
System.out.println("================================");
// helloworld
//String substring(int start)从指定位置处截取字符串,包括开始截取的位置,截取到末尾
//如果给的索引值不存在,报错
System.out.println(s.substring(3));//loworld
//System.out.println(s.substring(100));//StringIndexOutOfBoundsException
System.out.println("========================================");
//String substring(int start,int end)
//截取字符串的一部分出来
//截取的串从start位置开始截取,截取到end-1的位置结束
//左闭右开[,)含头不含尾
System.out.println(s.substring(5,8));//wor
//System.out.println(s.substring(1,20));//StringIndexOutOfBoundsException
}
}
-------------------------------------------------------------------------------------------------------------------------------
String 类的转换功能:
byte[] getBytes()
char[] toCharArray()
static String valueOf(char[] chs)
static String valueOf(int i)
String toLowerCase()
String toUpperCase()
String concat(String str)
public String[] split(String regex) *******
public class StringDemo8{
public static void main(String[] args){
String s = "HelloWorLD";
//public byte[] getBytes()适用平台默认的字符集将此String编码为字节序列,将结果存储到 新的字节数组中。
//将字符串转成字节数组
byete[] bytes = s.getBytes();
// System.out.println(bytes);
for(int i = 0;i<bytes.length;i++){
System.out.println(bytes[i]);
}
//72 101 108 108 111 87 111 114 76 68
System.out.println();
System.out.println("===================================");
//char[] toCharArray()
//将字符串转成字符数组
char[] chars = s.toCharArray();
for(int i = 0;i<chars.length;i++){
System.out.println(cahrs[i]);
}
//增强for循环,后面上到集合的时候会讲解
// for(char c : chars){
// System.out.println(c);
// }
System.out.println("=====================================");
// static String valueOf(char[] chs)
//将字符数组转成字符串
String s1 = String.valueOf(chars);
System.out.println(s1);
System.out.println("==================================");
//static String valueOf(int i)数据库
//将int类型的数据转成字符串
String s2 = String.valueOf(100);// 100-->"100"
System.out.println(s2);//100
System.out.println("=================================");
//String toLowerCase()
//将字符串中的内容全部转小写
String s3 = s.toLowerCase();
System.out.println(s3);//helloworld
system.out.println("====================================");
//String toUpperCase()
String s4 = s.toUpperCase();
System.out.println(s4);//HELLOWORLD
System.out.println("===========================================");
//String concat(String str)
//将小括号中的str的字符串拼接到达字符串的后面
String s5 = s.concat("hadoop");
System.out.println(s5);
System.out.println("====================================");
//public String[] split(String s)
String s6 = "hello wrold hello java world";
//需求:求出字符串中的每一个单词
String[] Strings = s6.split(" ");
for (int i = 0; i<strings.length;i++){
System.out.println(string[i]);
}
}
}
-----------------------------------------------------------------------------------------------------------------------
String类的其它功能:
替换功能
String repalce(char oid,char new)
String replace(String old,String new)
去除字符串两空格
String trim()
按字典顺序比较两个字符串
int compareTo(String str)
int compareToIgnoreCase(String str)
public class StringDemo9{
public static void main(String[] args){
String s = "hellodadadadadafafkhhhfgkkfkada";
//String replace(char old,char new)
//将新的字符串替换字符串中所指定的所有字符,并返回一个替换后的字符串
String s1 = s.replace('I','q');
System.out.println(s);
System.out.println(s1);
System.out.println("==================================");
//String replace(String old,String new)
//将字符串中的小串用新的小串替换,返回一个替换后的字符串
String s2 = s.replace("fgk","===");
System.out.println(s);
System.out.println(s2);
System.out.println();
String s3 = s.replace("fgk","@@@@");
System.out.println(s);
System.out.println(s3);
System.out.println();
//如果被替换的字符串不存在会是什么情况呢?返回的使原本的字符串
String s4 = s.replace("qwer", "LOL");
System.out.println(s);
System.out.println(s4);
System.out.println("=======================================");
//String trim()去除字符串两边的若干个空格
String s 5 = " hello world ";
System.out.println(s5);
System.out.println(s5.trim());
System.out.println("================================");
//int compareTo(String str)//比较字符串是否相同,如果相同返回0
String s6 = "hello";//h的ASCLL码值是104
String s7 = "hello";
String s8 = "abc";//a的ASCLL码的值是97
String s9 = "qwe"//q的ASCLL码的值是113
System.out.println(s6.compareTo(s7));//0
System.out.println(s6.compareTo(s8));//7
System.out.println(s6.compareTo(s9));//-9
String s10 = "hel";
String.out.println(s6.compareTo(s10));//2
}
}
--------------------------------------------------------------------------------------------------------------
String类中CompareTo的源码分析:
String s6 = "hello"; // h的ASCII码值是 104
String s9 = "qwe"; // q的ASCII码值是 113
System.out.println(s6.compareTo(s9)); // -9
public int compareTo(String anotherString) {
/*
this -- s6 -- "hello"
anotherString -- s9 -- "qwe"
*/
int len1 = value.length; // 5
int len2 = anotherString.value.length; // 3
int lim = Math.min(len1, len2); // 3
char v1[] = value; // "hello" --> char[]
char v2[] = anotherString.value; // "qwe" --> char[]
int k = 0;
while (k < lim) { // 3
char c1 = v1[k]; // h
char c2 = v2[k]; // q
if (c1 != c2) {
return c1 - c2; // 104 - 113 = -9
}
k++;
}
return len1 - len2;
}
================================================================
String s6 = "hello";
String s10 = "hel";
System.out.println(s6.compareTo(s10));
public int compareTo(String anotherString) {
/*
this -- s6 -- "hello"
anotherString -- s10 -- "hel"
*/
int len1 = value.length; // 5
int len2 = anotherString.value.length; // 3
int lim = Math.min(len1, len2); // 3
char v1[] = value; // "hello" --> char[]
char v2[] = anotherString.value; // "hel" --> char[]
int k = 0;
while (k < lim) { // k的值:0,1,2,3
char c1 = v1[k]; // h,e,l
char c2 = v2[k]; // h,e,l
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2; // 5 - 3 = 2
}