Java--String 喜欢ヅ旅行 2023-10-17 10:58 16阅读 0赞 一、字符串方法 1、public String();空构造 2、public String(byte\[\] bytes);把字符数组转成字符串public String(byte\[\] bytes,int index,intlenth);把字符数组一部分转成字符串 3、public String(String original);把字符串常量值转成字符串 二、字符串比较 1、boolean equals(Objectobj); //比较字符串内容是否相同,区分大小写 2、boolean equalsIgnoreCase(Stringstr); //比较字符串内容是否相同,忽略大小写 3、boolean contains(Stringstr); //判断大字符串中是否包含小字符串 4、boolean startsWith(Stringstr); //判断字符串是否以某个指定字符串开头 5、boolean endsWith(Stringstr); //判断字符串是否以某个指定字符串结尾 6、boolean isEmpty();//判断字符串是否为空 7、null和""区别 ""是字符串常量,同时也是一个String类的对象,既然是对象当然可以调用String类中方法 null是空常量,不能调用任何方法,否则会出现空指针异常,null常量可以给任意的引用数据类型赋值 三、获取字符串 1、int length(); //获取字符串长度 2、char charAt(int index); //获取指定索引位置的字符 3、int indexOf(intch); //返回制定字符在此字符串中第一次出现处的索引 4、int indexOf(Stringstr); //返回指定字符串在此字符串中第一次出现处的索引 5、int indexOf(intch,int fromIndex); //返回指定字符在此字符串中从指定位置后第一次出现处的索引 6、int indexOf(Stringstr,int fromIndex); //返回指定字符串在此字符串中从指定位置后第一次出现处的索引 7、lastIndexOf 8、String substring(int start); //从指定位置开始截取字符串,默认到末尾 9、String substring(int start,int end); //从指定位置开始到指定结尾处截取字符串,默认到末尾 四、字符串转换 1、byte\[\] getBytes(); //将字符串转为字节数组 2、char\[\] toCharArray(int index); //将字符循环转换为字符数组 3、static String valueOf(char\[\]chs); //把字符数组转成字符串4、static String valueOf(int i); //将int类型数据转换成字符串 String的valueOf方法可以把任意类型的数据转换成字符串 4、String toLowerCase();//将字符串转成小写 5、String toUpperCase(); //将字符串转成大写 6、String concat(string str);把字符串拼接 \+拼接字符串更加强大,可以使用任意类型和字符串进行相加 concat调用和传入的参数都必须是字符串 五、字符串替换 1、String的替换功能 \[1\]String replace(char old,char new) \[2\]String replace(String old,String new) 2、String的取出字符串空格功能 String trim() 3、String的按字典顺序比较两个字符串 \[1\]int compareTo(Stringstr) \[2\]int compareToIgnoreCase(Stringstr) package com.cat.string; public class StringMethod { public static void main(String[] args) { /* * public String();空构造 * public String(byte[] bytes);把字符数组转成字符串 * public String(byte[] bytes,int index,int lenth);把字符数组一部分转成字符串 * public String(String original);把字符串常量值转成字符串 * */ String s1 = new String(); System.out.println(s1); byte[] arr1 = {97,98,99}; String s2 = new String(arr1); System.out.println(s2); byte[] arr2 = {97,98,99,100,101,102,103}; String s3 = new String(arr2,2,4); System.out.println(s3); //==和equals区别 demo1(); demo2(); demo3(); demo4(); /* * boolean equals(Object obj); //比较字符串内容是否相同,区分大小写 * boolean equalsIgnoreCase(String str); //比较字符串内容是否相同,忽略大小写 * boolean contains(String str); //判断大字符串中是否包含小字符串 * boolean startsWith(String str); //判断字符串是否以某个指定字符串开头 * boolean endsWith(String str); //判断字符串是否以某个指定字符串结尾 * boolean isEmpty(); //判断字符串是否为空 * */ /* * null和""区别 * ""是字符串常量,同时也是一个String类的对象,既然是对象当然可以调用String类中方法 * null是空常量,不能调用任何方法,否则会出现空指针异常,null常量可以给任意的引用数据类型赋值 * */ String s11 = "abc"; String s22 = ""; String s33 = null; System.out.println(s11.isEmpty()); System.out.println(s22.isEmpty()); // System.out.println(s33.isEmpty()); //如果是字符串常量和字符串变量作比较,通常都是使用字符串常量去调用字符串的方法(因为此时字符创常量不为null),防止空指针异常 if("abc".equals(s1)){ System.out.println("匹配成功"); } /* * int length(); //获取字符串长度 * char charAt(int index); //获取指定索引位置的字符 * int indexOf(int ch); //返回制定字符在此字符串中第一次出现处的索引 * int indexOf(String str); //返回指定字符串在此字符串中第一次出现处的索引 * int indexOf(int ch,int fromIndex); //返回指定字符在此字符串中从指定位置后第一次出现处的索引 * int indexOf(String str,int fromIndex); //返回指定字符串在此字符串中从指定位置后第一次出现处的索引 * lastIndexOf * String substring(int start); //从指定位置开始截取字符串,默认到末尾 * String substring(int start,int end); //从指定位置开始到指定结尾处截取字符串,默认到末尾 * */ demo11();//Shift+Ait+m(Windows);Windows+Alt+m(Mac) demo22(); demo33(); demo44(); //统计一个字符串中大写、小写,数字个数 forString(); /* * byte[] getBytes(); //将字符串转为字节数组 * char[] toCharArray(int index); //将字符循环转换为字符数组 * static String valueOf(char[] chs); //把字符数组转成字符串 * static String valueOf(int i); //将int类型数据转换成字符串 * String的valueOf方法可以把任意类型的数据转换成字符串 * String toLowerCase(); //将字符串转成小写 * String toUpperCase(); //将字符串转成大写 * String concat(string str);把字符串拼接 * +拼接字符串更加强大,可以使用任意类型和字符串进行相加 * concat调用和传入的参数都必须是字符串 * */ byteArray(); charArray(); //将一个整形数组转换为字符串 changeToString(); //遍历字符串两种方法 loopString(); /* * 1、String的替换功能 * [1]String replace(char old,char new) * [2]String replace(String old,String new) * 2、String的取出字符串空格功能 * String trim() * 3、String的按字典顺序比较两个字符串 * [1]int compareTo(String str) * [2]int compareToIgnoreCase(String str) * */ replaceString();//替换字符串 trimString();//去除两边空格 compareString(); } private static void compareString() { String s1 = "abc"; String s2 = "efg"; int d = s1.compareTo(s2); System.out.println(d); } private static void trimString() { String s = " admin admin "; String s1 = s.trim(); System.out.println(s1); } private static void replaceString() { System.out.println("================遍历字符串两种方法================="); String s = "adsjd"; String s1 = s.replace('d', 'b'); System.out.println(s1); String s2 = s.replace('e', 'b'); //不存在就不发生替换 System.out.println(s2); String s3 = s.replace("ad", "sb"); System.out.println(s3); } private static void changeToString() { int[] intA = {1,2,3,4,5}; String s = "["; for (int i = 0; i < intA.length; i++) { if (i == intA.length - 1) { s = s + intA[i] + "]"; }else{ s = s + intA[i] + ","; } } System.out.print(s); System.out.println(); } private static void loopString() { System.out.println("================遍历字符串两种方法================="); String s = "adsjd"; //将字符串中字符遍历 for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); System.out.print(c + " "); } System.out.println(); //先将字符串转换为字符数组 char[] cArr = s.toCharArray(); for (int i = 0; i < cArr.length; i++) { System.out.print(cArr[i] + " "); } System.out.println(); } private static void charArray() { String s = "abc"; char[] c = s.toCharArray(); for (int i = 0; i < c.length; i++) { System.out.print(c[i] + " "); } System.out.println(); } private static void byteArray() { System.out.println("================字符串转换================="); String s = "abc"; byte[] b = s.getBytes(); for(int i = 0;i < b.length;i++){ System.out.print(b[i] + " "); } System.out.println(); String s1 = "吃饭了"; byte[] b1 = s1.getBytes(); //通过GBK码表将字符串转换成字节数组 for(int i = 0;i < b1.length;i++){ // 编码: System.out.print(b1[i] + " ");//GBK码表一个中文代表两字节 } //GBK码表特点:中文第一个字节肯定是负数 System.out.println(); } private static void forString() { System.out.println("-----------统计字符串-----------"); String s = "WSFYDYWhdjjsdFGFs1542RRa21&*^&%(gsfd"; int big = 0; int small = 0; int num = 0; int other = 0; for(int i = 0;i < s.length();i++) { char c = s.charAt(i); if(c >= 'A' && c <= 'Z'){ big++; }else if(c >= 'a' && c <= 'z'){ small++; }else if(c >= '0' && c <= '9'){ num++; }else{ other++; } } System.out.println(s + "字符串中大写字符有" + big + "个,小写字符有" + small + "个,数字有" + num + "个,其他字符有" + other + "个"); } private static void demo1(){ String s1 = new String("abc");//创建了两个对象,一个在常量池中,一个在堆内存中 String s2 = new String("abc"); String s3 = "abc"; String s4 = "abc"; System.out.println(s1 == s2); System.out.println(s1.equals(s2)); System.out.println(s3 == s4); System.out.println(s3.equals(s4)); } private static void demo2(){ System.out.println("----------------------"); String s1 = new String("abc"); //记录堆中的地址值 String s2 = "abc"; //记录常量池中的地址值 System.out.println(s1 == s2); System.out.println(s1.equals(s2)); } private static void demo3(){ System.out.println("----------------------"); String s1 = "a" + "b" + "c"; String s2 = "abc"; //记录常量池中的地址值 System.out.println(s1 == s2); System.out.println(s1.equals(s2)); } private static void demo4(){ System.out.println("----------------------"); String s1 = "ab"; String s2 = s1 + "c"; String s3 = "abc"; //记录常量池中的地址值 System.out.println(s3 == s2); System.out.println(s3.equals(s2)); } private static void demo44() { System.out.println("-----------substring(int start,int end)-----------"); String s11 = "catanddoga"; String str = s11.substring(4); //从指定位置开始截取字符串,默认到末尾 System.out.println(str); String str1 = s11.substring(4,6); //从指定位置开始到指定结尾处截取字符串(包含头,不包含尾),左闭右开 System.out.println(str1); } private static void demo33() { System.out.println("-----------indexOf(int ch,int fromIndex)-----------"); String s11 = "catanddoga"; int index = s11.indexOf("a",4); //从指定索引处向后找 System.out.println(index); int index1 = s11.lastIndexOf("o"); //从后向前找第一个出现的字符 System.out.println(index1); } private static void demo22() { System.out.println("-----------indexOf(int ch)-----------"); String s11 = "catanddog"; int index = s11.indexOf("t"); //参数是int类型,传递char类型自动提升 System.out.println(index); int index1 = s11.indexOf("e"); //如果不存在返回-1 System.out.println(index1); int index2 = s11.indexOf("at"); //获取字符串中第一个字符出现的位置 System.out.println(index2); } private static void demo11() { System.out.println("-----------length()方法-----------"); String s11 = "catanddog"; String s22 = "买衣服,知道不?"; System.out.println(s11.length()); System.out.println(s22.length()); char c1 = s22.charAt(4); System.out.println(c1); // char c2 = s22.charAt(10); //java.lang.StringIndexOutOfBoundsException 字符串索引越界异常 // System.out.println(c2); } } ![Center][] ![Center 1][] ![Center 2][] [Center]: https://img-blog.csdn.net/20170224115557200?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTWluZ2dlUWluZ2NodW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center [Center 1]: https://img-blog.csdn.net/20170224115611449?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTWluZ2dlUWluZ2NodW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center [Center 2]: https://img-blog.csdn.net/20170224115621592?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvTWluZ2dlUWluZ2NodW4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
相关 JAVAString与基本类包装类转换与编码方式设置------JAVA > import java.io.UnsupportedEncodingException; > > public class StringMetho 短命女/ 2024年03月22日 13:25/ 0 赞/ 100 阅读
相关 JAVAString相关API测试------JAVA > public class StringTest > { > public static void main(String[] args) > 我就是我/ 2024年03月22日 03:34/ 0 赞/ 146 阅读
相关 七千字详解javaString类 ![在这里插入图片描述][90766b53830c4764971aed0be5ceb5f0.gif_pic_center] 文章目录 一、String类 Love The Way You Lie/ 2023年09月28日 22:36/ 0 赞/ 66 阅读
相关 javastring的长度_Java String类的操作—叩丁狼超全汇总 Java String类的操作—叩丁狼超全汇总 在应用程序中经常会用到字符串,所谓字符串就是指一连串的字符,它是由许多单个字符连接而成的,如多个英文字母所组成的一个英 朴灿烈づ我的快乐病毒、/ 2023年01月10日 13:15/ 0 赞/ 202 阅读
相关 JavaString与Date类型互转 1、转换方法 //String转换为Date String strDate="2017-07-19 08:29:00"; SimpleDateFormat sdf1=n 末蓝、/ 2022年06月12日 07:24/ 0 赞/ 540 阅读
相关 流关闭?blob无法转换为javastring类型? 当你数据库的数据类型为LONG(长字符串)或者blob,clob类型的时候,在执行查询的时候,mybati映射文件(就是你写sql的那个文件)无法直接将该类型的字段转为stri 超、凢脫俗/ 2022年05月14日 04:55/ 0 赞/ 328 阅读
相关 JavaString.split小结及四则表达式 现在一道题目如下: java中提供了对正则表达式的支持。 有的时候,恰当地使用正则,可以让我们的工作事半功倍! 如下代码用来检验一个四则运算式中数据项的数目,请填写划 ╰半夏微凉°/ 2022年03月15日 14:28/ 0 赞/ 358 阅读
相关 JavaString类 /\ 接口 接口中只有常量和抽象方法 常量 默认 类型public static final 接口中定义时,写不写都是这样 方法的默认类 矫情吗;*/ 2021年09月20日 20:16/ 0 赞/ 531 阅读
还没有评论,来说两句吧...