Java常用函数-String类的函数
2021/7/17 20:08:38
本文主要是介绍Java常用函数-String类的函数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
记录:NO.253
场景:体验java.lang.String的JDK版本:jdk1.8.0_181。
一、基本信息
java.lang.String处理字符串的类,是一个final类。
java.lang.String实现如下接口:
java.io.Serializable
java.lang.Comparable
java.lang.CharSequence
java.lang.String存储值的属性名value[]
private final char value[];
二、静态方法
1.数组char[]转换为字符串
char[] chrStr = new char[]{'福','州','在','中','国','的','福','建'}; String staticOut1 = String.copyValueOf(chrStr); System.out.println("staticOut1 = " + staticOut1); String staticOut2 = String.copyValueOf(chrStr,3,5); System.out.println("staticOut2 = " + staticOut2);
2.格式化字符串
String staticOut3 = String.format("地级市: %s;\n输出16进制数字: %X.","Fuzhou",88); System.out.println("staticOut3 = " + staticOut3);
3.使用分隔符连接字符串
String staticOut4 = String.join(";","福州","厦门","泉州"); System.out.println("staticOut4 = " + staticOut4);
4.基本类型转换为String类型
String staticOut5 = String.valueOf(true); System.out.println("staticOut5 = " + staticOut5);; String staticOut6 = String.valueOf('a'); System.out.println("staticOut6 = " + staticOut6); char[] chrStr2 = new char[]{'福','州','在','中','国','的','福','建'}; String staticOut7 = String.valueOf(chrStr2); System.out.println("staticOut7 = " + staticOut7); String staticOut8 = String.valueOf(chrStr2,3,5); System.out.println("staticOut8 = " + staticOut8); double doubleNum = 89.123456789d; String staticOut9 = String.valueOf(doubleNum); System.out.println("staticOut9 = " + staticOut9); float floatNum = 99.123456789f; String staticOut10 = String.valueOf(floatNum); System.out.println("staticOut10 = " + staticOut10); int intNum = 999999999; String staticOut11 = String.valueOf(intNum); System.out.println("staticOut11 = " + staticOut11); long longNum = 999999999999999999l; String staticOut12 = String.valueOf(longNum); System.out.println("staticOut12 = " + staticOut12); Object obj = "福建的省会是福州"; String staticOut13 = String.valueOf(obj); System.out.println("staticOut13 = " + staticOut13);
三、非静态方法
1.字符串替换
String out31 =str.replace("厦门","泉州"); System.out.println("out31 = " + out31); String out32 =str.replaceAll("厦门","泉州"); System.out.println("out32 = " + out32); String out33 =str.replaceAll("厦门","泉州"); System.out.println("out33 = " + out33);
2.字符串分割
String str6 = "厦门;泉州;福州"; String[] out34 =str6.split(";"); System.out.println("out34 = " + out34[0]); String[] out35 =str6.split(";",2); System.out.println("out34 = " + out35[0]+" "+ out35[1]);
3.判断字符串是否以指定的前缀开头后缀结尾
boolean out10 = str.endsWith("福建"); System.out.println("out10 = " + out10); String str7 = "厦门;泉州;福州"; boolean out36 =str7.startsWith("厦门"); System.out.println("out36 = " + out36); boolean out37 =str7.startsWith("泉州",3); System.out.println("out37 = " + out37);
4.截取字符串
String str8 = "厦门;泉州;福州"; String out38 = (String) str8.subSequence(3,5); System.out.println("out38 = " + out38); String out39 = str8.substring(3); System.out.println("out39 = " + out39); String out40 = str8.substring(3,5); System.out.println("out40 = " + out40);
5.字符串转换为char数组
String str9 = "厦门;泉州;福州"; char[] out41 = str9.toCharArray(); System.out.println("out41 = " + out41[0]);
6.字母大小写转换
String str10 = "FujianXiaMen"; String out42 = str10.toLowerCase(); System.out.println("out42 = " + out42); String out43 = str10.toUpperCase(); System.out.println("out43 = " + out43);
7.删除字符串两端空格
String str11 = " FujianXiaMen "; String out44 = str11.trim(); System.out.println("out44 = " + out44);
8.字符匹配
boolean out28 =str.matches("福建"); System.out.println("out28 = " + out28); String str5 = "Fujian In China"; boolean out29 =str5.regionMatches(true,2,"Fujian",0,6); System.out.println("out29 = " + out29); boolean out30 =str5.regionMatches(2,"Fujian",0,6); System.out.println("out30 = " + out30);
9.计算字符串长度
int out27 =str.length(); System.out.println("out27 = " + out27);
10.字符和字符串的位置索引
String str = "厦门在中国的福建"; //返回 char指定索引处的值 char out1 = str.charAt(3); System.out.println("out1 = " + out1); //返回指定索引处的字符(Unicode代码点) int out2 = str.codePointAt(3); System.out.println("out2 = " + out2); //返回指定索引之前的字符(Unicode代码点) int out3 = str.codePointBefore(3); System.out.println("out3 = " + out3); //返回String指定文本范围内的Unicode代码点数 int out4 = str.codePointCount(3,5); System.out.println("out4 = " + out4); //返回指定字符第一次出现的字符串内的索引 String str4="ABCdBC"; int out17 =str4.indexOf(66); System.out.println("out17 = " + out17); int out18 =str4.indexOf(66,2); System.out.println("out18 = " + out18); //返回指定子字符串第一次出现的字符串内的索引 int out19 =str.indexOf("中国"); System.out.println("out19 = " + out19); int out20 =str.indexOf("中国",2); System.out.println("out20 = " + out20); //返回指定字符的最后一次出现的字符串中的索引 int out23 =str4.lastIndexOf(66); System.out.println("out23 = " + out23); int out24 =str4.lastIndexOf(66,2); System.out.println("out24 = " + out24); //返回指定子字符串第一次出现的字符串内的索引 int out25 =str.lastIndexOf("中国"); System.out.println("out25 = " + out25); int out26 =str.lastIndexOf("中国",2); System.out.println("out26 = " + out26);
11.判断字符串是否为空
boolean out22 =str.isEmpty(); System.out.println("out22 = " + out22);
12.字符串的哈希码
int out16 =str.hashCode(); System.out.println("out16 = " + out16);
13.返回字符串对象的规范表示
String out21 =str.intern(); System.out.println("out21 = " + out21);
14.字符串中的字符复制到目标字符数组
char[] out15 = new char[10]; String str3 = "厦门在中国的福建"; str3.getChars(0,8,out15,0); System.out.println("out15 = " + out15[0]);
15.字符串转换为byte[]
byte[] out13 = "Abcdefg".getBytes(); System.out.println("out13 = " + out13[0]); byte[] out14 = new byte[0]; try { out14 = "Abcdefg".getBytes("GBK"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } System.out.println("out14 = " + out14[0]);
16.字符串连接
String out7 = str.concat(",福州也在中国的福建"); System.out.println("out7 = " + out7);
17.字符串比较
//按字典顺序比较两个字符串 int out5 = str.compareTo("福州在中国的福建"); if(out5 ==0){ System.out.println("out5 = " + "两字符相等."); }else { System.out.println("out5 = " + "两字符不相等."); } //按字典顺序比较两个字符串,忽略病例差异 int out6 = str.compareToIgnoreCase("福州在中国的福建"); if(out6 ==0){ System.out.println("out6 = " + "两字符相等."); }else { System.out.println("out6 = " + "两字符不相等."); } //当且仅当此字符串包含指定的char值序列时才返回true boolean out8 = str.contains("福建"); System.out.println("out8 = " + out8); //将此字符串与指定的CharSequence进行比对 boolean out9 = str.contentEquals("福建"); System.out.println("out9 = " + out9); //将此字符串与指定对象进行比较 boolean out11 = str.equals("福建"); System.out.println("out11 = " + out11); //将此 String与其他 String比 String str2 = "AbcdEfG"; boolean out12 = str2.equalsIgnoreCase("aBCdEfG"); System.out.println("out12 = " + out12);
以上,感谢。
这篇关于Java常用函数-String类的函数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-27消息中间件底层原理资料详解
- 2024-11-27RocketMQ底层原理资料详解:新手入门教程
- 2024-11-27MQ底层原理资料详解:新手入门教程
- 2024-11-27MQ项目开发资料入门教程
- 2024-11-27RocketMQ源码资料详解:新手入门教程
- 2024-11-27本地多文件上传简易教程
- 2024-11-26消息中间件源码剖析教程
- 2024-11-26JAVA语音识别项目资料的收集与应用
- 2024-11-26Java语音识别项目资料:入门级教程与实战指南
- 2024-11-26SpringAI:Java 开发的智能新利器