搜索

JAVA---String,StringBuilder方法练习


发布时间: 2022-11-24 18:21:07    浏览次数:28 次
package Xiti01;

import lombok.Cleanup;
import sm.util.InputUtil;

import java.util.Arrays;
import java.util.Scanner;

public class Demo04 {
public static void main(String[] args) {
test11();

}

private static void test11() {
//去除重复
String str = "鸡鸡鸡鸡.......你你.....太太太太....美美美美...";
System.out.println(str.replaceAll("\\.+", "").replaceAll("(.)\\1+","$1"));
}

private static void test10() {
//用户录入文件例如as.jpg a1.txt等可多次录入,最后输出以jpg结尾的文件
Scanner input = new Scanner(System.in);
StringBuilder builder = new StringBuilder();
String regex = "^[\\w]+\\.[a-z]{2,}$";
String next = null;
String answer = null;
do {
while (true) {
System.out.println("请录入文件:");
next = input.next();
if (!next.matches(regex)) {
System.out.println("录入不合法,请重新录入");
continue;
}
builder.append(next);
builder.append(",");
break;
}
System.out.println("是否继续录入:y/n");
answer = input.next();
} while ("y".equals(answer));
String s1 = builder.toString();
String[] split = s1.split(",");
for (String s : split) {
if (s.endsWith(".jpg")) {
System.out.println(s);
}
}
}

private static void test9() {


String str = "awda.jpg,wda,spn,11.jpg,sada.txt";
String[] split = str.split(",");
for (String s : split) {
if (s.endsWith(".jpg")) {
System.out.println(s);
}
}

}

private static void test8() {
System.out.println("欢迎使用共享单车系统");
System.out.println("1.投放单车");
System.out.println("2.查看全部单车");
System.out.println("3.删除单车");
System.out.println("4.借出单车");
System.out.println("5.归还单车");
System.out.println("6.排行榜");
System.out.println("7.退出");

test7();
}

private static void test7() {
int chose = InputUtil.inputStr("请选择操作", "^[1-7]{1}$");
}

private static void test6() {
//每次录入两个字母,可多次录入,最后判断a的个数
@Cleanup
Scanner input = new Scanner(System.in);
String regex = "^[A-Za-z]{2}$";
StringBuilder builder = new StringBuilder();

while (builder.length() < 6) {
System.out.println("每次录入两个字母");
String str = input.nextLine();
if (!str.matches(regex)) {
System.out.println("录入不合法,请重新录入");
continue;
}
builder.append(str);

}
String str2 = builder.toString();
System.out.println(str2);
str2 = str2.toLowerCase();
String str3 = str2.replaceAll("a", "");
System.out.println("a的个数为:" + (builder.length() - str3.length()));
}

private static void test5() {
//求字符串里面有多少个abc
String str = "abcdasdsabcasdwadabcabcdasaw";
String s = str.replaceAll("abc", "HH");
int i = str.length() - s.length();
System.out.println(i);
String[] a1 = str.split("abc", -1);
System.out.println(a1.length - 1);
int count = 0;
int fromIndex = 0;
while (true) {
int index = str.indexOf("abc", fromIndex);
if (index == -1) {
break;
}
fromIndex = index;
fromIndex = fromIndex + 3;
count++;
}
System.out.println(count);

}

private static void test4() {
//去除字符串之间的空格
String str = " a b c d ";
String s = str.replaceAll("\\s+", "");
System.out.println(s);

}

private static void test3() {
//给定数组将数组元素拼接成字符串,不使用toString()
int[] arr = {1, 2, 3};
//使用toString()
System.out.println(Arrays.toString(arr));
//未使用
StringBuilder stringBuilder = new StringBuilder();
int length = stringBuilder.length();
stringBuilder.append("[");
for (int i = 0; i < arr.length; i++) {
stringBuilder.append(arr[i]);
if (i == arr.length - 1) {
stringBuilder.append("]");
break;
} else {
stringBuilder.append(", ");
}
}
System.out.println(stringBuilder);

}

private static void test2() {
//获得文件信息:盘,文件路径,文件名,txt
String path = "D:\\yuanshen\\原神\\Genshin Impact\\yuanshen.txt";
int indexOf = path.indexOf("\\");
int lastIndexOf1 = path.lastIndexOf("\\");
int lastIndexOf2 = path.lastIndexOf(".");
System.out.println("盘:" + path.substring(0, indexOf));
System.out.println("文件路径:" + path.substring(indexOf + 1, lastIndexOf1 + 1));
System.out.println("文件名:" + path.substring(lastIndexOf1 + 1, lastIndexOf2));
System.out.println("txt:" + path.substring(lastIndexOf2 + 1));
}

private static void test1() {
String str1 = "dasda";
String str2 = "dasda";

char[] chars1 = str1.toCharArray();
char[] chars2 = str2.toCharArray();
boolean flag = Arrays.toString(chars1).equals(Arrays.toString(chars2));
if (!flag) {
System.out.println("不相等");
} else System.out.println("相等");
}
}
免责声明 JAVA---String,StringBuilder方法练习,资源类别:文本, 浏览次数:28 次, 文件大小:-- , 由本站蜘蛛搜索收录2022-11-24 06:21:07。此页面由程序自动采集,只作交流和学习使用,本站不储存任何资源文件,如有侵权内容请联系我们举报删除, 感谢您对本站的支持。 原文链接:https://www.cnblogs.com/wang1999an/p/16809657.html