简体   繁体   中英

Making part of string equal to other part of string

// Convert a string to Robber Speak
  public static String toRobber(String text) {
    String vowel = "";
    String robber = "";

    for (int i = 0; i <= text.length(); i++) {
      {
        if (!isVowel(text.charAt(i))) {
          vowel = text.charAt(i) + "o" + text.charAt(i);

          robber = vowel + text.substring(i+1);
          i++;
        }

        else {
            robber.substring(i,i) = text.substring(i,i);
        }
      }
    }
    return robber;
  }

In the else statement (line 10) java demands a variable. How do i set part of a string equal to another part of a string?

A method call such as robber.substring(i,i) cannot be on the left hand side of an assignment operator.

Besides, String s are immutable, so you'll have to create a new String .

For example:

char[] chars = robber.toCharArray();
chars[i] = text.charAt(i);
robber = new String (chars);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM