Java 获取指定字符串出现的次数

Java中 获取指定字符串在另一个字符串中出现的次数

方式一

/**
 * @param args
 */
public static void main(String[] args) {

    String srcText = "Hello World";
    String findText = "e";
    int num = appearNumber(srcText, findText);
    System.out.println(num);
}

/**
 * 获取指定字符串出现的次数
 * 
 * @param srcText 源字符串
 * @param findText 要查找的字符串
 * @return
 */
public static int appearNumber(String srcText, String findText) {
    int count = 0;
    Pattern p = Pattern.compile(findText);
    Matcher m = p.matcher(srcText);
    while (m.find()) {
        count++;
    }
    return count;
}

方式二

/**
 * @param args
 */
public static void main(String[] args) {

    String srcText = "Hello World";
    String findText = "e";
    int num = appearNumber(srcText, findText);
    System.out.println(num);
}


/**
 * public int indexOf(int ch, int fromIndex)
 * 返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索
 * 
 * @param srcText
 * @param findText
 * @return
 */
public static int appearNumber(String srcText, String findText) {
    int count = 0;
    int index = 0;
    while ((index = srcText.indexOf(findText, index)) != -1) {
        index = index + findText.length();
        count++;
    }
    return count;
}

作者:itmyhome

### 回答1: 可以使用 String 类的 indexOf() 和 substring() 方法来获取一个字符在一个字符串出现次数。具体实现可以参考以下代码: public static int countChar(String str, char c) { int count = ; int index = str.indexOf(c); while (index != -1) { count++; index = str.indexOf(c, index + 1); } return count; } 其中,str 表示要查找的字符串,c 表示要查找的字符。该方法会返回字符 c 在字符串 str 中出现次数。 ### 回答2: 要获取一个字符在一个字符串出现次数,可以使用Java中的字符串方法和循环来实现。 首先,我们可以使用`charAt`方法逐个获取字符串中的字符,并通过与目标字符进行比较来判断是否匹配。然后,我们可以使用计数器变量来记录字符出现次数。在遍历完整个字符串后,就可以得到字符在字符串中的出现次数。 以下是一个示例代码: ```java public class CharCount { public static int countCharOccurrences(String string, char targetChar) { int count = 0; for (int i = 0; i < string.length(); i++) { if (string.charAt(i) == targetChar) { count++; } } return count; } public static void main(String[] args) { String str = "Hello, Java!"; char targetChar = 'a'; int occurrences = countCharOccurrences(str, targetChar); System.out.println("字符 '" + targetChar + "' 在字符串出现了 " + occurrences + " 次。"); } } ``` 以上示例代码中,`countCharOccurrences`方法接受一个字符串和一个目标字符作为参数,返回目标字符在字符串出现次数。在`main`方法中,我们定义了一个测试用的字符串`str`和目标字符`targetChar`,然后调用`countCharOccurrences`方法获取目标字符在字符串中的出现次数,并打印结果。 运行以上代码,输出结果为:字符 'a' 在字符串出现了 3 次。 ### 回答3: 要获取一个字符在一个字符串出现次数,可以使用Java中的字符串方法和循环来实现。以下是一种可行的方法: ```java public class CharacterCount { public static void main(String[] args) { String str = "Hello, world!"; char target = 'o'; int count = 0; // 遍历字符串中的每一个字符 for (int i = 0; i < str.length(); i++) { // 判断当前字符是否与目标字符相等 if (str.charAt(i) == target) { count++; // 如果相等,则计数器加1 } } System.out.println("字符 " + target + " 在字符串出现次数为:" + count); } } ``` 上述代码中,首先定义了一个字符串`str`和一个目标字符`target`。然后,通过使用`for`循环遍历字符串中的每个字符,使用`charAt()`方法获取字符串指定位置的字符,并将其与目标字符进行比较。如果相等,则将计数器`count`加1。最后,输出目标字符在字符串出现次数。 使用该方法可以获取任意字符在任意字符串出现次数
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值