字符串数组中的通配符替换

时间:2015-05-27 15:16:48

标签: java arrays string wildcard

我无法让wildString进入wildSum数组。这就是我需要运行这个程序。

示例运行1:

Enter the first String:
D*g
Enter the replacement String:
in
Output: Ding
Scanner input = new Scanner(System.in);

    System.out.println("Enter the first string:");
    String wildString = input.nextLine(); 
    char [] wildStringArray = wildString.toCharArray();

    System.out.println("Enter the replacement String:");
    String replaceString = input.nextLine(); 


    if(wildString.contains("*") != true) {
        System.out.println("Error: no *");
    }

    if (wildString.contains("\t") == true && wildString.contains(" ") == true && wildString.toLowerCase() != wildString.toUpperCase()) {
        System.out.println("Error: Incorrect characters");

    }
    char [] wildSum = new char[wildString.length() + replaceString.length() -1];
    char wildSum[] = new char[wildString.toCharArray()];

    for (int i = 0; i < wildSum.length; i ++ )
    {
    // insert wildString into wildSum
        if( wildSum[i] == '*' ) {   

            /* for (int i=0; i < wildSum.length(); i++) {
                  wildSum[i] = wildSum[i].replace('*', replaceString);
                }   */

1 个答案:

答案 0 :(得分:1)

尝试以下代码

Scanner input = new Scanner(System.in);

    System.out.println("Enter the first string:");
    String wildString = input.nextLine(); 


    System.out.println("Enter the replacement String:");
    String replaceString = input.nextLine(); 

    System.out.println(wildString.replaceAll("\\*",replaceString));