Android(Java) 字符串的常用操作,獲取指定字符出現的次數,根據指定字符截取字符串


/*這是第100000份數據,要截取出100000*/
String s="這是第100000份數據";
 String s1 = s.substring(s.indexOf("第") + 1, s.indexOf("份"));
 
        

 




/*判斷指定字符出現了幾次*/
    public static int countStr(String str, char key) {
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == key)
                count++;
        }
        return count == 0 ? -1 : count;
    }
 
        
/*根據指定字符截取字符串存入到數組中*/
private static String[] stringToArray(String str, String key) {
        String[] temp = null;
        temp = str.split(key);
        return temp;
    }
 
        

MainText.java

public static void main(String[] args) {
		 String str = "abc;abck;abc";
//		 System.out.println(countStr(str, ';'));
	        String [] temp = null; 
	        temp=stringToArray(str, ";");
	        for (int i = 0; i < temp.length; i++) {
				System.out.println(temp[i]);
			}

	}

 

 
         
         
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM