
public class StringTest1 {
public static void trim(String str) {
//删除首位的空格以及中间含有的数字 若想删除所有的空格,则 ^\s+|\s+|\s+$
str = str.replaceAll("^\s+|\s+$|\d+","");
System.out.println(str);
}
public static void main(String[] args) {
String s = " hello12312312 world sad a1123";
//用正则表达式进行处理
trim(s);
}
}
import java.util.Scanner;
public class StringTest2 {
public static void main(String[] args) {
String s = "abcdefg";
String s1 = "";
StringBuilder sb = new StringBuilder(s);
System.out.println("请输入想要指定反转的位置(start 开始的位置 end 结束的位置)");
Scanner sc = new Scanner(System.in);
int start = sc.nextInt();
int end = sc.nextInt();
sb.reverse();
// System.out.println(sb.substring(sb.length() - end, sb.length() - start + 1));
s1 = s.substring(0, start - 1) + sb.substring(sb.length() - end, sb.length() - start + 1) + s.substring(end, s.length());
System.out.println(s1);
// System.out.println(s.substring(start - 1, end));
}
}
public class StringTest3 {
//思路:先将小串进行长度依次递减与大串进行比较
public static String compare(String s1, String s2) {
//先比较两个字符串哪个是大串,哪个是小串
String max = "";
String min = "";
max = (s1.length() > s2.length()) ? s1 : s2;
min = (max == s1) ? s2 : s1;
//接收找到的所有相同的字符串
ArrayList arr = new ArrayList();
for (int i = 0; i < min.length(); i++) {
for (int j = min.length(); j > i; j--) {
if (max.contains(min.substring(i, j))) {
arr.add(min.substring(i, j));
}
}
}
//判断接收到的字符串中最大的字符串
int index = 0;
if (arr.get(0) == null) {
return "没有找到相同的字符串";
} else {
for (int i = 0; i < arr.size(); i++) {
if (arr.get(index).length() < arr.get(i).length()) {
index = i;
}
}
}
//返回最大的字符串
return arr.get(index);
}
public static void main(String[] args) {
String str1 = "abcwerthelloyuiodef";
String str2 = "cvhellobnm";
String minstring = compare(str1, str2);
System.out.println(minstring);
}
}
import java.util.Arrays;
public class StringTest4 {
//选择排序
public static void SelectSort(char[] chars) {
for (int i = 0; i < chars.length; i++) {
for (int j = i + 1; j < chars.length - 1; j++) {
if (chars[i] > chars[j]) {
char c = chars[i];
chars[i] = chars[j];
chars[j] = c;
}
}
}
}
//冒泡排序
public static void BubbleSort(char[] chars) {
for (int i = 0; i < chars.length; i++) {
for (int j = 0; j < chars.length - 1; j++) {
if (chars[j] > chars[j + 1]) {
char c = chars[j];
chars[j] = chars[j + 1];
chars[j + 1] = c;
}
}
}
}
//Arrays.sort
public static void ArraysSort(char[] chars) {
Arrays.sort(chars);
}
public static void main(String[] args) {
String s = "abcwerthelloyuiodef";
char[] chars = s.toCharArray();
//选择排序
// SelectSort(chars);
// String s1 = String.valueOf(chars);
// System.out.println(chars);
//冒泡排序
BubbleSort(chars);
String s1 = String.valueOf(chars);
System.out.println(chars);
//利用Arrays.sort进行排序
// ArraysSort(chars);
// String s1 = String.valueOf(chars);
// System.out.println(s1);
}
}
正则表达式的预习内容
限定符(一个字符)
used? 代表前面的额字符可以出现 u us use used
ab*c 代表b可以出现0次,也可以出现很多次
ab+c 代表b必须出现一次以上
ab{2,6}c 代表b出现2到6次 若想要字符出现两次以上,则可以省略6 ab{2,}c
若想匹配多个字符,可以将其括号在一起 (ab)*c、(ab)+c
或运算符 |
a (cat|dog)
子父类
[abc]+ 匹配到的是abc、aabbcc、aaabbbccc
[a-z]代表所有小写的字符、
[A-Z]代表所有大写的字符
[0-9]代表所有0-9数字
[^0-9]+ 代表所有非数字的字符
元字符
d 代表数字字符
w 代表所有的单词字符(包括数字、换行符)
s 代表空白符 包含tab字符,以及换行符
D 代表非数字字符
W 代表非单词字符
S 代表非空白字符
.* 代表任意字符,但不包含换行符
特殊的字符
^会匹配行首
会
匹
配
行
尾
a
匹
配
行
首
的
a
a
会匹配行尾 ^a 匹配行首的a a
会匹配行尾a匹配行首的aa 匹配行尾的a
高级概念
<.+> 贪婪匹配,会匹配到更多的标签
<.+?>懒惰匹配,仅匹配<>内的字符串
b 代表单词字符的边界
replaceAll使用在替换字符串的时候
match()使用在判断字符串是否合法的时候
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)