本文實例講述了Java統計字符串中字符出現次數的方法。分享給大家供大家參考,具體如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.wenzhi; import java.util.Scanner; public class Test01 { public static void main(String args[]) { Scanner scan = new Scanner(System.in); System.out.println( "請你輸入字符" ); String str = scan.nextLine(); Scanner scan1 = new Scanner(System.in); System.out.println( "請輸入你要查找的子字符串" ); String str1 = scan1.nextLine(); int count = 0 ; int start = 0 ; while (str.indexOf(str1, start) >= 0 && start < str.length()) { count++; start = str.indexOf(str1, start) + str1.length(); } System.out.println(str1 + "在" + str + "出現的次數為" + count); } } |
運行結果:
希望本文所述對大家java程序設計有所幫助。
原文鏈接:http://blog.csdn.net/wenzhilanyu2012/article/details/12921913