publicclassStringTest3{publicstaticvoidmain(String[] args){int[] arr ={1,2,3};String s =arrToString(arr);System.out.println(s);}publicstaticStringarrToString(int[] arr){if(arr ==null){return"";}if(arr.length ==0){return"[]";}String result ="[";for(int i =0; i < arr.length; i++){if(i == arr.length -1){
result += arr[i];}else{
result += arr[i]+", ";}}
result +="]";return result;}}
2. 使用StringBuilder
publicclassStringBuilderTest1{publicstaticvoidmain(String[] args){int[] arr ={1,2,3};String s =arrToString(arr);System.out.println(s);}publicstaticStringarrToString(int[] arr){StringBuilder sb =newStringBuilder();
sb.append("[");for(int i =0; i < arr.length; i++){if(i == arr.length -1){
sb.append(arr[i]);}else{
sb.append(arr[i]).append(", ");}}
sb.append("]");return sb.toString();}}
四、字符串反转
需求:定义一个方法,实现字符串反转。
键盘录入一个字符串,调用该方法后,在控制台输出结果。
例如:键盘录入abc,输出结果cba。
importjava.util.Scanner;publicclassStringTest4{publicstaticvoidmain(String[] args){Scanner scanner =newScanner(System.in);System.out.println("请输入一个字符串");String str = scanner.next();String reverser =reverser(str);System.out.println(reverser);}publicstaticStringreverser(String str){String result ="";for(int i = str.length()-1; i >=0; i--){char c = str.charAt(i);
result += c;}return result;}}
五、金额转换
需求:键盘录入一个七位以内的正整数,将整数转为中文描述,不足七位补零。
importjava.util.Scanner;publicclassStringTest5{publicstaticvoidmain(String[] args){Scanner sc =newScanner(System.in);// 获得一个数字int money;while(true){System.out.println("请输入一个金额:");
money = sc.nextInt();if(money >=0&& money <=9999999){break;}else{System.out.println("金额无效");}}// 把数字转成中文String moneyStr ="";while(money !=0){int ge = money %10;String capitalNumber =getCapitalNumber(ge);
moneyStr = capitalNumber + moneyStr;
money = money /10;}// 不满7位补零int count =7- moneyStr.length();for(int i =0; i < count; i++){
moneyStr ="零"+ moneyStr;}// 插入单位String[] arr ={"佰","拾","万","仟","佰","拾","元"};String result ="";for(int i =0; i < moneyStr.length(); i++){char c = moneyStr.charAt(i);
result = result + c + arr[i];}System.out.println(result);}publicstaticStringgetCapitalNumber(int number){String[] arr ={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};return arr[number];}}
publicclassStringTest7{publicstaticvoidmain(String[] args){String id ="123456202001011234";String year = id.substring(6,10);String month = id.substring(10,12);String day = id.substring(12,14);System.out.println("人物信息为:");System.out.println("出生年月日:"+ year +"年"+ month +"月"+ day +"日");char gender = id.charAt(16);// 把字符转成数字int num = gender -48;if(num %2==0){System.out.println("性别为:女");}else{System.out.println("性别为:男");}}}
八、敏感词替换
publicclassStringTest8{publicstaticvoidmain(String[] args){String talk ="你玩的真好,以后不要再玩了,TMD";System.out.println(talk);String[] arr ={"TMD","CNM","MLGB","SB"};for(int i =0; i < arr.length; i++){
talk = talk.replace(arr[i],"***");}System.out.println(talk);}}
importjava.util.Scanner;publicclassStringTest9{publicstaticvoidmain(String[] args){String str;Scanner scanner =newScanner(System.in);// 获取数字while(true){System.out.println("请输入一个数字:");
str = scanner.next();// 校验字符串是否满足规则boolean flag =checkStr(str);if(flag){break;}else{System.out.println("当前字符串不符合规则,请重新输入");}}// 字符串拼接StringBuilder sb =newStringBuilder();for(int i =0; i < str.length(); i++){char c = str.charAt(i);int number = c -48;String s =changeLuoMa(number);
sb.append(s).append(" ");}System.out.println(sb);}publicstaticStringchangeLuoMa(int number){String[] arr ={"","I","II","III","IV","V","VI","VII","VIII","IX"};return arr[number];}publicstaticbooleancheckStr(String str){if(str.length()>9){returnfalse;}for(int i =0; i < str.length(); i++){char c = str.charAt(i);if(c <'0'|| c >'9'){returnfalse;}}returntrue;}}
十一、调整字符串
给定两个字符串,A和B
A的旋转操作就是将A最左边的字符移动到最右边。
例如:若A=“abcde”,在移动一次之后结果就是:“bcdea”
如果在若干次调整操作之后,A能变成B,那么返回True
如果不能匹配成功,则返回false
publicclassStringTest10{publicstaticvoidmain(String[] args){StringA="abcde";StringB="cdeab";System.out.println("A = \"abcde\"");System.out.println("B = \"cdeab\"");System.out.println(check(A,B)?"True":"False");}publicstaticbooleancheck(String strA,String strB){for(int i =0; i < strA.length(); i++){
strA = strA.substring(1)+ strA.charAt(0);if(strA.equals(strB))returntrue;}returnfalse;}}
十二、打乱字符串
需求:输入一个字符串,并打乱顺序。
importjava.util.Random;importjava.util.Scanner;publicclassStringTest11{publicstaticvoidmain(String[] args){Scanner scanner =newScanner(System.in);System.out.println("请输入一个字符串:");String str = scanner.next();// 字符串转为字符数组char[] chars = str.toCharArray();// 打乱数组Random random =newRandom();for(int i =0; i < chars.length; i++){int randomIndex = random.nextInt(chars.length -1);char temp = chars[randomIndex];
chars[randomIndex]= chars[i];
chars[i]= temp;}// 把字符数组在变回字符串String result =newString(chars);System.out.println(result);}}
1.SigNoz介绍
github SigNoz SigNoz官方文档 2022 年 11 大 MYSQL 监控工具 MySQL | 六个最常用的 MySQL 数据库监控工具
2.SigNoz安装
从官方文档上得知使用以下命令进行安装:
git clone -b main https://github.com/SigNoz/signoz.git && cd signoz/d…