Bootstrap

【Java基础语法】String类

1.认识String类

C 语言中已经涉及到字符串了,但是在 C 语言中要表示字符串只能使用字符数组或者字符指针,可以使用标准库提 供的字符串系列函数完成大部分操作,但是这种将数据和操作数据方法分离开的方式不符合面相象的思想,而字 符串应用又非常广泛,因此Java语言专门提供了 String 类。

 2.常用的方法

2.1.字符串的构造

代码如下:

public static void main(String[] args) {
        String s1="hello world";
        System.out.println(s1);


        String s2=new String("hello world");
        System.out.println(s2);

        char arr[]={'h','e','l','l','o'};
        String s3=new String(arr);
        System.out.println(s3);
    }
String 类提供的构造方式非常多,常用的就以下三种:
1.使用常量串构造
2.直接newString对象
3.使用字符数组进行构造
注意: String 是引用类型,内部并不存储字符串本身
此时可以看到其是由final修饰的,而value[]字符数组是用于指向输入的字符。
注意:这幅图,小编这里str1的地址传给了str3,这里可以忽略,方便理解。

2.2String对象的比较

在字符串比较中:

1.地址比较
对于内置类型, == 比较的是变量中的值;对于引用类型 == 比较的是引用中的地址。
咧如:
        String s4 = new String("hello");
        String s5 = new String("world");
        String s6 = s4;
        System.out.println(s4 == s5); //false
        System.out.println(s5 == s6); //false
        System.out.println(s6 == s4); //true

s4与s5对象的地址值是不一样的,所以是false,在将s6的地址传给s4,再次比较此时,输出的就是true。

2.内容比较

在String重写了Object类的equals方法后,就大大简化了我们的比较过程

比较代码如下:

        String s4 = new String("hello");
        String s5 = new String("hello");
        System.out.println(s4==s5);   //false
        System.out.println(s4.equals(s5));   //true

如图,在地址不同时,虽然内容一样但是==表示的是地址的比较,而equals是进行内容比较

通常比较方式是:  

对象1.equals(对象2)

 3.intcompareTo(Strings)比较
equals 不同的是, equals 返回的是 boolean 类型,而 compareTo 返回的是 int 类型。具体比较方式:
1. 先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
2. 如果前 k 个字符相等 (k 为两个字符长度最小值 ) ,返回值两个字符串长度差值

 

 String s1 = new String("abc");
        String s2 = new String("ac");
        String s3 = new String("abc");
        String s4 = new String("abcdef");
        System.out.println(s1.compareTo(s2)); //不同输出字符差值-1
        System.out.println(s1.compareTo(s3)); //相同输出0
        System.out.println(s1.compareTo(s4)); //前面字符一样输出后面字符数量的差值-3
如图:
a与b的ASCII码值相差为1;所以如注解,输出-1;
如果两内容一样,则输出0;
如果前面字符一样,则在后面输出相差的字符数量。
4. intcompareToIgnoreCase(Stringstr)比较

compareTo方式相同,但是忽略大小写比较。

例如:

 String s1 = new String("abc");
 String s5=new String("ABc");
 System.out.println(s1.compareToIgnoreCase(s5)); //忽略大小写,相同输出0

忽略了AB大写,所以s1与s5对象指向的地址对应的内容一样,输出为0。

2.3.字符串的查找

以下是一些相关功能代码

1.char charAt(int index)     

返回index位置上字符,如果index为负数或者越界,抛出

IndexOutOfBoundsException 异常。
2.int indexOf( ch)
返回 ch 第一次出现的位置,没有返回 -1
3. int indexOf( ch, int fromIndex)
fromIndex 位置开始找 ch 第一次出现的位置,没有返回 -1
4.int indexOf(String str)
返回 str 第一次出现的位置,没有返回 -1
5. int indexOf(String str, int fromIndex)
fromIndex 位置开始找 str 第一次出现的位置,没有返回 -1
6.int lastIndexOf( ch)
从后往前找,返回 ch 第一次出现的位置,没有返回 -1
7. int lastIndexOf(ch, int fromIndex)
fromIndex 位置开始找,从后往前找 ch 第一次出现的位置,没有返回-1
8.int lastIndexOf(String str)
从后往前找,返回 str 第一次出现的位置,没有返回 -1
9. int lastIndexOf(String str, int fromIndex)
fromIndex 位置开始找,从后往前找 str 第一次出现的位置,没有返回-1

 

