重拾JAVA基础10——String常用方法

package day12;

public class Demo1 {
//计算重复出现的子串,利用了split 出现一个子串(substr)会把主串(sub)分两段
	public static int MyCount(String str ,String substr){
		int sum = 0;
		
		int firstIndex = str.indexOf(substr);
		
		int lastIndex = str.lastIndexOf(substr);
		
		String[] seg = str.split(substr);
		
		sum = seg.length -1 ;
		
		return sum;
		
	}
//翻转字符串
	public static String MyReverse(String str) {

		char[] newStr = str.toCharArray();

		int len = newStr.length;

		char[] deChar = new char[len];

		int index = 0;

		while (len > 0) {
			deChar[index++] = newStr[--len];
		}

		return new String (deChar);
	}
	
	
	
//清除前后空格
	public static String MyTrim(String str) {
		char[] arr = str.toCharArray();
		int start = 0;
		int end = arr.length - 1;
		while (arr[start] == ' ') {
			start++;
		}
		while (arr[end] == ' ') {
			end--;
		}
		str = str.substring(start, end + 1);
		return str;
	}

	public static void main(String[] args) {

		String aa = "Gt0tpppitbint";
		
		int count = MyCount(aa,"i");
		//
		System.out.println( count);

	}

	public static String MyName(String path) {
		int index = path.lastIndexOf("\\");
		String name = path.substring(index + 1);
		return name;

	}

}

上一篇:9-2Java常用类----(2)String算法练习2


下一篇:字符串截取算法