1.String类
package com.guada.string;
public class Demo1_String {
public static void main(String[] args) {
String str = "abc"; //''abc''可以被看作是一个字符串对象
str="def"; //当把"def"赋值给str的时候原来的abc就变成了垃圾
System.out.println(str); //String类重写了toString()方法返回的是该对象本身
}
}
2.String类常用的构造方法
package com.guada.string;
public class Demo2_String {
/**
*String类 常用的构造方法
*public String():空构造;
*public String(byte [] bytes):把字节数组转换成字符串
*public String(byte [] bytes,int index,int length):把字节数组的一部分转成字符串
*public String(char[] value):把字符数组转成字符串
*public String(char [] value,int index,int count):把字符数组的一部分转成字符串
*public String(String original):把字符串常量 值转成字符串
*/
public static void main(String[] args) {
String str = new String(); //空参构造
System.out.println(str);
byte [] be = {97,98,99,100,101,102};
String str1 = new String(be); //把字节数组转换成字符串
System.out.println(str1);
String str2 = new String(be,1,3);
System.out.println(str2); //把字节数组的一部分转换成字符串
char [] ch = {'y','k','i','l','i','g'};
String str3 = new String(ch); //把字符数组转换成字符串
System.out.println(str3);
String str4 = new String(ch,1,3); //把字符数组的一部分转化成字符串
System.out.println(str4);
String s = "lkijm" ;
String str5 = new String(s); //把字符串常量转化成字符串
System.out.println(str5);
}
}
3.String类中字符串的比较
package com.guada.string;
public class Demo1_String {
public static void main(String[] args) {
// Demo1_String();
//Demo2_String();
//Demo3_String();
//Demo4_String();
Demo5_String();
}
public static void Demo5_String() {
String s1 = "ab";
String s2 = "abc";
String s3 = s1+"c";
System.out.println(s2 == s3); //false
System.out.println(s2.equals(s3)); //true
System.out.println(s3.equals(s2)); //true
}
public static void Demo4_String() {
String s1 = "a" + "b" +"c";
String s2 = "abc";
System.out.println(s1 == s2); //true java中有常量优化机制
System.out.println(s1.equals(s2)); //true
}
public static void Demo3_String() {
String s1 = new String("abc");
String s2 = "abc";
System.out.println(s1 == s2); //false s1记录的是堆内存中的地址值,s2记录的是常量池中的地址
System.out.println(s1.equals(s2)); //true
}
public static void Demo2_String() {
String str = new String("abc"); //创建了几次对象,,两次,一个对象在堆内存中,一个在常量池中
}
public static void Demo1_String(){
String str1 = "abc";
String str2 = "abc";
System.out.println(str1 == str2); //常量池当中如果没有这个字符串对应的对象就创建一个,如果有就直接用
System.out.println(str1.equals(str2)); //true
}
}
4.String类的判断功能
package com.guada.string;
public class Demo3_String {
/**
* boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
* boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
* boolean contains(String str):判断大字符串中是否包含小字符串
* boolean startsWith(String str):判断字符串是否以某个指定的字符串开头
* boolean endsWith(String str):判断字符串是否以某个指定的字符串结尾
* boolean isEmpty():判断字符串是否为空
*/
public static void main(String[] args) {
String str1 = "guada";
String str2 = "guada";
String str3 = "GuaDa";
String str4 = "Gua";
String str5 = "Da";
String str6 = "";
//Demo1_String(str1, str2, str3);
//Demo2_String(str1, str2, str3);
//Demo3_String(str1, str2, str3, str4);
//Demo4_String(str1, str3, str4);
//Demo5_String(str1, str3, str5);
Demo6_String(str1, str2, str3, str4, str5, str6);
}
public static void Demo6_String(String str1, String str2, String str3,String str4, String str5, String str6) {
System.out.println(str1.isEmpty());
System.out.println(str2.isEmpty());
System.out.println(str3.isEmpty());
System.out.println(str4.isEmpty());
System.out.println(str5.isEmpty());
System.out.println(str6.isEmpty());
}
public static void Demo5_String(String str1, String str3, String str5) {
System.out.println(str1.endsWith(str5));
System.out.println(str3.endsWith(str5));
}
public static void Demo4_String(String str1, String str3, String str4) {
System.out.println(str1.startsWith(str4));
System.out.println(str3.startsWith(str4));
}
public static void Demo3_String(String str1, String str2, String str3,String str4) {
System.out.println(str1.contains(str2));
System.out.println(str1.contains(str4));
System.out.println(str3.contains(str4));
}
public static void Demo2_String(String str1, String str2, String str3) {
System.out.println(str1.equalsIgnoreCase(str2)); //true
System.out.println(str1.equalsIgnoreCase(str3)); //true
}
public static void Demo1_String(String str1, String str2, String str3) {
System.out.println(str1.equals(str2)); //true
System.out.println(str1.equals(str3)); //false
}
}
""和null的区别:
""是字符串常量,同时也是一个String类的对象,既然是对象当然可以调用String类中的方法
null是空常量,不能调用任何的方法,否则会出现空指针异常,null常量可以给任意的引用数据类型赋值
4.模拟用户登录
package com.guada.string;
import java.util.Scanner;
public class Demo5_String {
/**
* 模拟用户登录
* 1.需要键盘键入,默认用户名密码为admin,Scanner类
* 2.控制键盘输入的次数有for循环,用if判定密码是否正确
*/
public static void main(String[] args) {
System.out.println("请输入用户名:");
Scanner sc = new Scanner(System.in);
String userName = sc.nextLine();
System.out.println("请输入密码:");
Scanner scanner = new Scanner(System.in);
String password = scanner.nextLine();
if("admin".equals(userName)&&"admin".equals(password)) {
System.out.println("成功登录");
}
else {
System.out.println("登录失败");
}
}
}
package com.guada.string;
import java.util.Scanner;
public class Demo12_String {
/**
* Reverse string
* @param a string
* return the inverse of the string
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一行字符串");
String line = sc.nextLine();
String s = "";
char [] arr =line.toCharArray();
for (int i = arr.length-1; i >=0 ; i--) {
s = s+arr[i];
}
System.out.println(s);
}
}