string例题
- 判断邮箱输入是否合法,并且判断输入的邮箱是否是新浪邮箱
private static void demo1() {
System.out.println("请输入您的邮箱");
String email = input.next();
String emailRegex = "^([a-zA-Z0-9]{1,})(@)([a-zA-Z0-9]{2,5})(.)(com)$";
System.out.println("输入邮箱格式: " + email.matches(emailRegex));
String business = email.replaceAll(emailRegex, "$3");
if ("sina".equals(business)) System.out.println("输入的邮箱为新浪邮箱");
}
- 判断字符串是否是回文字符串
private static void demo4() {
String str=input.nextLine();
StringBuilder builder=new StringBuilder(str);
builder=builder.reverse();
if(str.equals(builder.toString())) System.out.println(str+"是回文字符串");
}
- 忽略字符串所含字符的顺序,判断字符串是否一致
方法一:先将字符串转数组来排序
private static void demo3() {
String str1 = "ababcc";
String str2 = "abcabc";
int length = str1.length();
char[] chars1 = str1.toCharArray();
// for (int i = 0; i < length; i++) {
// chars1[i] = str1.charAt(i);
// }
Arrays.sort(chars1);
// for (int i = 1; i < length; i++) {
// for (int j = 0; j < length - i; j++) {
// if (chars1[j] > chars1[j + 1]) {
// char temp = chars1[j];
// chars1[j] = chars1[j + 1];
// chars1[j + 1] = temp;
// }
// }
// }
int length1 = str2.length();
char[] chars2 = new char[length1];
for (int i = 0; i < length1; i++) {
chars2[i] = str2.charAt(i);
}
Arrays.sort(chars2);
// for (int i = 1; i < length1; i++) {
// for (int j = 0; j < length - i; j++) {
// if (chars2[j] > chars2[j + 1]) {
// char temp = chars2[j];
// chars2[j] = chars2[j + 1];
// chars2[j + 1] = temp;
// }
// }
// }
str1 = new String(chars1);
str2 = new String(chars2);
if (str1.equals(str2)) System.out.println("输入字符串是一致");
}
方法二:利用stringbuilder来操作,查找相同的字符然后删除,最后看builder是否为空字符串
private static void demo5() {
String str="ababcc";
String str2="abcabc";
StringBuilder builder=new StringBuilder(str2);
int length = str.length();
for (int i = 0; i < length; i++) {
char c = str.charAt(i);
int i1 = builder.indexOf(String.valueOf(c));
if(i1==-1){
throw new RuntimeException("字符串不一致");
}
builder= builder.deleteCharAt(i1);
}
if(!builder.toString().isEmpty())throw new RuntimeException("字符串不一致");
System.out.println("字符串一致");
}
- 从控制台实现输入字符串路径,分别输出盘符、文件路径(不包括盘符)、文件名、文件类型:
利用截取:
private static void demo6() {
String path="E:\\欧阳路路\\student\\07欧阳路路\\IdeaProjects\\JavaSE\\day01\\day01.iml";
int i = path.indexOf("\\");
int j=path.lastIndexOf("\\");
String pan=path.substring(0,i);
String textPath=path.substring(i+1,j+1);
String type=path.substring(j+1);
System.out.println("pan:"+pan+"\ttextPath:"+textPath+"\ttype:"+type);
}//pan:E: textPath:欧阳路路\student\07欧阳路路\IdeaProjects\JavaSE\day01\ type:day01.iml
- 将数字数组,转换成字符串输出:
可以利用Arrays.toString也可以利用拼接来写
private static void demo7() {
Integer [] arr={1,2,3};
String str=new String();
StringBuilder builder=new StringBuilder(str);
for (int i = 0; i < arr.length; i++) {
builder.append(String.valueOf(arr[i]));
}
System.out.println(builder.toString());
//或者直接Arrays.toString(arr)
}
- 给定一个字符串查找“nba”出现的次数:
方法一:利用nba出现的索引位置来查找,并且每次找到之后起始索引值需要在nba出现位置的索引值加3:
private static void demo8() {
String str="hanbasdnbafllgnbahjnbakqqqqlnbaxi";
StringBuilder builder=new StringBuilder(str);
int count=0;
int len=str.length();
int index=0;
for (int i = 0; i <len; i++) {
index=str.indexOf("nba",index);
if(index==-1) break;
count++;
index+=3;
}
System.out.println(count);
}
方法二:利用字符串来分割,要注意末尾是否有需要查找的字符串,因此控制匹配的次数应尽可能的多,最后分割成的数组个数减一就为字符串所出现的次数
private static void demo5() {
String str="hanbasdnbafllgnbahjnbakqqqqlnbaxi";
String[] nbas = str.split("nba", -1);
int i = nbas.length - 1;
System.out.println(i);
}
方法三:
利用replace替换,将nba替换成**,可见从三个长度到两个长度,缺少的长度就是nba字符串出现的次数
private static void demo9() {
String str="hanbasdnbafllgnbahjnbakqqqqlnbaxinba";
String replaceStr = str.replaceAll("nba", "**");
System.out.println(str.length()-replaceStr.length());
}
- 去除一个字符串里面的所有空格
private static void demo4() {
String str=" a dc j k ";
System.out.println(str.replaceAll("\\s+", ""));
}
- 让用户在控制台不断录入英文字母(每次录入两个字母即可),知道总长度超过六个停止,然后统计一下一共有几个a:
private static void demo6() {
String s = "";
StringBuilder builder = new StringBuilder(s);
while (builder.length() <6) {
System.out.println("请录入两个字母");
s = input.nextLine();
if (!s.matches("^[a-zA-Z]{2}$")) {
System.out.println("输入错误,程序退出");
continue;//跳过下面拼接继续循环
}
builder.append(s);
}
String s1 = builder.toString().toLowerCase();
String[] as = s1.split("a", -1);
System.out.println("一共有"+(as.length-1)+"个a");
}
- 将输入功能封装,并且用正则控制输入内容
创建一个输入类,将构造私有化,属性input私有化,直接用类名调用属性,将控制输入的方法,写入,并用正则控制内容,注意返回值类型
public class InputUtil {
private InputUtil() {
}
private static Scanner input;
static {
input = new Scanner(System.in);
}
public static int inputStr(String meg, String regex) {
System.out.println(meg);
String next="";
while (true) {
next=input.nextLine();
if (!next.matches(regex)) System.out.println("the way you input is wrong ,please input again");
else break;
}
return Integer.valueOf(next);
}
}
- 现有一段字符串,将字符串去重:
private static void demo8() {
String str="我我.。。。.我。。要..学..学.学学.编.。。编编程..程程";
System.out.println(str.replaceAll("\\.+", "").replaceAll("。","").replaceAll("(.)\\1+","$1"));
}