string类的简单操作

package dishiyitian;

import java.util.Arrays;

/*

 * 1):String的创建和转换:

    byte[] getBytes():把字符串转换为byte数组

    char[] toCharArray():把字符串转换为char数组

    String(byte[] bytes):把byte数组转换为字符串

    String(char[] value):把char数组转换为字符串

 * */

/*

 * 2):获取字符串信息

    int length() 返回此字符串的长度

    char charAt(int index) 返回指定索引处的 char 值

    int indexOf(String str)返回指定子字符串在此字符串中第一次出现处的索引。

    int lastIndexOf(String str)返回指定子字符串

 * */

public class yinyong {

    public static void main(String[] args) {

        // TODO Auto-generated method stub

          getconvert();

           germessage();

    }

    private static void germessage() {

        // TODO Auto-generated method stub

        char[] d=new char[]{'a','b','c'};

        String eString=new String(d);

        System.out.println(eString.charAt(0));

        System.out.println(eString.indexOf("c"));    //必须是字符串才能使用该方法,同时字符串记得加双引号!!

        String aString="fjdfk";

        System.out.println(aString.charAt(4));

        System.out.println(d.length);

        System.out.println("abc".length());

        System.out.println("abc".charAt(1));

    }

    private static void getconvert() {

        // TODO Auto-generated method stub

        char[] a=new char[] {'a','b','c'};

        String str=new String(a);

        System.out.println(str);

        System.out.println(str.toCharArray());

        System.out.println("abc".getBytes());

        System.out.println(Arrays.toString("abc".getBytes()));

        System.out.println(new String("abc".getBytes()));

        

    }

}

猜你喜欢

转载自blog.csdn.net/zhouzhou_98/article/details/81229992