Java:String、StringBuilder、reverse反转、append拼接、遍历、统计
2022/5/2 1:13:36
本文主要是介绍Java:String、StringBuilder、reverse反转、append拼接、遍历、统计,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import java.util.Scanner; /* * Scanner:用于获取键盘录入数据 * public String nextline():获取键盘录入字符串数据 */ public class ScannerLearn { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入数据:"); String line = sc.nextLine(); System.out.println("您输入的数据是:"+line); /* * String构造方法 * public String() 创建空白字符串对象 * public String(char[] chs) 根据字符数组的内容,来创建字符串对象 * public String(byte[] bys) 根据字节数组的内容,来创建字符串对象 * String s = "abc" 直接赋值(推荐使用)*/ String s1 = new String(); System.out.println("s1:"+s1); //s1: char [] chs = {'a','b','c'}; String s2 = new String(chs); System.out.println("s2:"+s2); //s2:abc byte[] bys = {97,98,99}; //97字节底层对应的就是a String s3 = new String(bys); System.out.println("s3:"+s3); //s3:abc //以下存储内存地址不一样 String s4 = new String(chs); String s5 = new String(chs); //以下存储内存地址一样 String s6 = "abc"; String s7 = "abc"; //比较的为地址值 System.out.println(s4==s5); //false System.out.println(s6==s7); //true System.out.println(s4==s6); //false //比较数据值 System.out.println(s4.equals(s5)); //true System.out.println(s6.equals(s7)); //true System.out.println(s4.equals(s6)); //true //遍历字符串 String name = "YinRuMin"; for(int i=0;i<name.length();i++){ System.out.println(name.charAt(i)); } //统计字符次数,大小写和数字分别统计 //Scanner ss = new Scanner(System.in); System.out.println("请输入字符串:"); String word = sc.nextLine(); int uw = 0; int lw = 0; int dig = 0; for(int i=0;i<word.length();i++){ if(word.charAt(i)>='A'&&word.charAt(i)<='Z'){ uw+=1; }else if(word.charAt(i)>='a'&&word.charAt(i)<='z'){ lw+=1; }else if(word.charAt(i)>='0'&&word.charAt(i)<='9'){ dig+=1; } } System.out.println("大写字母有:"+uw+"个"); System.out.println("小写字母有:"+lw+"个"); System.out.println("数字字符有:"+dig+"个"); //StringBuild StringBuilder sb = new StringBuilder(); System.out.println("sb:"+sb); System.out.println("sb.length():"+sb.length()); StringBuilder sb2 = new StringBuilder("abdcdd"); System.out.println("sb2:"+sb2); System.out.println("sb2.length():"+sb2.length()); StringBuilder sb3 = sb2.append("eee"); System.out.println("sb2:"+sb2); //sb2:abdcddeee System.out.println("sb3:"+sb3); //sb2:abdcddeee System.out.println(sb2==sb3); //true sb.append(100); sb.append("yin"); sb.append("xxx"); System.out.println("sb:"+sb); //sb:100yinxxx sb.append(22).append("ccc").append("fff"); //链式拼接 System.out.println("sb:"+sb); //sb:100yinxxx22cccfff sb.reverse(); //反转 System.out.println("sb:"+sb); //sb:fffccc22xxxniy001 //String转换为StringBuilder String sb6 = "Iverson"; StringBuilder sb7 = new StringBuilder(sb6); System.out.println("sb7:"+sb7); //StringBuilder转换为String StringBuilder sb4 = new StringBuilder(); sb4.append("Iverson"); //String sb5 = sb4;错误方法 String sb5 = sb4.toString(); System.out.println("sb5:"+sb5); //拼接字符串 int[] arr = {1,2,3,4,5,6}; String s = arrayToString(arr); //普通拼接 System.out.println("s:"+s); //s:[1, 2, 3, 4, 5, 6] String sb8 = arrayToString2(arr); //StringBuilder拼接 System.out.println("sb8:"+sb8); //sb8:[1,2,3,4,5,6] } public static String arrayToString(int[] arr){ String s = ""; s +="["; for(int i = 0;i<arr.length;i++){ if (i==arr.length-1){ s+=arr[i]; }else { s+=arr[i]; s+=", "; } } s+="]"; return s; } public static String arrayToString2(int[] arr){ StringBuilder s = new StringBuilder(); s.append("["); for(int i = 0;i<arr.length;i++){ if (i==arr.length-1){ s.append(arr[i]); }else { s.append(arr[i]).append(","); } } s.append("]"); String sb = s.toString(); return sb; } }
这篇关于Java:String、StringBuilder、reverse反转、append拼接、遍历、统计的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26Mybatis官方生成器资料详解与应用教程
- 2024-11-26Mybatis一级缓存资料详解与实战教程
- 2024-11-26Mybatis一级缓存资料详解:新手快速入门
- 2024-11-26SpringBoot3+JDK17搭建后端资料详尽教程
- 2024-11-26Springboot单体架构搭建资料:新手入门教程
- 2024-11-26Springboot单体架构搭建资料详解与实战教程
- 2024-11-26Springboot框架资料:新手入门教程
- 2024-11-26Springboot企业级开发资料入门教程
- 2024-11-26SpringBoot企业级开发资料详解与实战教程
- 2024-11-26Springboot微服务资料:新手入门全攻略