import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author 许泽鑫
* @data 2019/10/9 - 16:36
*/
public class StringDemo {
public static void main(String[] args) {
String str = " ab ed ";
System.out.println(myTrim(str));
String str2 = "abcdefg";
String str3 = reverseString(str2,2,5);
System.out.println(str3);
String str4 = reverseString1(str2,2,5);
System.out.println(str4);
String str5 = "ababab";
String str6 = "ab";
System.out.println(getNum(str5,str6));
System.out.println(getNum1(str5,str6));
String str7 = "abcwerthelloyuiodef";
String str8 = "abcwecvhellobnm";
System.out.println(getMaxSubString(str7,str8));
System.out.println(reverseString2("I am a student !"));
}
//1.模拟一个trim方法,去除字符串两端的空格
public static String myTrim(String str){
int start = 0;
int end = str.length() - 1;
while(start < end &&str.charAt(start) == ' '){
start++;
}
while(start < end && str.charAt(end) == ' '){
end--;
}
return str.substring(start,end + 1);
}
//2.将一个字符串进行反转。将字符串中指定部分进行反转。比如"abcdefg"反转为"abfedcg"
public static String reverseString(String str , int start , int end){
char[] c = str.toCharArray();//字符串-->字符数组
return reverseArray(c,start,end);
}
public static String reverseArray(char[] c,int start,int end){
for(int i = start,j = end;i < j;i++,j--){
char temp = c[i];
c[i] = c[j];
c[j] = temp;
}
return new String(c);
}
//法二:
public static String reverseString1(String str,int start,int end){
String str1 = str.substring(0,start);
for (int i = end; i >= start ; i--) {
char c = str.charAt(i);
str1 += c;
}
str1 += str.substring(end + 1);
return str1;
}
//3.获取一个字符串在另一个字符串中出现的次数
// 比如获取"ab"在“abkkcadkavkevfkabkskab”中出现的次数
public static int getNum(String str1,String str2){
int count = 0;
for (int i = 0; i < str1.length()-1 ; i++) {
String str4 = str1.substring(i,i+2);
if(str4.equals(str2)){
count++;
}
}
return count;
}
//法二:
public static int getNum1(String str1,String str2){
int count = 0;
while((str1.indexOf(str2)) != -1){
count++;
str1 = str1.substring(str1.indexOf(str2) + str2.length());
}
return count;
}
//获取两个字符串中的最大相同字串比如:
//str1 = "abcwerthelloyuiodef"; str2 = "cvhellobnm"
public static List<String> getMaxSubString(String str1, String str2) {
String maxStr = (str1.length() > str2.length())? str1 : str2;
String minStr = (str1.length() < str2.length())? str1 : str2;
int len = minStr.length();
List<String> list = new ArrayList<>();
for(int i = 0;i < minStr.length();i++){
for(int x = 0,y = len -i;y <= len;x++,y++){
String str = minStr.substring(x,y);
if(maxStr.contains(str)){
list.add(str);
};
}
if(list.size() != 0){
return list;
}
}
return null;
}
//5.对字符串中字符进行自然顺序的排序
public static String sort(String str){
char[] c = str.toCharArray();
Arrays.sort(c);
return new String(c);
}
//练习 I am a student! 写一个方法:实现输出!student a am I
public static StringBuffer reverseString2(String str) {
String[] str1 = reverse(str.split("\\s+"));
StringBuffer s4 = new StringBuffer();
for (String string : str1) {
s4.append(string);
s4.append(" ");
}
return s4;
}
public static String[] reverse(String[] arr){
for(int i = 0;i < arr.length / 2;i++){
String temp = arr[arr.length -i - 1];
arr[arr.length -i - 1] = arr[i];
arr[i] = temp;
}
return arr;
}
}
涉及String类笔试的练习
最新推荐文章于 2022-01-17 22:55:43 发布