package com.pl;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.regex.Pattern;
public class css_5 {
public static void main(String[] args) {
//m1();
StringBuilder sbu = new StringBuilder();
sbu.append("hello");
sbu.append(18);
sbu.insert(1, "Welcome - ");
//sbu.append("Welcome - ");
//sbu.append("Welcome - ",0,"Welcome - ".length());
System.out.println(sbu);
System.out.println(sbu.toString().concat("java"));
sbu.delete(0, 5);
System.out.println(sbu);
//错误的
//String s = sbu;
//String 转换为 StringBuilder
String s1 = "hello";
StringBuilder ss1 = new StringBuilder(s1);
System.out.println(s1);
System.out.println(ss1);
System.out.println(ss1.toString());
System.out.println(ss1.reverse().append("java.18").toString().toUpperCase());
//StringBuilder 转换为 String
String s2 = ss1.toString();
System.out.println(s2);
StringBuilder su1 = new StringBuilder();
StringBuilder su2 = new StringBuilder(6);
StringBuilder su3 = new StringBuilder("hello");
System.out.println(su1);
System.out.println(su1.length());
System.out.println(su2);
System.out.println(su2.length());
System.out.println(su3);
System.out.println(su3.length());
StringBuffer suf1 = new StringBuffer();
StringBuffer suf2 = new StringBuffer(6);
StringBuffer suf3 = new StringBuffer("hello");
System.out.println(suf1);
System.out.println(suf1.length());
System.out.println(suf2);
System.out.println(suf2.length());
System.out.println(suf3);
System.out.println(suf3.length());
System.out.printf("\n=========================================");
String str = "hello16java16mysql8html";
System.out.println(str);
//删除指定的数字
System.out.println(str.replace("6", ""));
//删除所有数字
for (int i = 0; i <= 9; i++) {
str = str.replace(String.valueOf(i), "");
}
System.out.println(str);
//------------使用正则表达式,删除所有数字
String str2 = "hello16789java16mysql8html";
System.out.println(str2);
//hello16java16mysql8html
//这两个方法,支持正则表达式 \\d 代表数字 \\D 代表非数字
//String.replaceAll() String.replaceFirst();
System.out.println(str2.replaceAll("\\d", ""));//删除字符串数字内容
//hellojavamysqlhtml
System.out.println(str2.replaceAll("\\D", ""));//删除不是数字的内容
//16168
//删除首次出现的字符串匹配删除 \\d+
System.out.println(str2.replaceFirst("\\d+", ""));
String[] langs = str2.split("\\d+");
System.out.println(Arrays.toString(langs));
// String 支持正则表达式方法
// .replaceAll()
// .replaceFirst()
// .split()
// .matches()
System.out.printf("\n=========================================");
//判断字符串有没有大写
String strx = "hello";
//.*[A-Z].*
// .代表一个任意符号
if (strx.matches("^.*[A-Z].*$")) {
System.out.println(String.format("%s:有大写字母", strx));
} else {
System.err.println(String.format("%s:没有大写字母", strx));
}
// 判断字符串有没有数字
System.out.println("hello".matches("^.*\\d.*$"));
//判断有没有汉字
System.out.println("china".matches("^.*[\u4e00-\u9fa5].*$"));
// 判断是不是手机号
String phone = "13014577066";
String pattern = "^1[3,5,8]\\d{9}$";
if (phone.matches(pattern)) {
System.out.println("yes");
} else {
System.out.printf("手机号:%s不是合法的手机号。 \n", phone);
}
// 判断是不是纯中文
String name = "jack";
String p = "^[\u4e00-\u9fa5]+$";
if (name.matches(p)) {
System.out.println(name);
} else {
System.out.println("姓名必须为纯中文");
}
String strv = "他的手机号是:13014577033,他在郑州。";
System.out.println(strv.matches(".*\\d.*"));
//判断str是不是标准的手机号格式
System.out.println(strv.matches("1[358]\\d{9}"));
//判断字符串有没有手机号
System.out.println(strv.matches(".*1[358]\\d{9}.*"));
//是不是中文是不是一个汉字 ^开头 $代表结尾 {1}
System.out.println("中文".matches("^[\u4e00-\u9fa5]$"));//false
//+ {1,}
System.out.println("中文".matches("^[\u4e00-\u9fa5]+$"));//true
System.out.println("中文".matches("[\u4e00-\u9fa5]+"));//true
System.out.println("--".repeat(20));
//* 代表是 {0,}
System.out.println("中文a".matches("[\u4e00-\u9fa5]*"));//false
System.out.println("".matches("[\u4e00-\u9fa5]*"));//true
// ? {0,1}
System.out.println("中".matches("[\u4e00-\u9fa5]?"));//true
System.out.println("中文".matches("[\u4e00-\u9fa5]{2,6}"));//true
System.out.println("**".repeat(25));
//是不是全英文
System.out.println("abc".matches("[a-zA-Z]+"));//true
System.out.println("ab c".matches("[a-zA-Z]+"));//false
System.out.println("ab123c".matches("[a-zA-Z]+"));//false
System.out.println("abDDDc".matches("[a-zA-Z]{1,}"));//true
System.out.println("___".repeat(20));
//是不是全数字 "\\d+" "\\d*"
System.out.println("0000".matches("\\d"));//false
System.out.println("0000".matches("\\d+"));//true
System.out.println("0000".matches("\\d?"));//false
System.out.println("0000".matches("\\d*"));//true
// for (int i = 0; i < sbu.length(); i++) {
// System.out.printf("\n=======" + sbu[i]);
//
// }
System.out.printf("\n=========================================");
//java.util.regex.Pattern
//var p1 = Pattern.compile("\\d");
var p1 = Pattern.compile("[a-z]{1,}");
//var p2 = Pattern.compile("[a-zA-Z]{1,}");
var p2 = Pattern.compile("[a-z]{1,}", Pattern.CASE_INSENSITIVE);
//var p2 = Pattern.compile("[a-z]{1,}", 2);
//输出正则表达式,字符串
//System.out.println(p1);
//System.out.println(p1.toString());
//System.out.println(p1.pattern());
//java.util.regex.Matcher
var matcher = p2.matcher("java123mysql8html5javascriptcn.webrx.Book-student");
while (matcher.find()) {
System.out.printf("%s%n", matcher.group());
}
//123456789
//12+3+4+56-7-8-9 = 100
Set<String> set = new HashSet<String>();
int index = 0;
while (true) {
String[] s1x = {"", "+", "-"};
Random rand = new Random();
StringBuilder sbux = new StringBuilder("1");
for (int i = 2; i <= 9; i++) {
sbux.append(s1x[rand.nextInt(s1x.length)]);
sbux.append(i);
}
//System.out.printf("%s%n", sbu);
//[+-]?\\d+
var p3 = Pattern.compile("[+-]*[0-9]+");
var m3 = p3.matcher(sbu);
int sum = 0;
while (m3.find()) {
int num = Integer.parseInt(m3.group());
sum += num;
}
if (sum == 100 && !set.contains(sbu.toString())) {
System.out.printf("%d、%s = 100%n", ++index, sbu);
set.add(sbu.toString());
}
if (set.size() == 11) {
break;
}
}
for (String s : set) {
System.out.printf("%s = 100%n", s);
}
}
}
java string
于 2023-10-21 16:45:27 首次发布
9236

被折叠的 条评论
为什么被折叠?



