String类常见方法

常见方法

-charAt( int index):返回字符串指定位置的字符

复制代码
public class CharAtExample {
    public static void main(String[] args) {
        String greeting = "Hello"; // 定义一个字符串
        int index = 1; // 选择一个索引,例如1,表示第二个字符

        // 使用charAt方法获取指定索引的字符
        char character = greeting.charAt(index);

        // 打印结果
        System.out.println("The character at index " + index + " is: " + character);
    }
}
复制代码
The character at index 1 is: e

-indexOf(String s):返回指定字符串第一次出现的位置

复制代码
public class IndexOfExample {
    public static void main(String[] args) {
        String text = "Java is fun and Java is also great!";
        String searchString = "is";

        // 寻找子串在文本中第一次出现的位置
        int index = text.indexOf(searchString);

        // 打印结果
        if (index != -1) {
            System.out.println("The string \"" + searchString + "\" first appears at index: " + index);
        } else {
            System.out.println("The string \"" + searchString + "\" does not appear in the text.");
        }
    }
}
复制代码
The string "is" first appears at index: 4

-startsWith(String s):测试字符串是否以指定前缀开始

复制代码
public class StartsWithExample {
    public static void main(String[] args) {
        String text = "Hello, World!";
        String prefix = "Hello";

        // 检查字符串是否以指定的前缀开始
        boolean startsWithPrefix = text.startsWith(prefix);

        // 打印结果
        if (startsWithPrefix) {
            System.out.println("The string starts with the prefix: \"" + prefix + "\"");
        } else {
            System.out.println("The string does not start with the prefix: \"" + prefix + "\"");
        }
    }
}

输出:

复制代码
The string starts with the prefix: "Hello"

-endsWith(String s):测试字符串是否以指定后缀开始

复制代码
public class EndsWithExample {
    public static void main(String[] args) {
        String text = "Java is fun!";
        String suffix = "fun";

        // 检查字符串是否以指定的后缀结束
        boolean endsWithSuffix = text.endsWith(suffix);

        // 打印结果
        if (endsWithSuffix) {
            System.out.println("The string ends with the suffix: \"" + suffix + "\"");
        } else {
            System.out.println("The string does not end with the suffix: \"" + suffix + "\"");
        }
    }
}

//它会输出:

The string ends with the suffix: "fun"

-subString(int index):返回字符串的子字符串

复制代码
public class SubstringExample {
    public static void main(String[] args) {
        String text = "Java Programming Language";
        int index = 5; // 从索引5开始截取子字符串

        // 使用subString方法获取从指定索引到字符串末尾的子字符串
        String sub = text.substring(index);

        // 打印结果
        System.out.println("The substring starting from index " + index + " is: \"" + sub + "\"");
    }
}
//输出:

The substring starting from index 5 is: "Programming Language"

-replace(char a,char b):替换字符串的指定字符

复制代码
public class ReplaceExample {
    public static void main(String[] args) {
        String originalString = "Hello World!";
        char toReplace = 'o'; // 要替换的字符
        char replacement = '0'; // 替换后的字符

        // 使用replace方法替换字符
        String replacedString = originalString.replace(toReplace, replacement);

        // 打印原始字符串和替换后的字符串
        System.out.println("Original string: " + originalString);
        System.out.println("Replaced string: " + replacedString);
    }
}
//输出:

Original string: Hello World!
Replaced string: Hell0 W0rld!

-trim():去掉字符串的前后空格

复制代码
public class TrimExample {
    public static void main(String[] args) {
        String originalString = "   Hello, World!   ";
        
        // 使用trim方法去掉字符串前后的空格
        String trimmedString = originalString.trim();

        // 打印原始字符串和去掉空格后的字符串
        System.out.println("Original string: '" + originalString + "'");
        System.out.println("Trimmed string: '" + trimmedString + "'");
    }
}

输出:

复制代码
Original string: '   Hello, World!   '
Trimmed string: 'Hello, World!'

-concat(String str):连接两个字符串

复制代码
public class ConcatExample {
    public static void main(String[] args) {
        String baseString = "Hello, ";
        String附加String = "World!";

        // 使用concat方法连接两个字符串
        String concatenatedString = baseString.concat(附加String);

        // 打印连接后的字符串
        System.out.println("Concatenated string: " + concatenatedString);
    }
}

输出:

复制代码
Concatenated string: Hello, World!

-split(String regex):给定正则表达式的匹配来拆分字符串

复制代码
public class SplitExample {
    public static void main(String[] args) {
        String text = "apple,orange,banana,grape";
        String regex = ","; // 正则表达式,用于按逗号拆分字符串

        // 使用split方法根据正则表达式拆分字符串
        String[] fruits = text.split(regex);

        // 打印拆分后的字符串数组
        System.out.println("Split strings:");
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
    }
}

输出:

复制代码
Split strings:
apple
orange
banana
grape

作者:静默虚空
欢迎任何形式的转载,但请务必注明出处。
限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教。

posted @   糊涂图-4060  阅读(13)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示