字符串学习总结
构造器
- 字符串是一种类,引用类型 字符串常量是一种定义的方式。
- 字符串需要有其他形式的构造器,比如可以通过字符的数组,字节组,io流中的数据创建等等。
String()
初始化一个新创建的 String 对象,使其表示一个空字符序列。
String(byte[] bytes)
通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
String(byte[] bytes, int offset, int length)
通过使用平台的默认字符集解码指定的 byte 子数组,构造一个新的 String。
String(char[] value)
分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。
String(char[] value, int offset, int count)
分配一个新的 String,它包含取自字符数组参数一个子数组的字符。
public class StringConstructor{
public static void main(String[] args) {
String s = new String();//alt+ enter,前面的代码补全
System.out.println(s); //sout输入
String s1 = "";
byte[] bs = {97, 98, 99, 100};
//通过字节数组创建字符串
String s2 = new String(bs);
System.out.println(s2);
//通过字节数组创建字符串,offset就是索引的开始位置,length就是索引的长度。
String s3 = new String(bs, 1, 2);
System.out.println(s3);
char[] cs = {'a', 'd', 'f', 's', 'r'};
String s4 = new String(cs);
System.out.println(s4);
//通过字符数组创建字符串,offset就是索引的开始位置,
//length就是截取的长度。
String s5 = new String(cs, 2, 2);
System.out.println(s5);
//通过字符串的常量创建字符串的对象。
String s6 = new String("hello");
Systm.out.println(s6);
}
}
空字符串和null的比较
空字符串是有效的引用,有地址的,只不过是内容的长度是0;
null这个引用是空引用,不能使用。使用一定会报空指针的错误。
引用数据类型默认是null.在使用是一定要判断是否为空
public class KongZiFuChuanHenullBiJiao {
public static void main(String[] args) {
String s2 = "abc";
//null表示的是空的引用,不能使用
String s1 = null;
}
}
==和equals()的区别
==
常用 比较基本类型,比较值是否相同。
也可 比较引用类型,比较地址值是否相同。
equals():
常用 比较引用类型,默认比较的是地址是否相同。
对于一般的引用类型都会重写该方法,一个类具体比较的是什么我们就得分析源码。
*String s1 = new String(“abc”); 和 String s2 = “abc”; 的区别
String s1 = new String(“abc”);
String s2 = “abc”;
有什么区别:
new String(“abc”);
如果在字符串的常量池中没有abc,就会在堆中创建一个对象,同时又在字符串常量池中也创建一个对象,然后用堆中的对象来引用字符串常量池中也的对象。实则创建了2个对象。
若new String(“abc”); 之前有了 =“abc”;
如果在字符串的常量池中有abc,就会在堆中创建一个对象,直接引用已经存在的字符串常量池中的那个对象。
/**
* 1.字符串概述
* String str = "abc";
* 字符串是类,是引用的数据类型。和基本数据类型的字符型char不同。
* String类代表字符串。java程序中的所有字符串字面值(如 “abc”)都作为此类的
* 实例实现。常量:他们的值在创建之后不能更改。
-----------------------------------------------------
* 1.字符串常量是共享的。
* 2.字符串不能变化,如果我们在字符串上做拼接,其实是产生一个新的字符串。
* 只是在str指向时,从新指向了。
*/
public class ZiFuChuan1 { //3
public static void main(String[] args) {
//定义一个字符类型的常量,这个常量是这个对象“da”,字符串的常量
//需要使用 “ ” 来引起来。
String str = "da";
str += "hei";
System.out.println(str);
}
}
上面一些小的知识点讲完了,下面对为空做案例
package String2;
/**
* equals()的重写。快捷键 ctrl+o
*/
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public boolean equals(Object obj) {
boolean flag = false;
if (obj instanceof Person) {
Person p = (Person) obj;
//就是在这里判空 this.name != null 防止空指针异常
if (this.age == p.age && this.name != null &&
this.name.equals(p.name)) {
flag = true;
}
}
return flag;
}
}
上面片段截取
//就是在这里判空 this.name != null 防止空指针异常
if (this.age == p.age && this.name != null &&
this.name.equals(p.name)) {
flag = true;
}
String API方法的学习
1、字符串的判断方法
/**
*字符串的判断方法:
* endsWith(String suffix)
* 测试此字符串是否以指定的后缀结束。
* boolean equals(Object anObject)
* 将此字符串与指定的对象比较。
* boolean equalsIgnoreCase(String anotherString)
* 将此 String 与另一个 String 比较,不考虑大小写。
* isEmpty()
* 当且仅当 length() 为 0 时返回 true。
* endsWith(String suffix)
* 测试此字符串是否以指定的后缀结束。
* boolean equals(Object anObject)
* 将此字符串与指定的对象比较。
* boolean equalsIgnoreCase(String anotherString)
* 将此 String 与另一个 String 比较,不考虑大小写。
*/
public class FangFa1 {
public static void main(String[] args) {
String s ="helloworld";
//判断一个字符串是否以一个字符串结尾
boolean world = s.endsWith("rld");
System.out.println(world);
String s1 = new String("HElloworld");
//判断两个字符串的值是否相等
boolean a1 = s1.equals(s);
System.out.println(a1);
//判断两个字符的大小写判断是否相等。合适用在验证码上
boolean a2 = s.equalsIgnoreCase(s1);
System.out.println(a2);
//判断一个字符串是否包含一个字符串
boolean hello = s.contains("world");
System.out.println(hello);
//判断一个字符串是否以某一个字符串为开头,是否是从指定索引位置开始
boolean a3 = s.startsWith("hello", 1);
System.out.println(a3);
//判断一个字符串是否是空串
boolean a4 =s.isEmpty();
System.out.println(a4);
boolean a5 = "".isEmpty();
System.out.println(a5);
//判断一个字符串是否是空字符串,最好把空字符串放在前面,防止空指针异常。
boolean a6 = "".equals(s);
System.out.println(a6);
}
}
2、字符串的获取功能
/**
* 字符串的获取功能
* int length() :返回字符串的长度
* char charAt(int index) :返回指定索引处的char值
* int indexOf(int ch) :返回指定字符在此字符串中第一次出现的索引
* int indexOf(String str) :返回指定子字符串在此字符串中第一次出现的索引。
*/
public class HuQuShang1 { //获取上7
public static void main(String[] args) {
String s = "helloworld";
//获取一个字符串的长度
int length = s.length();
System.out.println(length);
//根据索引号或者字符,去获取他们的字符或所在位置,注意,不要越界
char c=s.charAt(6);
System.out.println(c);
//获得ascii字符在字符串中第一次出现的位置
//101 =e
int i = s.indexOf(101);
System.out.println(i);
String a = "d";
int hashcode = a.hashCode();
System.out.println(hashcode);
//获得一个字符串在母字符串中第一次出现的位置索引
int ii = s.indexOf("el");
System.out.println(ii);
}
}
这里有一道测试题
/**
* 计算一个字符串中大小写字母还有数字的数量
* srfehg86sbetr7yh6s7583rtdujb7nSDJLGFKJuj35drysjn736SDJGFLK81r6yuj73n98
* 1、遍历这个字符串
* 2.判断字符串是否是大写,小写,数字
* 3,对每一个做累加
*/
public class StringTest1 {
public static void main(String[] args) {
String s = "srfehg86sbetr7yh6s7583rtdujb7nSDJLGFKJ" +
"uj35drysjn736SDJGFLK81r6yuj73n98";
int num = 0;
int upper = 0;
int lower = 0;
for(int i =0;i<s.length();i++){
//获得字符串
char c = s.charAt(i);//返回指定索引处的char值
int asc = c;//字符转换成数字
if (asc>=48 && asc<=57){
num++;
}
if (asc>=65 && asc<=90){
upper++;
}
if (asc>=97 && asc<=122){
lower++;
}
}
System.out.println("数字字符的数量:"+num);
System.out.println("大写字母的数量:"+upper);
System.out.println("小写字母的数量:"+lower);
}
}
3、字符串的获取方法
import java.util.Arrays;
/**
* int indexOf(String str, int fromIndex)
* 返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始.
* String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个
* 子字符串
* String substring(int beginIndex,int endIndex) 返回一个新的字符串,
* 它是此字符串的一个子字符串
* byte[] getBytes()
* 使用平台的默认字符集将此 String 编码为 byte 序列,
* 并将结果存储到一个新的 byte 数组中。
* char[] toCharArray()
* 将此字符串转换为一个新的字符数组。
*
*
*static String valueOf(boolean b)
* 返回 boolean 参数的字符串表示形式。
* static String valueOf(char c)
* 返回 char 参数的字符串表示形式。
* static String valueOf(char[] data)
* 返回 char 数组参数的字符串表示形式。
*
*
* String toString()
* 返回此对象本身(它已经是一个字符串!)。
* String toUpperCase()
* 使用默认语言环境的规则将此 String 中的所有字符都转换为大写。
* String toUpperCase(Locale locale)
* 使用给定 Locale 的规则将此 String 中的所有字符都转换为大写。
* String trim()
* 返回字符串的副本,忽略前导空白和尾部空白。
*
* String concat(String str)
* 将指定字符串连接到此字符串的结尾。
*/
public class FangFa2 { //8
public static void main(String[] args) {
String s ="helloworlsd";
//从某一个索引开始向后数,第一次出现的子字符串的索引
int a1 = s.indexOf("w",1);
System.out.println(a1);
//截取指定的索引之后的字符串,包含索引处的字符
String substring = s.substring ( 5);
System.out.println(substring);
//截取指定的开始索引和结束索引之间的字符串,包头不包尾
String s1 = s.substring(6,9);
System.out.println(s1);
String s2 = "abcde";
//获得这个字符串对应的字符的数组
byte[] bytes = s2.getBytes();
System.out.println(bytes);
System.out.println(Arrays.toString(bytes));//转字符串形式
//把字符串转换成字符分开的数组
char[] chars = s2.toCharArray();
System.out.println(Arrays.toString(chars));
int i=10;
String s4 =i+"";//常用
//valueOf不常用
String s5 = String.valueOf(s4);
System.out.println(s5);
//转大写
String s3 = s2.toUpperCase();
System.out.println(s3);
//转小写
System.out.println(s3.toLowerCase());
//连接字符串,了解就行
String concat = s2.concat(s3);
System.out.println(concat);
/** 这些不是太实用
* static String valueOf(boolean b)
* 返回 boolean 参数的字符串表示形式。
* static String valueOf(char c)
* 返回 char 参数的字符串表示形式。
* static String valueOf(char[] data)
* 返回 char 数组参数的字符串表示形式。
* static String valueOf(char[] data, int offset, int count)
* 返回 char 数组参数的特定子数组的字符串表示形式。
* static String valueOf(double d)
* 返回 double 参数的字符串表示形式。
* static String valueOf(float f)
* 返回 float 参数的字符串表示形式。
* static String valueOf(int i)
* 返回 int 参数的字符串表示形式。
* static String valueOf(long l)
* 返回 long 参数的字符串表示形式。
* static String valueOf(Object obj)
* 返回 Object 参数的字符串表示形式。
*/
}
}
例题:从控制台输入一段字符。然后把首字母变成大写,其余部分变成小写
import java.util.Scanner;
/**
* 从控制台输入一段字符。然后把首字母变成大写,其余部分变成小写。
*/
public class Test1 {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
//允许连续输入进行转换
while(true){
String s1 = s.nextLine();
if(s1 == null| s1.isEmpty()){
System.out.println("输入的字符串不合法,请重新输入");
continue;
}
if("exit".equals(s1)){
break;
}
//获得到首字符
String firstStr = s1.substring(0,1);
//获得第二个字符后面的
String restStr = s1.substring(1);
String s2 = firstStr.toUpperCase();
String s3 = restStr.toLowerCase();
System.out.println("首字母:"+firstStr);
System.out.println("剩余的字符:"+restStr);
System.out.println(s2+s3);
}
}
}
4、不常用方法
/**
* String split(String regex) 根据给定正则表达式的匹配拆分此字符串
* String replace(char oldChar,char newChar)
* 返回一个新的字符串,用新字符替换老字符.它是通过用
* newChar 替换此字符串中出现的所有 oldChar 得到的。
* String replace(CharSequence target, CharSequence replacement)
* 使用指定的字面值替换序列,替换此字符串所有匹配字面值目标序列的子字符串。
* String trim() 返回字符串的副本,忽略前导空白和尾部空白。
*/
public class FangFa3 { //8
public static void main(String[] args) {
String s = "aaaa-bbbb-aaaa-dddd";
//把一个字符串按着某种字符拆分成数组
String[] split = s.split("\\-");
for(int i=0; i<split.length;i++){
System.out.println(split[i]);
}
String s1 = "HelloWorld";
//把字符串中的一个字符替换成另一个字符
String replace = s1.replace('l','x');
System.out.println(replace);
//把字符串中的一个子字符替换成另一个子字符
String replace1 = s.replace("aaaa","中国");
System.out.println(replace1);
//忽略前导空白和尾部空白
String s3 = " abc ";
String trim = s3.trim();
System.out.println("|"+trim+"|");
}
}
例题:找出下面字符串中li的数量
/**
* 需求: 找出下面字符串中li的数量
*/
public class CountNumLi {
public static void main(String[] args) {
String s = "lisedgtfsdglisdgsfdlisdf";
int count = 0;
//获取到第一个li的index
int index = s.indexOf("li");
while(index != -1){
//计数器累加
count++;
//查找下一次的索引,并且更新索引,下一次要去掉li两个字符的计数
index = s.indexOf("li",index+2);
}
System.out.println("li的数量是"+count);
}
}
例题:将该字符串分解,将属性赋给定义的Person类的对象上,并且输出。
package String2;
public class People {
private String idcard;
private String name;
private int age;
private int gender;
public People(){
}
public People(String idcard, String name, int age, int gender) {
this.idcard = idcard;
this.name = name;
this.age = age;
this.gender = gender;
}
public String getIdcard() {
return idcard;
}
@Override
public String toString() {
return "People{" +
"idcard='" + idcard + '\'' +
", name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
'}';
}
public void setIdcard(String idcard) {
this.idcard = idcard;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
}
/**
* 定义一个方法,传入字符串格式(idcard:name:age:gender)的分隔形式如
* "21341234:kenxin:20:1|23423423:21:kaka:1|45635463456:xiaohong:22:2",将该字符串分解,将属性赋给定义的Person类的对象上,并且输出。
* 提示:创建每个Person对象,每个对象都有idcard,name,age,gender属性
*
*/
public class ShenFenShuJuChaiFen {
public static void main(String[] args) {
String s ="21341234:kenxin:20:1|23423423:kaka:21:1|45635463456:xiaohong:22:2";
//根据|拆分字符串
String[] pdate = s.split("\\|");
People[] ps = new People[3];
//遍历这个数组
for (int i =0;i<pdate.length;i++){
String pstr = pdate[i];
//创建一个People对象
People p = new People();
String[] attrStr = pstr.split(":");
//给属性赋值
p.setIdcard(attrStr[0]);
p.setName(attrStr[1]);
p.setAge(Integer.parseInt(attrStr[2]));
p.setGender(Integer.parseInt(attrStr[3]));
//把创建的People对象放入数组中
ps[i] = p;
}
System.out.println(Arrays.toString(ps));
}
}