您的位置:首页 > 职场人生

java基础面试题之String字符串出现次数

2018-01-29 22:05 435 查看
package com.tarena.fly;
/**
* \* Created with IntelliJ IDEA.
* \* User: 武健
* \* Date: 2018/1/29
* \* Time: 20:45
* \* To change this template use File | Settings | File Templates.
* \* Description:String 字符串 指定字符出现次数
* \
*/
public class Main {
public static void main(String[] args) {
String str = "HelloWorld";
String key = "o";
int count = 0;
while (str.indexOf(key) > 0) {
count = count + 1;
str = str.substring(str.indexOf(key) + 1, str.length());
}
System.out.println(count);
}
}
package com.tarena.fly;
/**
* \* Created with IntelliJ IDEA.
* \* User: 武健
* \* Date: 2018/1/29
* \* Time: 20:45
* \* To change this template use File | Settings | File Templates.
* \* Description:String 字符串 指定字符出现次数
* \
*/
public class Main {
public static void main(String[] args) {
String str = "HelloWorld";
String key = "o";
int count = 0;
while (str.indexOf(key) > 0) {
count = count + 1;
str = str.replaceFirst(key,"");
}
System.out.println(count);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: