题解 | #统计字符串中字母出现次数# str.replac

统计字符串中字母出现次数

https://www.nowcoder.com/practice/83350872bdb5406fa706895d5efb1c55

import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        String string = "H e l l o ! n o w c o d e r";
        Scanner scanner= new Scanner(System.in);
        String word = scanner.next();
        scanner.close();
        System.out.println(check(string, word));
    }
    public static int check(String str,String word){
        return str.length() - str.replace(word,"").length();
    }
}

// import java.util.Scanner;

// public class Main {
//     public static void main(String[] args) {
//         String string = "H e l l o ! n o w c o d e r";
//         Scanner scanner= new Scanner(System.in);
//         String word = scanner.next();
//         scanner.close();
//         System.out.println(check(string, word));
//     }

//     public static int check(String str, String word) {

//         //write your code here......
//         int n =0;
//         int i = 0;

//         while(i < str.length())
//         {
//             if(str.indexOf(word, i) != -1)
//             {
//                 n++;
//                 i = str.indexOf(word,i)+1;
//             }
//             else
//             {
//                 break;
//             }
//         } 

//         return n;
        

//     }
// }

str.replace()你学会了么

替换之后求长度变化

Java语法基础 文章被收录于专栏

基础语法的熟悉

全部评论

相关推荐

但听说转正率很低,我现在有在实习了,好纠结要不要去
熬夜脱发码农:转正率低归低,但是实习的经历你可以拿着,又不是说秋招不准备了
点赞 评论 收藏
分享
后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
无实习如何秋招上岸
点赞 评论 收藏
分享
程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
看到这个内容真是闹麻了。。。。。。现在有了AI以后很多人面试都会作弊吗?&nbsp;那对老老实实面试的人岂不是不公平....
程序员牛肉:公平那是对小孩子讲的童话故事,成年人的世界只有能不能接受失败的后果。 你要是能接受面试作弊被发现之后多家公司联合永久拉黑的后果,你就搞。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务