JAVA String 常用方法(超详细)

本文详细介绍了Java中String类的一些主要功能,包括获取字符串长度、获取指定索引的字符、查找字符或子串、截取字符串,以及字符串的比较、转换、替换和处理空格等操作。这些方法是Java编程中常见的字符串操作。
摘要由CSDN通过智能技术生成

文章目录

一 、常见String类的获取功能

(1) length:获取字符串长度;

   String test ="abcdefg";
        System.out.println("长度为:"+ test.length() );

长度为: 7

(2) charAt(int index):获取指定索引位置的字符;

     String test ="abcdefg";
        System.out.println( "索引为0既第一个字符为:" + test.charAt(0) );

索引为0既第一个字符为: a

(3) indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引;(数字是ASCII码中对应的字符数值)

     String test ="abcdefg";
        System.out.println( test.indexOf(97) );
        System.out.println( test.indexOf("b") );

0
1

(4) substring(int start):从指定位置开始截取字符串,默认到末尾;

      String test ="abcdefg";
        System.out.println( test.substring(2) );

cdefg

(5) substring(int start,int end):从指定位置开始到指定位置结束截取字符串;

   String test ="abcdefg";
        System.out.println( test.substring(2,5));

cde

二、常见String类的判断功能

(1)equals(Object obj): 比较字符串的内容是否相同,区分大小写;

 String test ="abcdefg";
        String test1 ="abcdefg";
        System.out.println( test.equals(test1) );

true

(2)contains(String str): 判断字符串中是否包含传递进来的字符串;

   String test ="abcdefg";
        System.out.println( test.contains("ab") );

true

(3)startsWith(String str): 判断字符串是否以传递进来的字符串开头;

  String test ="abcdefg";
        System.out.println( test.startsWith("ab") );

true

(4)endsWith(String str): 判断字符串是否以传递进来的字符串结尾;

   String test ="abcdefg";
        System.out.println( test.endsWith(fg));

true

(5)isEmpty(): 判断字符串的内容是否为空串"";

   String test ="abcdefg";
        System.out.println( test.isEmpty());

false

三、常见String类的转换功能

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

   String test ="abcdefg";
        byte[] test1 =test.getBytes();
        System.out.println( test1[0] );

97

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

    String test ="abcdefg";
        char[] test1=test.toCharArray();
        System.out.println( test1[0] );

a

(3)String valueOf(char[] chs): 把字符数组转成字符串。valueOf可以将任意类型转为字符串;

        char[] test ={'a','b','c','d'};
        String  test1=String.valueOf(test);
        System.out.println(test1);

abcd

(4)toLowerCase(): 把字符串转成小写;

toUpperCase(): 把字符串转成大写;

    String test ="abcdefg";
        String test1 ="ABCDEFG";
        System.out.println(test1.toLowerCase());
        System.out.println(test.toUpperCase());

abcdefg
ABCDEFG

(5)concat(String str): 把字符串拼接;

      String test ="abcdefg";
        String test1 ="higk";
        System.out.println(test.concat(test1));

abcdefghigk

四、常见String类的其他常用功能

(1)replace(char old,char new) 将指定字符进行互换

 String test ="abcdefg";
        System.out.println(test.replace("a","A"));

Abcdefg

(2)replace(String old,String new) 将指定字符串进行互换

 String test ="abcdefg";
        System.out.println(test.replace("ab","AB"));

ABcdefg

(3)trim() 去除两端空格

 String test =" abcdefg ";
        System.out.println(test );
        System.out.println(test.trim());

&abcdefg&(代表空格)
abcdefg

(4)int compareTo(String str)

会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果,如果前面几个字 母一样会根据两个字符串的长度进行减法运算返回的就是这个减法的结果,如果连个字符串一摸一样 返回的就是0。

String test ="abcdefg";
        String test1 ="abcdefg";
        String test2 ="abcdefgh";

        System.out.println(test.compareTo(test1) );
        System.out.println(test.compareTo(test2) );

0
-1

  • 20
    点赞
  • 199
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值