【JAVA笔记】JAVA常用的字符串操作03
一、Java中常用的字符串操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | public class Common_String_Operations { public static void main(String[] args) { boolean p1 = isEmpty( "aa" ); System.out.println( "判断字符串是否为空:" + p1); String p2 = subStartString( "a1234567890" , "a" , "3" ); System.out.println( "判断字符串start开始,end结束之间的内容:" + p2); String p3 = subStartString1( "abcdef" , "c" , 2 ); System.out.println( "判断字符串start开始的后几位:" + p3); String p4 = insertString( "abc" , "f" , 2 ); System.out.println( "index字符位插入insert字符:" + p4); boolean p5 = contains( "abcdef" , "ef" ); System.out.println( "判断str是否包含subStr:" + p5); boolean p6 = containsIgnoreCase( "ABCdef" , "bc" ); System.out.println( "判断str是否包含subStr(不区分大小写):" + p6); boolean p7 = startWith( "abcdef" , "abc" ); System.out.println( "判断字符串是否以start开头:" + p7); boolean p8 = endWith( "abcdef" , "ef" ); System.out.println( "判断字符串是否以end结尾:" + p8); String p9 = replace( "abcdef" , "de" , "ff" ); System.out.printf( "字符串替换:" + p9); boolean p10 = equals( "11" , "cc" ); System.out.println( "判断字符串是否相同:" + p10); boolean p11 = equalsIgnoreCase( "AABBCC" , "aabbcc" ); System.out.println( "判断字符串是否相同(忽略大小写):" + p11); String p12 = toUpperCase( "bbccdf" ); System.out.println( "字符串小写变大写:" + p12); String p13 = toLowerCase( "AAccdf" ); System.out.println( "字符串大写变小写:" + p13); int p14 = length( "aaaaaabbbbbbbbbbccccccccc" ); System.out.println( "统计字符串长度:" + p14); } //1、判断字符串是否为空 public static boolean isEmpty(String str) { return str == null || str.length() == 0 ; } //2、截取字符串(star起,end闭之间的内容) public static String subStartString(String str, String start, String end) { int startIndex = str.indexOf(start); int endIndex = str.indexOf(end); return str.substring(startIndex, endIndex); } //3、截取字符串 start开始,count后几位之间的字符串 public static String subStartString1(String str, String start, int count) { int startIndex = str.indexOf(start); return str.substring(startIndex + start.length(), startIndex + start.length() + count); } // 4、插入字符串 public static String insertString(String str, String insert, int index) { return str.substring( 0 , index) + insert + str.substring(index); } //5、是否包含字符串 public static boolean contains(String str, String subStr) { return str.indexOf(subStr) != - 1 ; } // 6、是否包含字符串不区分大小写 public static boolean containsIgnoreCase(String str, String subStr) { return str.toLowerCase().indexOf(subStr.toLowerCase()) != - 1 ; } //7、是否以字符串开头 public static boolean startWith(String str, String start) { return str.startsWith(start); } //8、是否以字符串结尾 public static boolean endWith(String str, String end) { return str.endsWith(end); } //9、替换字符串 public static String replace(String str, String oldStr, String newStr) { return str.replace(oldStr, newStr); } //10、比较字符串-是否相等 public static boolean equals(String str1, String str2) { return str1.equals(str2); } //11、比较字符串,忽略大小写 public static boolean equalsIgnoreCase(String str1, String str2) { return str1.equalsIgnoreCase(str2); } //12、字符串大写 public static String toUpperCase(String str) { return str.toUpperCase(); } //13、字符串小写 public static String toLowerCase(String str) { return str.toLowerCase(); } //14、字符串长度 public static int length(String str) { return str.length(); } } |
运行结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 判断字符串是否为空: false 判断字符串start开始,end结束之间的内容:a12 判断字符串start开始的后几位:de index字符位插入insert字符:abfc 判断str是否包含subStr: true 判断str是否包含subStr(不区分大小写): true 判断字符串是否以start开头: true 判断字符串是否以end结尾: true 字符串替换:abcfff判断字符串是否相同: false 判断字符串是否相同(忽略大小写): true 字符串小写变大写:BBCCDF 字符串大写变小写:aaccdf 统计字符串长度: 25 |
参考文章:
https://www.weixueyuan.net/view/6317.html
本文来自博客园,作者:橘子偏爱橙子,转载请注明原文链接:https://www.cnblogs.com/xfbk/p/16922531.html
分类:
JAVA系列
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
· 零经验选手,Compose 一天开发一款小游戏!