String类中的常用方法

本文深入解析Java中String类的各种方法,包括创建、比较、搜索、截取、替换、大小写转换及去空格等操作。同时,介绍了字符串的逆序和句子翻转技巧,为Java开发者提供全面的字符串处理指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建一个String对象

源码

public String(char[] value)

实例

char[] value ={"a","b","c","d"};
String str = new String(value);
//相当于String str = new String("abcd")

源码

public String(char chars[], int x, int n)

实例

char[] array = {'a','b','c','d','e'};
String str1 = new String(array,2,3); //cd

startsWith()

源码

public boolean startsWith(String prefix) {
        return startsWith(prefix, 0);
    }

实例

String str1 = "abcde";
System.out.println(str1.startsWith("ab")); //true

endsWith()

源码

public boolean endsWith(String suffix)
//是否以指定后缀结束

获取字符串长度

源码

public int length()

实例

       String str1 = "abcde";
       System.out.println(str1.length()); //5

charAt()

源码

public char charAt(int index) {
        if ((index < 0) || (index >= value.length)) {
            throw new StringIndexOutOfBoundsException(index);
        }
        return value[index];
    }

实例

String str1 = "abcde";
char ch = str1.charAt(4); //e

substring()

源码

public String substring(int beginIndex)
//从beginIndex位置开始直到此字符串末尾的所有字符

实例

String str1 = "abcde";
System.out.println(str1.substring(1));
//bcde

源码

public String substring(int beginIndex, int endIndex)
//从beginIndex处开始,到 endIndex-1处的所有字符

实例

String str1 = "abcde";
System.out.println(str1.substring(1,4));
//bcd

equals()

源码

public boolean equals(Object anObject)

实例

String str1 = "abcde";
System.out.println(str1.equals("abcde"));//true

compareTo()

源码

public int compareTo(String anotherString)
//该方法是对字符串内容按字典顺序进行大小比较,
//通过返回的整数值指明当前字符串与参数字符串的大小关系。
//若当前对象比参数大则返回正整数,反之返回负整数,相等返回0。

实例

       String str1 = "abcde";
       System.out.println(str1.compareTo("abcd")); //1
       //==运算符用于比较对象

equals和compareTo方法用于比较字符串的内容

indexOf()

源码

public int indexOf(String str)
//返回指定字符串在此字符串中第一次出现处的索引
public int indexOf(String str, int fromIndex)
//从指定索引开始查找指定字符串
public int lastIndexOf(String str)
//返回指定字符串在此字符串中最后一次出现处的索引,从字符串的末尾位置向前查找
public int lastIndexOf(String str, int fromIndex)
//与第二种方法类似,区别于该方法从fromIndex位置向前查找。

concat()

源码

 public String concat(String str)
 //将指定字符串连接到此字符串的结尾

实例

s1 = s.concat("abc");

大小写转换

源码

public String toLowerCase()
//返回将当前字符串中所有字符转换成小写后的新字符串
public String toUpperCase()
//返回将当前字符串中所有字符转换成大写后的新字符串

字符串两端去空格

String trim()
//删除字符串前后的所有空格

replace()

源码

 public String replace(char oldChar, char newChar)
 ////用字符newChar替换当前字符串中oldChar字符
 public String replace(CharSequence target, CharSequence replacement) 
 //将字符串中的指定字符序列target替换为replacement

split()

源码

public String[] split(String regex)
//根据参数regex将原来的字符串分割为若干个子字符串

"abcdef" ===> "cdefab"

import java.util.Scanner;
public class Demo6 {
    public static String reverse(String str, int begin, int end) {
        char[] ch = str.toCharArray();
        char tmp;
        while (begin < end) {
            tmp = ch[begin];
            ch[begin] = ch[end];
            ch[end] = tmp;
            begin++;
            end--;
        }
        return String.copyValueOf(ch);//将ch转化为字符串
    }
    public static void leftRoadString(String str, int n) {
        if (str == null || n < 0 || n > str.length()) {
            return;
        }
        //abcdef
        int left = 0;
        int leftend = n - 1;
        int right = n;
        int rightend = str.length() - 1;
        str = reverse(str, left, leftend);//bacdef
        str = reverse(str, right, rightend);//bafedc
        str = reverse(str, left, rightend);//cdefab
        System.out.println(str);
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入字符串:");
        String str = scanner.nextLine();
        System.out.print("请输入旋转的位置:");
        int n = scanner.nextInt();
        leftRoadString(str, n);
    }
}

"Here is 优快云" ===> "优快云 is Here"

import java.util.Scanner;
public class Demo6 {
    public static void reverse(char[] ch, int begin, int end) {
        char tmp;
        while (begin < end) {
            tmp = ch[begin];
            ch[begin] = ch[end];
            ch[end] = tmp;
            begin++;
            end--;
        }
    }

    //here is tulun
    public static String reverseSentence(String str) {
        if (str == null) {
            return null;
        }
        char[] ch = str.toCharArray();
        reverse(ch, 0, ch.length - 1);
        int i = 0;//单词的开始
        int j = 0;//单词的结束
        while (i < str.length()) {
            if (ch[i] == ' ') {
                i++;
                j++;
            } else if (j == ch.length || ch[j] == ' ') {
                reverse(ch, i, --j);
                i = ++j;
            } else {
                j++;
            }
        }
        return String.copyValueOf(ch);
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入字符串:");
        String str = scanner.nextLine();
        System.out.println(reverseSentence(str));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值