String类方法调用十题

编写程序将 “startWITH” 全部变为大写,并输出到屏幕;

public class M1 {

	public static void main(String[] args) {
			String s = "startWITH";
			System.out.println(s.toLowerCase());//大写转小写
			System.out.println(s.toUpperCase());//小写转大写
	}

}

编写程序将String类型字符串"abcdefg" 变为"gfedcba"。

public class M2 {

	public static void main(String[] args) {
			
		String s = "abcdefg";
		
		System.out.println(new StringBuffer(s).reverse().toString());
	}

}

写一个方法判断一个字符串是否对称。

import java.util.Scanner;

public class M3 {
	
	public boolean dc(String str) {
		boolean flag = false;
		if (str != null && str.length() > 0) {
			if(str.contains(new StringBuffer(str).reverse())) {
				System.out.println("相同");
				flag = true;
				return flag;
			}
			
		}
		return flag;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一个字符串");
		String str = sc.next();
		M5 t = new M5();
		boolean flag = t.dc(str);
		System.out.println(flag);
	}
}

String s = “113@ ere qqq yyui”; 请输出所有子串

113

ere

qqq

yyui

public class M4 {
	
	public static void main(String[] args) {
		String s = "113@ ere qqq yyui"; 
		String[] arr = s.split("@?\\s+");
		for (String x : arr) {
			System.out.println(x);
		}
	}
		
}

编写一个程序,将下面的一段文本中的各个单词的字母顺序翻转,“To be or not to be”,将变成"oT eb ro ton ot eb"。

public class M5 {

		public static void main(String[] args) {
			String s = "To be or not to be";
			
			String[] arr = s.split(" ") ;
			
			for (String x : arr) {
				System.out.print(new StringBuffer(x).reverse()+" ");
			}
	}

}

String s=”name=zhangsan age=18 classNo=090728”; 将上面的字符串拆分,结果如下:

zhangsan 18 090728

public class M6 {
	public static void main(String[] args) {
		String s = "name=zhangsan age=18 classNo=090728";
		
		String[] arr = s.split(" ");
		for (String x : arr) {
			String[] temp = x.split("\\s?[a-zA-Z]+=");
			System.out.println(temp[1]);
		}
		
	}
}

给定一个字符串数组。按照字典顺序进行从小到大的排序。

//String s = {"nba","abc","cba","zz","qq","haha"} 


public class M7 {
	public static void main(String[] args) {
		String[] arr = { "nba", "abc", "cba", "zz", "qq", "haha" };
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr.length-1; j++) {
				if("arr[j]".compareTo("arr[j+1")>0) {
					String temp = arr[j+1];
					arr[j+1] = arr[j];
					arr[j] = temp;
				}
				
			}
			
		}
		System.out.println(Arrays.toString(arr));
		
	}
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dNchYA7s-1594901815759)(C:\Users\lx\AppData\Local\Temp\1594870869860.png)]

一个子串在整串中出现的次数。

//String s = "nbaernbatynbauinbaopnba"


public class M7 {
	public static void main(String[] args) {
		
	
		String s = "nbaernbatynbauinbaopnba";
		int count = 0;
		while(s.contains("nb")) {
			int index= s.indexOf("nb");
			s = s.substring(index+2,s.length());
			count++;
			System.out.println(s);
		}
		System.out.println(count);
		
	}
}

.模拟一个trim功能一致的方法。去除字符串两端的空白

public class M10 {
	public static String m10(String s)  {
		
		int start = 0;
		int end = 0 ;
		
		for (int i = 0; i <s.length(); i++) {
			if(s.charAt(i)!=' ') {
				start=i;
				break;
			}
		}
		for (int i =s.length()-1; i>=0; i--) {
			if(s.charAt(i)!=' ') {
				end = i;
				break;
			}
		}
		
		System.out.println(start+","+end);
		if(start==end) {
			return "";
		}
		return s.substring(start, end+1);
	}
	
	public static void main(String[] args) {
		String s = "   i  love   you     ";
		String s1 = m10(s);
		System.out.println(s1);
	}

}

两个字符串中最大相同的子串。

import java.util.ArrayList;

public class M9 {
	
	
	public static ArrayList<String> fun(String a,String b) {
		ArrayList<String> list = new ArrayList();
		//长字符串
		String s1 = a.length()>b.length()?a:b;
		//短字符串
		String s2 = a.length()<=b.length()?a:b;
		for (int i = 0; i < s2.length(); i++)
	    {
	      System.out.println("当前截取" + i + "个字符");
	      int start = 0;
	      int end = s2.length() - i;
	      while (end != s2.length() + 1)
	      {
	        System.out.println(start + "," + end);
	        String sub = s2.substring(start, end);
	        System.out.println(sub);
	        if (s1.contains(sub)) {
	          list.add(sub);
	        }
	        start++;
	        end++;
	      }
	    }
	    return list;
	  }
	public static void main(String[] args) {
		String s1 = "asdfvxjnmkjk";
		String s2 = "fvjadaskjk";
		ArrayList<String> list = fun(s1,s2);
		System.out.println(list);
	}

}

你可能感兴趣的:(类,java,字符串)