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