Solved Replacing 1 char with more than 1 word

Discussion in 'Plugin Development' started by kreashenz, Mar 29, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    kreashenz

    This is probably something easy to do but I have no idea how I would do it. I've tried putting the string into char arrays and checking for the '_' but when I try replacing it, it replaces both the '_' from in the string.
    And if you don't understand what I mean:
    I'm trying to change
    Code:
    "I can turn _ into _"
    into
    Code:
    "I can turn cats into dogs"
    My current code, which is replacing both the '_' in the string:
    Code:java
    1. String lore = black.getText();
    2. int where_is = 0;
    3. for(int j = 0; j < lore.toCharArray().length; j++) {
    4. if(lore.toCharArray()[j] == '_') {
    5. where_is = j;
    6. }
    7. }
    8.  
    9. lore = lore.replace(String.valueOf(lore.toCharArray()[where_is]), submittedCard.get(fromPlayer.getName()).getItemMeta().getDisplayName());
    10. ItemStack white = Utils.setName(Utils.setLore(submittedCard.get(fromPlayer.getName()), lore), "§b" + fromPlayer.getName());
    11.  
     
  2. Offline

    d3v1n302418

    Try the .replaceAll method. It converts one string to another.
     
  3. Offline

    kreashenz

    d3v1n302418 Yes but that would change all the underscores to the same replacement. I need it to have multiple replacements.
     
  4. Offline

    GotRice

    Try
    Code:
    String[] data = string.split("");
    String result = "";
    boolean blank1 = false;
    String result;
    for(int i = 0; i &lt; data.length; i++){
    if(data.equals("_")){
    if(blank1)
    result += p.getName();
    else
    result += "Test";
    }
    else{
    result +=s;
    }
    }
    }
    Please edit the code to fit your variables. This is not a very dynamic way to do it (meaning its not one size fits all), but I find this easiest to understand.
     
  5. Offline

    kreashenz

    GotRice
    Code: System.out.print(Utils.replace("I can turn _ into _", '_', "cats", "dogs"));
    Code:java
    1. public static String replace(String string, char letter, String... words) {
    2. String[] data = string.split("");
    3. String result = "";
    4. boolean blank1 = false;
    5. for(int i = 0; i < data.length; i++){
    6. if(data.equals(String.valueOf(letter))){
    7. if(blank1) {
    8. result += words[0];
    9. }
    10. else {
    11. result += words[1];
    12. }
    13. }
    14. else{
    15. result += string;
    16. }
    17. }
    18.  
    19. return result;
    20. }

    This prints out
    Code:
    [18:44:05 INFO]: I can turn _ into _I can turn _ into _I can turn _ into _I can turn _ into _I can turn _ into _I can turn _ into _I can turn _ into _I can turn  _ into _I can turn _ into _I can turn _ into _I can turn _ into _I can turn _ into _I can turn _ into _I can turn _ into _I can turn _ into _I can turn _ into _I can turn _ into _I can turn _ into _I can turn _ into _I can turn _ into _
     
  6. Offline

    GotRice

    kreashenz

    Code:java
    1. public static String replace(String string, String letter, String word1, String word2) {
    2. String[] data = string.split("");
    3. String result = "";
    4. boolean blank1 = true;
    5. for (int i = 0; i < data.length; i++) {
    6. String s = data[i];
    7. if (s.equals(letter)) {
    8. if (blank1) {
    9. result += word1;
    10. blank1 = false;
    11. } else {
    12. result += word2;
    13. }
    14. } else {
    15. result += s;
    16. }
    17. }
    18. return result;
    19. }[/i]


    Rewritten, sorry I realized how bad that code was (wrote it in forum). This is tested, Good Luck!
     
Thread Status:
Not open for further replies.

Share This Page