代码如下:

        String s = "aaabbbcccaaabbbccc";
        System.out.println(s.charAt(3)); //'b'
        System.out.println(s.indexOf('c')); //6
        System.out.println(s.indexOf('c', 10)); //15
        System.out.println(s.indexOf("bbb")); //3
        System.out.println(s.indexOf("bbb", 10)); //12
        System.out.println(s.lastIndexOf('c'));//17
        System.out.println(s.lastIndexOf('c', 10)); //8
        System.out.println(s.lastIndexOf("bbb"));//12
        System.out.println(s.lastIndexOf("bbb",10));//5
注意:再找对应字符串的时候可以把字符串,看做字符,这样就方便数出答案;

2.4.转化

1.数值和字符串转化
数字,对象转字符串
代码如下:
        String s1 = String.valueOf(1234);
        String s2 = String.valueOf(12.34);
        String s3 = String.valueOf(true);
        String s4 = String.valueOf(new Student("Hanmeimei", 18));
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println(s4);
    }
}
class Student{
    public String name;
    public int age;
    public Student(String name,int age){
        this.name=name;
        this.age=age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

这里对像转化成字符串要进行toString方法的重写,才能打印。

主要运用String的valueOf()方法;

字符串转数字:
  int data1=Integer.parseInt("1234");
  double data2=Double.parseDouble("12.34");
  System.out.println(data1);
  System.out.println(data2);

这里要注意:不同类型数字的不同写法

2.大小写转换
  String s1="hello";
  String s2="HELLO";
  //小写转大写
  System.out.println(s1.toUpperCase());
  //大写转小写
  System.out.println(s2.toLowerCase());}
两个方法 toUpperCase();toLowerCase()
3.字符串转数组 

代码如下:

        String s = "hello";
        //字符串转数组
        char[] ch = s.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            System.out.print(ch[i]);
        }
方法 toCharArray(),注意要用char类型的数组接收
 4.格式化
Strings=String.format("%d-%d-%d",2019,9,14);
  System.out.println(s);
}

方法 format()的使用 

2.5字符串替换

        String str="helloworld";
        System.out.println(str.replaceAll("l","_"));
        System.out.println(str.replaceFirst("l","_"));

运行结果:

he__owor_d
he_loworld

注意事项: 由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串.

 2.6字符串的拆分

String[] split(String regex)                            String[] split(String regex, int limit)
将字符串全部拆分                                        将字符串以指定的格式,拆分为 limit
        String str1 = "hello world" ;
        String[] result = str1.split(" ") ; // 按照空格拆分
        for(String s1: result)
        {
            System.out.println(s1);
        }

 输出:

hello
world

String str = "hello world hello bit" ;
String[] result = str.split(" ",3) ;  //拆分为两组
for(String s: result) {
System.out.println(s);
}

 输出:

hello
world
hello you

注意:拆分是特别常用的操作. 一定要重点掌握. 另外有些特殊字符作为分割符可能无法正确切分, 需要加上转义. 

1. 字符"|","*","+"都得加上转义字符,前面加上 "\\" .
2. 而如果是 "\" ,那么就得写成 "\\\\" .
3. 如果一个字符串中有多个分隔符,可以用"|"作为连字符
代码如下:
 String s2=new String("name=zhangsan&age=15");
        String[] arr=s2.split("=|&");
        for(String s: arr){
            System.out.println(s);
        }
输出:
name

zhangsan

age

15

2.7.字符串截取

从一个完整的字符串之中截取出部分内容。可用方法如下:
String substring(int beginIndex)
从指定索引截取到结尾
String substring(int beginIndex, int endIndex)
截取部分内容

 代码实例:

        String str = "helloworld" ;
        System.out.println(str.substring(5));
        System.out.println(str.substring(0, 5));
输出结果:
hello
world

 注意:在java中一般是左开右闭,所以(0,5)指的是重0下标到4下标。

2.8字符串空白去除

String trim()
去掉字符串中的左右空格 , 保留中间空格
代码实例:
        String st="   hello world   ";
        System.out.println(st);
        String st1=st.trim();
        System.out.println(st1);

代码输出:

       hello world   
hello world

 2.9字符串的不可变性

1. String 类被 final 修饰,表明该类不能被继承
2. value 被修饰被 final 修饰,表明 value 自身的值不能改变,即不能引用其它字符数组,但是其引用空间中的内容可以改。
3.所有涉及到可能修改字符串内容的操作都是创建一个新对象,改变的是新对象
对于第二点,代码实例如下:
可以发现报错。
final 修饰类表明该类不想被继承, final 修饰引用类型表明该引用变量不能引用其他对象,但是其引用对象中的内容是可以修改的

3.总结

String类的方法小编在这里列举了很多,如果还有,希望各位uu在评论区提出宝贵意见。

制作不易,麻烦给小编一个小小的赞吧。

 

 

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;