java中的String详解(创建字符串、字符串比较、查找字符串、String与int之间的转换、字符串大小写转换、字符串转数组)


前言

本文主要对java中的String字符串类进行详细介绍,包括创建字符串、字符串比较、字符串查找以及字符串转换。


一、String类

在java中,String类表示字符串,属于对象。

1.创建字符串

(1)直接创建

String s = "hello";

(2)使用构造方法创建 String 对象

以下是不同构造方法举例:

//1.String(String Original),把字符串封装成字符串对象
String s1 = new String("hello world");

//2.String(char[] value),把字符数组封装成字符串对象
char[] c1 = {
    
    'h','a','h','a','!'};   
String s2 = new String(c1);

//String(char[] value,int index, int count)
//将index到count个字符数组值封装成字符串对象
char[] c2 = {
    
    'h','a','h','a','!'};   
String s3 = new String(c2,1,3);

2.以上两种方式创建的区别

(1)直接赋值创建: 该对象为方法区中的常量池中的字符串常量。
(2)通过构造方法创建: 该String对象存放在java虚拟机的堆内存中。即:堆内存里存放的是字符串常量的地址,字符串常量存放在方法区的常量池中。
例:

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

运行结果:
在这里插入图片描述

图解如下:
在这里插入图片描述

注意: String类是final类,不能被继承,它的成员方法默认为final方法。
( 在Java中,被final修饰的类是不允许被继承的,并且该类中的成员方法都默认为final方法)


二、String对象的比较

在Java中共提供了4中方式进行字符串的比较

1.==比较

注意: 对于基本类型,== 比较的是变量中的值,对于引用类型 == 比较的是引用中的地址。
例1:

public static void main(String[] args) {
    
    
	 int a = 10;
	 int b = 20;
	 int c = 10;
	 // 对于基本类型变量,==比较两个变量中存储的值是否相同
	 System.out.println(a == b); // false
	 System.out.println(a == c); // true
	 // 对于引用类型变量,==比较两个引用变量引用的是否为同一个对象
  	 String s1 = new String("hello");
     String s2 = new String("world");
     String s3 = "hello";
     String s4 = "hello";
     System.out.println(s1==s2);//false
     System.out.println(s3==s4);//true
}

2. equals() 方法(按照字典序比较)

equals比较:String对象中的逐个字符(按字符大小的顺序比较),返回boolean类型的值。
例2:

public static void main(String[] args) {
    
    
	 String s1 = new String("hello");
	 String s2 = new String("hello");
	 String s3 = new String("Hello");
	 System.out.println(s1.equals(s2)); // true
	 System.out.println(s1.equals(s3)); // false
}

结果截图:
在这里插入图片描述

3. compareTo() 方法

方法原型:int compareTo(String s)
具体比较方式:
(1)先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
(2)如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值
例3:

public static void main(String[] args) {
    
    
	 String s1 = new String("abc");
	 String s2 = new String("abc");
	 String s3 = new String("ac");
	 String s4 = new String("abcdef");
	 System.out.println(s1.compareTo(s2)); 
	 System.out.println(s1.compareTo(s3));
	 System.out.println(s1.compareTo(s4)); 
}

结果截图:
在这里插入图片描述

4. compareToIgnoreCase() 方法

方法原型:int compareToIgnoreCase(String str)
与compareTo方式相同,但忽略大小写比较
例4:

public static void main(String[] args) {
    
    
	 String s1 = new String("abc");
	 String s2 = new String("ABc");
	 String s3 = new String("ac");
	 String s4 = new String("abcdef");
	 System.out.println(s1.compareToIgnoreCase(s2)); 
	 System.out.println(s1.compareToIgnoreCase(s3)); 
	 System.out.println(s1.compareToIgnoreCase(s4)); /
}

结果截图:
在这里插入图片描述


三、字符串查找

以下是String类提供的常用查找的方法:
(1)char charAt(int index) : 返回index位置上字符,如果index为负数或者越界,抛出 IndexOutOfBoundsException异常。
(2)int indexOf(int ch) : 返回ch第一次出现的位置,没有返回-1。
(3)int indexOf(int 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(int ch) : 从后往前找,返回ch第一次出现的位置,没有返回-1。
(7)int lastIndexOf(int ch, int fromIndex): 从fromIndex位置开始找,从后往前找ch第一次出现的位置,没有返回-1。
(8)int lastIndexOf(String str) : 从后往前找,返回str第一次出现的位置,没有返回-1。
(9)int lastIndexOf(String str, int fromIndex): 从fromIndex位置开始找,从后往前找str第一次出现的位置,没有返回-1。
例:

 public static void main(String[] args) {
    
    
        String s = "abcaaddccbbcccaaddcc";
        System.out.println(s.charAt(5)); // 'd'---从0开始
        System.out.println(s.indexOf('c')); // 2
        System.out.println(s.indexOf('c', 10)); // 11
        System.out.println(s.indexOf("ccc")); // 11
        System.out.println(s.indexOf("dd", 10)); // 16
        System.out.println(s.lastIndexOf('c')); // 19
        System.out.println(s.lastIndexOf('c', 10)); // 8
        System.out.println(s.lastIndexOf("bb")); // 9
        System.out.println(s.lastIndexOf("cc", 10)); // 7
    }

四、字符串转换

1.int与字符串的转换

(1)int转字符串
例1:

int a = 123;
String s = "" + a;   //直接拼接 “”

例2:

int a =123;
String s = String.valueOf(a); //调用String的valueOf()方法

例3:

int a = 123;
String s = Integer.toString(a);//调用整型包装类Integer的toString()方法

(2)字符串转int

例1:

String s = "1234";
int a = Integer.parseInt(s);//调用整型包装类Integer的parseInt()方法

例2:

String s = "1234";
int a = Integer.valueOf(s).intValue();

2.数值与字符串的转换

例:

public static void main(String[] args) {
    
    
	 // 数字转字符串
	 String s1 = String.valueOf(1234);
	 String s2 = String.valueOf(1.48);
	 String s3 = String.valueOf(true);
	 System.out.println(s1);
	 System.out.println(s2);
	 System.out.println(s3);
	 // 字符串转数字
	 int data1 = Integer.parseInt("1234");
	 double data2 = Double.parseDouble("12.34");
	 System.out.println(data1);
	 System.out.println(data2);
}

结果截图:
在这里插入图片描述

3.大小写转换

例:

public static void main(String[] args) {
    
    
        String s1 = "hello";
        String s2 = "HELLO";
        System.out.println(s1.toUpperCase());//小写转大写
        System.out.println(s2.toLowerCase());// 大写转小写
    }

4. 字符串转数组

例:

public static void main(String[] args) {
    
    
	 String s = "hello";
	 // 字符串转数组
	 char[] ch = s.toCharArray();
	 for (int i = 0; i < ch.length; i++) {
    
    
	 System.out.print(ch[i]);
	 }
	 System.out.println();
	 // 数组转字符串
	 String s2 = new String(ch);
	 System.out.println(s2);
}

总结

以上就是本文的全部内容。

猜你喜欢

转载自blog.csdn.net/m0_53689542/article/details/124534832
今日推荐