如何用另一个字符串替换一个字符串的出现并计算更改的次数?

时间:2019-02-27 21:12:42

标签: java string replace

我已经坚持了好几天,我不知道如何用“ was”替换“ is”并计算所做的更改。我的代码在下面。

公共类BasicString {

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String s;
    int count = 0;
    int count1 = 0;
    while (true) {
        System.out.println("Enter text");
        s = scan.nextLine();
        String t = new String("is");
        String j = new String("was");
        if (s.equals("The end")) {
            break;
        }
        // IndexOf method used with variable count to see if "Is" is inserted 
        if (s.indexOf(t) != -1) {
            String r = t.replace("is", "was");

            count++;
            System.out.println(count + r);

        }

    }

任务是这个- 使用到目前为止所学的字符串方法,编写一个程序,该程序将允许用户在文本行中键入内容,直到用户自己在行中键入短语“ The end”为止。该程序将用字符串“ was”替换每次出现的“ is”,并计算更改的次数。字符串具有两种基本方法,您需要进行此分配:

0 个答案:

没有答案