2017-11-25 286 views
-3

我有一个赋值,用大写“JAVA”替换所有出现的单词“java”,而不使用正则表达式或替换方法。请参阅下面的代码&输出。请确认这是否是最好的方法。使用大写“JAVA”替换所有出现的字母“JAVA”而不使用正则表达式或替换方法

代码:


import java.util.Scanner; 
public class Substitute 
{ 
    public static void main(String[] args) 
    { 
     String javaString = "java"; 
     String left, right, sub = "", result = ""; 
     int index; 
     Scanner input = new Scanner(System.in); 
     System.out.print("Enter a sentence with a word \"java\" and replaces every occurrence of it with \"JAVA\" or type \"quit\" to quit: "); 
     String sentence = input.nextLine(); 
     while(!(sentence.equalsIgnoreCase("quit"))) 
     { 
      index = sentence.toLowerCase().indexOf("java"); 
      if(index != -1) 
      { 
       if(javaString.equalsIgnoreCase(sentence.substring(index, index + 4))) 
       { 
        while(index != -1) 
        { 
         left = sentence.substring(index, index + 4); 
         right = sentence.substring(index + 4); 

         if(sentence.indexOf(right) <= 0 || sentence.indexOf(right) >= sentence.length()) 
         { 
          sub = sentence.substring(index, index + 4); 
          sub = sub.toUpperCase(); 
          if(result == "") 
          { 
           result = sentence.substring(0, index) + sub; 
          } 
          else 
          { 
           result = result.substring(0, index) + sub; 
          } 
          index = -1; 
         } 
         else if(sentence.indexOf(right) > 0 && sentence.indexOf(right) < sentence.length()) 
         { 
          sub = sentence.substring(sentence.indexOf(left, index), sentence.indexOf(right, index)); 
          sub = sub.toUpperCase(); 
          if(result == "") 
          { 
           result = sentence.substring(0, (sentence.indexOf(left, index))) + sub + sentence.substring(sentence.indexOf(right, index)); 
          } 
          else 
          { 
           result = result.substring(0, (sentence.indexOf(left, index))) + sub + sentence.substring(sentence.indexOf(right, index)); 
          } 
          index = sentence.toLowerCase().indexOf("java", index + 4); 
         } 
         if(index == -1) 
         { 
          System.out.println(result); 
         } 
        } //end of while loop 
        System.out.print("Please enter a sentence with a word \"java\" and replaces every occurrence of it with \"JAVA\" or type \"quit\" to quit: "); 
        sentence = input.nextLine(); 
        index = sentence.toLowerCase().indexOf("java"); 
        result = ""; 
       } 
      } 
      else 
      { 
       System.out.println("There is no word \"java\" in the sentence."); 
       System.out.print("Please enter a sentence with a word \"java\" and replaces every occurrence of it with \"JAVA\" or type \"quit\" to quit: "); 
       sentence = input.nextLine(); 
       index = sentence.toLowerCase().indexOf("java"); 
       result =""; 
      } 
     } 
    } 
} 

输出:


$ java Substitute 
Enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: Program 
There is no word "java" in the sentence. 

Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: rogram java 
rogram JAVA 

Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: s java and java 
s JAVA and JAVA 

Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: ScriptJava 
ScriptJAVA 

Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: p java 
p JAVA 

Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: scjava 
scJAVA 

Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: Javascript 
JAVAscript 

Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: Programming java and java JaVa 
Programming JAVA and JAVA JAVA 

Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: rogramming java and java JaVa 
rogramming JAVA and JAVA JAVA 

Please enter a sentence with a word "java" and replaces every occurrence of it with "JAVA" or type "quit" to quit: quit 
+0

绝对不是一个好办法。 1)你比较字符串不正确,不是一个好习惯。 2)使用一个StringBuilder。 –

+4

这不是一个“检查我的代码”网站。如果您的代码无法正常工作,请隔离错误并提出相关问题。如果它正在工作,那么我们无法在这里帮你,你可能想考虑在代码审查stackexchange网站上发布一个问题。但是,如果您选择这样做,您仍然希望改进您​​的问题,包括并特别针对您希望代码改进的具体指标。 –

+0

使用indexof()查找“java”,一旦找到了使用.toUpperCase()。这段代码看起来FAR太长了。 – IMustBeSomeone

回答

0

只需使用的indexOf找到字的 “java” 的索引,然后CONCAT左子+置换​​+右子。

private String replace(String s, String what, String with){ 
    int index; 
    while ((index = s.indexOf(what)) != -1){ 
     s = s.substring(0,index)+with+s.substring(index+what.length()); 
    } 
    return s; 
} 

@Test 
public void test(){ 
    TestCase.assertEquals(
      "Hello from JAVA, JAVA...JAVA", 
      replace("Hello from java, java...java", "java", "JAVA") 
    ); 
} 
相关问题