String.replace结果被忽略吗? [英] String.replace result is ignored?

查看:320
本文介绍了String.replace结果被忽略吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用IntelliJ,并且替换流行语突出显示.大多数技巧都困扰着我,所以我忽略了它们,但是我得到的这一点是,string.replace的结果被忽略了.为什么?

So i'm using IntelliJ and the replace buzzword is highlighted. Most of the tips are over my head so i ignore them, but what i got for this one was that the result of string.replace is ignored. Why?

我是否需要类似( string = string.replace(string.charAt(i)); )的东西?

would i need something like ( string = string.replace(string.charAt(i));)?

import java.util.Scanner;

public class PhoneNumberDecipher {
    public static String phoneNumber;
    public static String Decipher(String string) {
        string = phoneNumber;
        for(int i =0; i<=phoneNumber.length(); i++) {
            if(string.equalsIgnoreCase("A")
               ||string.equalsIgnoreCase("B")
               ||string.equalsIgnoreCase("C")) {
                string.replace(string.charAt(i),'2')
            }
            else if(string.equalsIgnoreCase("D")
                    ||string.equalsIgnoreCase("E")
                    ||string.equalsIgnoreCase("F")) {
                string.replace(string.charAt(i),'3');
            }
            else if(string.equalsIgnoreCase("G")
                    ||string.equalsIgnoreCase("H")
                    ||string.equalsIgnoreCase("I")) {
                string.replace(string.charAt(i),'4');
            }
            else if(string.equalsIgnoreCase("J")
                    ||string.equalsIgnoreCase("K")
                    ||string.equalsIgnoreCase("L")) {
                string.replace(string.charAt(i),'5');
            }
            else if(string.equalsIgnoreCase("M")
                    ||string.equalsIgnoreCase("N")
                    ||string.equalsIgnoreCase("O")) {
                string.replace(string.charAt(i),'6');
            }
            else if(string.equalsIgnoreCase("P")
                    ||string.equalsIgnoreCase("Q")
                    ||string.equalsIgnoreCase("R")
                    || string.equalsIgnoreCase("S")) {
                string.replace(string.charAt(i),'7');
            }
            else if(string.equalsIgnoreCase("T")
                    ||string.equalsIgnoreCase("U")
                    ||string.equalsIgnoreCase("V")) {
                string.replace(string.charAt(i),'8');
            }
            else if(string.equalsIgnoreCase("W")
                    ||string.equalsIgnoreCase("X")
                    ||string.equalsIgnoreCase("Y")
                    ||string.equalsIgnoreCase("Z")) {
                string.replace(string.charAt(i),'9');
            }
        }

        return string;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("Please Enter a Phone Number you Wish to Decipher...");
        phoneNumber = input.nextLine();

        System.out.print(Decipher(phoneNumber));
    }
}

推荐答案

String 对象是来自文档:

公共字符串replace(char oldChar,char newChar)

返回一个字符串,该字符串是用newChar替换此字符串中所有出现的oldChar的结果.

Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.

希望这会有所帮助.

这篇关于String.replace结果被忽略吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