String类回顾
1.String类的构造方法
/**
java.lang.String类:
String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
字符串是常量;它们的值在创建之后不能更改。
字符串的底层就是一个数组,数组被final修饰,所以字符串是一个常量
private final char value[];
*/
public class Demo01String{
puublic void static void main(String[] args){
/**
String类的构造方法
String(String original);
String(byte[] bytes) 通过使用平台(操作系统)的默认字符集(ASCII)解码指定的byte数组,构造一个新的String
String(char[] value)把字符数组转换成字符串
*/
String s1 = new String("abc");
System.out.println(s1); //s1:abc
byte[] bytes = {65, 66, 67, 68, 69, 70};
String s2 = new String(bytes);
System.out.println(s2); //s2:ABCDEF
char[] chars = {'中','国','人'};
String s3 = new String(chars);
System.out.println("s3:"+s3);//s3:中国人
String s4 = "abc";
System.out.println("s4:"+s4);//s4:abc
}
}
/*
String类中常用的成员方法:
String concat(String str) 将指定字符串连接到此字符串的结尾。
boolean contains(CharSequence s) 判断字符串中是否包含指定的字符串;包含返回true,不包含返回false
boolean endsWith(String suffix) 判断字符串是否以指定的字符串结尾;是返回true,否返回false
boolean startsWith(String prefix) 判断字符串是否以指定的字符串开头;是返回true,否返回false
int indexOf(String str) 从前往后在字符串中查找另外一个字符串,找到了返回字符串的索引,找不到返回-1
注意:
如果大的字符串中有重复的被查找的字符串,找到了第一个就不在找了 "abcabcabc"==>"abc"==>0
int lastIndexOf(String str) 从后往前在字符串中查找另外一个字符串,找到了返回字符串的索引,找不到返回-1
注意:
如果大的字符串中有重复的被查找的字符串,找到了第一个就不在找了 "abcabcabc"==>"abc"==>6
String replace(CharSequence target, CharSequence replacement) 把大的字符串中所有的目标字符串,替换为新的字符串
String substring(int beginIndex) 从开始索引beginIndex截取字符串到字符串的末尾
String substring(int beginIndex, int endIndex) 从开始索引beginIndex到结束索引endIndex截取字符串;包含头不包含尾
char[] toCharArray() 将此字符串转换为一个新的字符数组。
byte[] getBytes() 把字符串转换为一个字节数组
String toUpperCase() 把字符串中所有的英文字符都转换为大写字符 "abc123你好"==>"ABC123你好"
String toLowerCase() 把字符串中所有的英文字符都转换为小写字符 "ABC123你好"==>"abc123你好"
String trim() 去掉字符串两端的空格 " abc de f "==> "abc de f"
注意:
方法的参数CharSequence是一个接口,String类实现了这个接口,所以方法的参数可以传递String对象
*/
public class Demo02StringMethod{
public static void main(String[] args){
show09();
}
/*
String trim() 去掉字符串两端的空格 "abc de f "==> "abc de f"
*/
private static void show09(){
String s = " abc de f ";
//去掉字符串两端的空格
String strim = s.trim();
System.out.println(strim); //abc de f
}
Scanner sc = new Scanner();
//请输入您的用户名
System.out.println("请输入您的用户名:");
String name = sc.nextLine();
System.out.println(name);
if("jack".equals(name.trim())){
System.out.println("用户名正确");
}
/*
String toUpperCase() 把字符串中所有的英文字符都转换为大写字符 "abc123你好"==>"ABC123你好"
String toLowerCase() 把字符串中所有的英文字符都转换为小写字符 "ABC123你好"==>"abc123你好"
*/
private static void show08(){
String str = "abc你好";
//把字符串中所有的英文字符都转换成大写字母
String upperCase = s.toUpperCase();//ABC123你好
//把所有的字符串英文字符串都转换陈小写
String lowerCase = s.toLowerCase();
System.out.println(lowerCase);//abc你好
}
/*
char[] toCharArray() 将此字符串转换为一个新的字符数组。
byte[] getBytes() 把字符串转换为一个字节数组
*/
private static void show07(){
String s = "abcde";
char[] chars = s.toCharArray();
System.out.println(Arrays.toString(chars));//[a, b, c, d, e]
byte[] bytes = s.getBytes();
System.out.println(Arrays.toString(bytes));//[97, 98, 99, 100- 101]
s= "你好";
/*
IDEA默认字符集是UTF-8:一个中文占3个字节
GBK字符集:一个中文占用2个字节
*/
System.ouut.println(Arrays.toString(s.getBytes()));//[-28, -67, -96, -27, -91, -67]
}
/*
String substring(int beginIndex) 从开始索引beginIndex截取字符串到字符串的末尾
String substring(int beginIndex, int endIndex) 从开始索引beginIndex到结束索引endIndex截取字符串;包含头不包含尾
*/
private static void show06(){
String s = "六朝何事,只因门户私计";
//对s字符串进行截取,只要门户私计
String sub1 = s.substring(7);//从7索引到末尾截取
System.out.println(sub1);//sub1: 门户私计
//只要六朝何事
String sub2 = s.substring(0, 4);
System.out.pringln(sub2);//sub2: 六朝何事
}
/*
String replace(CharSequence target, CharSequence replacement) 把大的字符串中所有的目标字符串,替换为新的字符串
此方法可以用于过滤关键字
*/
private static void show05(){
String s = "abc123你好abc123我好abc123大家好!";
//把s字符串中的"123"字符串替换为"@_@"
String replace = s.replace("123", "@_@");
System.out.println(replace);//abc@_@你好abc@_@我好abc@_@大家好!
}
/*
int indexOf(String str) 从前往后在字符串中查找另外一个字符串,找到了返回字符串的索引,找不到返回-1
注意:
如果大的字符串中有重复的被查找的字符串,找到了第一个就不在找了 "abcabcabc"==>"abc"==>0
int lastIndexOf(String str) 从后往前在字符串中查找另外一个字符串,找到了返回字符串的索引,找不到返回-1
注意:
如果大的字符串中有重复的被查找的字符串,找到了第一个就不在找了 "abcabcabc"==>"abc"==>6
*/
private static void show04() {
String s = "abc123你好abc123我好abc123大家好!";
//在s这个字符串中查找"123"字符串的索引
int index1 = s.indexOf("123");
System.out.println("index1:"+index1);//index1:3
int index2 = s.lastIndexOf("123");
System.out.println("index2:"+index2);//index2:19
//在s这个字符串中查找"我好"字符串的索引
int index3 = s.indexOf("我好");
System.out.println("index3:"+index3);//index3:14
int index4 = s.lastIndexOf("我好");
System.out.println("index4:"+index4);//index4:14
//在s这个字符串中查找"刘德华"字符串的索引
int index5 = s.indexOf("刘德华");
System.out.println("index5:"+index5);//index5:-1
int index6 = s.lastIndexOf("刘德华");
System.out.println("index6:"+index6);//index6:-1
}
/*
boolean endsWith(String suffix) 判断字符串是否以指定的字符串结尾;是返回true,否返回false
boolean startsWith(String prefix) 判断字符串是否以指定的字符串开头;是返回true,否返回false
*/
private static void show03() {
String s1 = "Hello.java";
//判断s1字符串是否以.java结尾
boolean b1 = s1.endsWith(".java");
System.out.println("b1:"+b1);//b1:true
//判断s1字符串是否以.class结尾
boolean b2 = s1.endsWith(".class");
System.out.println("b2:"+b2);//b2:false
String s2 = "HelloWorld";
//判断s2字符串是否以"Hello"开头
boolean b3 = s2.startsWith("Hello");
System.out.println("b3:"+b3);//b3:true
//判断s2字符串是否以"hello"开头
boolean b4 = s2.startsWith("hello");
System.out.println("b4:"+b4);//b4:false
}
/*
boolean contains(CharSequence s) 判断字符串中是否包含指定的字符串;包含返回true,不包含返回false
boolean contains(String s) 判断字符串中是否包含指定的字符串;包含返回true,不包含返回false
*/
private static void show02() {
String s = "你好,我好,大家好,才是真的好,广州好迪!hello";
//在字符串中判断有没有"大家好"
boolean b1 = s.contains("大家好");
System.out.println("b1:"+b1);//b1:true
//在字符串中判断有没有"Hello"
boolean b2 = s.contains("Hello");
System.out.println("b2:"+b2);//b2:false
}
/*
String concat(String str) 将指定字符串连接到此字符串的结尾。
注意:
concat方法参数只能传递String类型,只能连接字符串(效率高)
+可以连接任意数据类型的值
*/
private static void show01() {
String s1 = "你好";
String s2 = "我好";
String concat = s1.concat(s2);
System.out.println(concat);//你好我好
String s3 = s1+s2+12+true+1.1;
System.out.println(s3);//你好我好12true1.1
}
}
文章版权声明:除非注明,否则均为彭超的博客原创文章,转载或复制请以超链接形式并注明出处。
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。