1.1 String概述
1.2 构造方法
package com.study.demo.stringdemo;
/**
* @Auther: lds
* @Date: 2018/11/23 14:41
* @Description:
*/
public class StringDemo01 {
public static void main(String[] args) {
//方式 1
//String(String original):把字符串数据封装成字符串对象
String s1 = new String("hello");
System.out.println("s1:" + s1);
System.out.println("---------");
//方式 2
//String(char[] value):把字符数组的数据封装成字符串对象
char[] chs = {'h', 'e', 'l', 'l', 'o'};
String s2 = new String(chs);
System.out.println("s2:" + s2);
System.out.println("---------");
//方式 3
//String(char[] value, int index, int count):把字符数组中的一部分数据封装成字符串对象
//String s3 = new String(chs,0,chs.length);
String s3 = new String(chs, 1, 3);
System.out.println("s3:" + s3);
System.out.println("---------");
//方式 4
String s4 = "hello";
System.out.println("s4:" + s4);
}
}
1.3 String 的特点
String s = new String(“hello”);
直接赋值创建字符串对象
String s = “hello”;
区别是什么?(面试的经典问题,String s = new String(“hello”);是几个对象?)
package com.study.demo.stringdemo;
/**
* @Auther: lds
* @Date: 2018/11/23 14:47
* @Description:
*/
public class StringDemo02 {
public static void main(String[] args) {
String s1 = new String("hello");
String s2 = "hello";
System.out.println("s1:" + s1);
System.out.println("s2:" + s2);
System.out.println("s1==s2:" + (s1 == s2)); //false
String s3 = "hello";
System.out.println("s1==s3:" + (s1 == s3)); //false
System.out.println("s2==s3:" + (s2 == s3)); //true
}
}
代码很直观的反映了,s1和s2不相等,这里所说的相等,不仅仅是数值上面的相等,还有它们的物理地址是不是相等的!
1.4 常用方法
package com.study.demo.stringdemo;
/**
* @Auther: lds
* @Date: 2018/11/23 15:00
* @Description:
*/
public class StringDemo03 {
public static void main(String[] args) {
//创建字符串对象
String s1 = "hello";
String s2 = "hello";
String s3 = "Hello";
//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.equalsIgnoreCase(s3));
System.out.println("-----------");
//boolean startsWith(String str):判断字符串对象是否以指定的 str 开头
System.out.println(s1.startsWith("he"));
System.out.println(s1.startsWith("ll"));
}
}
例:模拟用户登录案例,给三次机会,并提示还有几次
package com.study.demo.stringdemo;
import java.util.Scanner;
/**
* @Auther: lds
* @Date: 2018/11/23 15:31
* @Description:
*/
public class StringDemo04 {
public static void main(String[] args) {
//定义两个字符串对象,用于存储已经存在的用户名和密码
String username = "admin";
String password = "admin";
//给三次机会,用 for 循环实现
for (int i = 0; i < 3; i++) {
//键盘录入用户名和密码
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String name = sc.nextLine();
System.out.println("请输入密码:");
String pwd = sc.nextLine();
//拿键盘录入的用户名和密码和已经存在的用户名和密码进行比较
if (username.equals(name) && password.equals(pwd)) {
System.out.println("登录成功");
break;
} else {
if ((2 - i) == 0) {
System.out.println("用户名和密码被锁定,请与管理员联系!");
} else {
System.out.println("登录失败,你还有" + (2 - i) + "次机会!");
}
}
}
}
}
package com.study.demo.stringdemo;
/**
* @Auther: lds
* @Date: 2018/11/23 16:07
* @Description:
*/
public class StringDemo05 {
public static void main(String[] args) {
//创建字符串对象
String s = "helloworld";
//int length():获取字符串的长度,其实也就是字符个数
System.out.println(s.length());
System.out.println("--------");
//char charAt(int index):获取指定索引处的字符
System.out.println(s.charAt(0));
System.out.println(s.charAt(1));
System.out.println("--------");
//int indexOf(String str):获取 str 在字符串对象中第一次出现的索引,没有返回-1
System.out.println(s.indexOf("l"));
System.out.println(s.indexOf("owo"));
System.out.println(s.indexOf("ak"));
System.out.println("--------");
//String substring(int start):从 start 开始截取字符串到字符串结尾
System.out.println(s.substring(0));
System.out.println(s.substring(5));
System.out.println("--------");
//String substring(int start,int end):从 start 开始,到 end 结束截取字符串。包括 start,不包括 end
System.out.println(s.substring(0, s.length()));
System.out.println(s.substring(3, 8));
}
}
package com.study.demo.stringdemo;
import java.util.Scanner;
/**
* @Auther: lds
* @Date: 2018/11/23 16:11
* @Description:
*/
public class StringDemo06 {
public static void main(String[] args) {
//键盘录入一个字符串
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String s = sc.nextLine();
//截取首字母
String s1 = s.substring(0, 1);
//截取除了首字母以外的字符串
String s2 = s.substring(1);
//2 转大写+3 转小写
String s3 = s1.toUpperCase()+s2.toLowerCase();
//输出即可
System.out.println("s3:"+s3);
}
}
package com.study.demo.stringdemo;
/**
* @Auther: lds
* @Date: 2018/11/23 16:19
* @Description:
*/
public class StringDemo07 {
public static void main(String[] args) {
//创建字符串对象
String s1 = "helloworld";
String s2 = " helloworld ";
String s3 = " hello world ";
System.out.println("---" + s1 + "---");
System.out.println("---" + s1.trim() + "---");
System.out.println("---" + s2 + "---");
System.out.println("---" + s2.trim() + "---");
System.out.println("---" + s3 + "---");
System.out.println("---" + s3.trim() + "---");
System.out.println("-------------------");
//String[] split(String str)
//创建字符串对象
String s4 = "aa,bb,cc";
String[] strArray = s4.split(",");
for (int x = 0; x < strArray.length; x++) {
System.out.println(strArray[x]);
}
}
}
2.1 StringBuilder概述
我们如果对字符串进行拼接操作,每次拼接,都会构建一个新的 String 对象,既耗时,又浪费空间。而 StringBuilder 就可以解决这个问题。
2.2 StringBuilder和String的区别
2.3 构造方法
package com.study.demo.stringbuilder;
/**
* @Auther: lds
* @Date: 2018/11/23 16:30
* @Description:
*/
public class StringBuilderDemo01 {
public static void main(String[] args) {
//创建对象
StringBuilder builder = new StringBuilder();
System.out.println("builder:" + builder);
System.out.println("builder.capacity():" + builder.capacity());
System.out.println("builder.length():" + builder.length());
}
}
2.4 常用方法
package com.study.demo.stringbuilder;
/**
* @Auther: lds
* @Date: 2018/11/23 16:35
* @Description:
*/
public class StringBuilderDemo02 {
public static void main(String[] args) {
//创建对象
StringBuilder builder = new StringBuilder();
//public StringBuilder append(任意类型)
//StringBuilder builder2 = builder.append("hello");
/*
System.out.println("builder:"+builder);
System.out.println("builder2:"+builder2);
System.out.println(builder == builder2); //true
*/
/*
builder.append("hello");
builder.append("world");
builder.append(true);
builder.append(100);
*/
//链式编程
builder.append("hello").append("world").append(true).append(100);
System.out.println("builder:" + builder);
//public StringBuilder reverse()
builder.reverse();
System.out.println("builder:" + builder);
}
}
package com.study.demo.stringbuilder;
/**
* @Auther: lds
* @Date: 2018/11/23 16:39
* @Description:
*/
public class StringBuilderDemo03 {
public static void main(String[] args) {
//StringBuilder -- String
/*
StringBuilder builder = new StringBuilder();
builder.append("hello").append("world");
String s = builder.toString();
System.out.println(s);
*/
//String -- StringBuilder
String s = "helloworld";
StringBuilder builder = new StringBuilder(s);
System.out.println(builder);
}
}
例:判断对称字符串,"abc"不是对称字符串,"aba"、"abba"、"aaa"、"mnanm"是对称字符串
package com.study.demo.stringbuilder;
import java.util.Scanner;
/**
* @Auther: lds
* @Date: 2018/11/23 17:19
* @Description:
*/
public class StringBuilderDemo04 {
public static void main(String[] args) {
//1:键盘录入一个字符串
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String s = sc.nextLine();
//3:调用方法
boolean b = isSymmetry(s);
//4:输出结果
System.out.println("b:" + b);
}
/*
* 2:写方法实现判断一个字符串是否是对称字符串
* 两个明确:
* 返回值类型:boolean
* 参数列表:String s
*/
public static boolean isSymmetry(String s) {
//把字符串反转,和反转前的字符串进行比较,如果内容相同,就说明是对称字符串
StringBuilder builder = new StringBuilder(s);
builder.reverse();
String result = builder.toString();
return result.equals(s);
}
}