JAVA String 转换相关函数分析


在这里插入图片描述

字符串转换

valueOf

valueOf() 方法有以下几种不同形式:

  • valueOf(boolean b): 返回 boolean 参数的字符串表示形式。.
  • valueOf(char c): 返回 char 参数的字符串表示形式。
  • valueOf(char[] data): 返回 char 数组参数的字符串表示形式。
  • valueOf(char[] data, int offset, int count): 返回 char 数组参数的特定子数组的字符串表示形式。
  • valueOf(double d): 返回 double 参数的字符串表示形式。
  • valueOf(float f): 返回 float 参数的字符串表示形式。
  • valueOf(int i): 返回 int 参数的字符串表示形式。
  • valueOf(long l): 返回 long 参数的字符串表示形式。
  • valueOf(Object obj): 返回 Object 参数的字符串表示形式。
参数
  • 指定类型参数。
返回值

返回参数类型的表现形式。

实例
public class Test {
    public static void main(String args[]) {
        double d = 1100.00;
        boolean b = true;
        long l = 1234567890;
        char[] arr = {'r', 'u', 'n' };

        System.out.println("返回值 : " + String.valueOf(d) );
        System.out.println("返回值 : " + String.valueOf(b) );
        System.out.println("返回值 : " + String.valueOf(l) );
        System.out.println("返回值 : " + String.valueOf(arr) );
    }
}

以上程序执行结果为:

返回值 : 1100.0
返回值 : true
返回值 : 1234567890
返回值 : run
源码
   public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }
    public static String valueOf(int i) {
        return Integer.toString(i);
    }
    public static String valueOf(char data[], int offset, int count) {
        return new String(data, offset, count);
    }

对象、基本类型采用包装类型的toString,数组采用构造函数

toCharArray()

toCharArray() 方法将字符串转换为字符数组。

语法
public char[] toCharArray()
参数
返回值

字符数组。

实例
public class Test {
    public static void main(String args[]) {
        String str = new String("www.hello.com");

        System.out.print("返回值 :" );
        System.out.println( str.toCharArray() );
    }
}

以上程序执行结果为:

返回值 :www.hello.com
源码
public char[] toCharArray() {
        // Cannot use Arrays.copyOf because of class initialization order issues
        char result[] = new char[value.length];
        System.arraycopy(value, 0, result, 0, value.length);//复制数组
        return result;
    }

toLowerCase

toLowerCase() 方法将字符串转换为小写。

语法

public String toLowerCase()
或
public String toLowerCase(Locale locale)

参数

返回值

转换为小写的字符串。

实例
public class Test {
    public static void main(String args[]) {
        String str = "WWW.HELLO.COM";

        System.out.print("返回值 :" );
        System.out.println( str.toLowerCase() );
    }
}

以上程序执行结果为:

返回值 :www.hello.com

toUpperCase

String转换为大写。

getBytes()

getBytes() 方法有两种形式:

  • getBytes(String charsetName): 使用指定的字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
  • getBytes(): 使用平台的默认字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
语法
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException

或

public byte[] getBytes()
参数
  • charsetName – 支持的字符集名称。
返回值

返回 byte 数组。

实例
import java.io.*;
 
public class Test {
    public static void main(String args[]) {
         String str1 = "hello";

        try{
            byte[] str2 = str1.getBytes();
            System.out.println("返回值:" + str2 );

            str2 = str1.getBytes( "UTF-8" );
            System.out.println("返回值:" + str2 );

            str2 = str1.getBytes( "ISO-8859-1" );
            System.out.println("返回值:" + str2 );
        } catch ( UnsupportedEncodingException e){
            System.out.println("不支持的字符集");
        }
    }
}

以上程序执行结果为:

返回值:[B@7852e922
返回值:[B@4e25154f
返回值:[B@70dea4e

编码、解码是为了传输数据过程中进行加密、解密。

String(byte bytes[], Charset charset)

对字节按照 charset 进行解码(charset→unicode),返回解码后的字符串。
String(byte bytes[]) 表示按照系统默认编码方式进行

 		String s = "浣犲ソ"; //这是"你好"的gbk编码的字符串
        String ss = new String(s.getBytes("GBK"), "UTF-8");
        System.out.println(ss);//你好

猜你喜欢

转载自blog.csdn.net/qq_15604349/article/details/124396703