查找“asdfjvjadsffvaadfkfasaffdsasdffadsafafsafdadsfaafd,该字符串中有多少个af”
// String str = "asdfjvjadsffvaadfkfasaffdsasdffadsafafsafdadsfaafd";
// Pattern p = Pattern.compile("af");
// Matcher m = p.matcher(str);
// int sum = 0;
// while(m.find()){
// sum++;
// }
// System.out.println(sum);
输入任意一个字符串,如:“abDEe23dJfd343dPOddfe4CdD5ccv!23rr”。取出该字符串中所有的字母。顺序不能改变!并把大写字母变成小写,小写字母变成大写!
String str1 = "abDEe23dJfd343dPOddfe4CdD5ccv!23rr";
String str22 = str1.replaceAll("([^a-zA-Z]|)", "");
StringBuffer buf = new StringBuffer(str22.length());
String upper = str22.toUpperCase();
String lower = str22.toLowerCase();
for(int i = 0;i<str22.length();i++){
if(str22.charAt(i)==upper.charAt(i)){
buf.append(lower.charAt(i));
}else{
buf.append(upper.charAt(i));
}
}
// System.out.print("请输入第一个字符串:");
// Pattern tmp = Pattern.compile(scan.nextLine().trim());
// System.out.print("请输入第二个字符串:");
// String b = scan.nextLine().trim();
// Matcher end = tmp.matcher(b);
// if(end.find()){
// System.out.println(tmp+"包含在"+b);
// }else{
// System.out.println("不包含");
String str2 = "want you to know one thing";
Pattern tmp1 = Pattern.compile("n");
Pattern tmp2 = Pattern.compile("o");
Matcher sb = tmp1.matcher(str2);
int i = 0;
while(sb.find()){
i++;
}
System.out.println("n出现的次数"+i);
Matcher sb1 = tmp2.matcher(str2);
int j = 0;
while(sb1.find()){
j++;
}
System.out.println("o出现的次数"+j);
// String str = "asdfjvjadsffvaadfkfasaffdsasdffadsafafsafdadsfaafd";
// Pattern p = Pattern.compile("af");
// Matcher m = p.matcher(str);
// int sum = 0;
// while(m.find()){
// sum++;
// }
// System.out.println(sum);
输入任意一个字符串,如:“abDEe23dJfd343dPOddfe4CdD5ccv!23rr”。取出该字符串中所有的字母。顺序不能改变!并把大写字母变成小写,小写字母变成大写!
String str1 = "abDEe23dJfd343dPOddfe4CdD5ccv!23rr";
String str22 = str1.replaceAll("([^a-zA-Z]|)", "");
StringBuffer buf = new StringBuffer(str22.length());
String upper = str22.toUpperCase();
String lower = str22.toLowerCase();
for(int i = 0;i<str22.length();i++){
if(str22.charAt(i)==upper.charAt(i)){
buf.append(lower.charAt(i));
}else{
buf.append(upper.charAt(i));
}
}
System.out.println(buf);
// System.out.print("请输入第一个字符串:");
// Pattern tmp = Pattern.compile(scan.nextLine().trim());
// System.out.print("请输入第二个字符串:");
// String b = scan.nextLine().trim();
// Matcher end = tmp.matcher(b);
// if(end.find()){
// System.out.println(tmp+"包含在"+b);
// }else{
// System.out.println("不包含");
// }
String str2 = "want you to know one thing";
Pattern tmp1 = Pattern.compile("n");
Pattern tmp2 = Pattern.compile("o");
Matcher sb = tmp1.matcher(str2);
int i = 0;
while(sb.find()){
i++;
}
System.out.println("n出现的次数"+i);
Matcher sb1 = tmp2.matcher(str2);
int j = 0;
while(sb1.find()){
j++;
}
System.out.println("o出现的次数"+j);