How-to Replacing strings (case insensitive)?

Discussion in 'Plugin Development' started by gomeow, Dec 2, 2012.

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

    gomeow

    So I have this code:
    Code:java
    1. @Override
    2. public void onEnable() {
    3. saveDefaultConfig();
    4. final Set<String> messagekeys = this.getConfig().getConfigurationSection("Message").getKeys(false);
    5. final ArrayList<String> messages = new ArrayList<String>();
    6. final ArrayList<String> newmessages = new ArrayList<String>();
    7. for(String messageline : messagekeys) {
    8. messages.add(this.getConfig().getString("Message."+messageline));
    9. }
    10. for(String messages2 : messages) {
    11. if(messages2.toUpperCase().contains("..PLAYERS..")) {
    12. System.out.println("Contains ..PLAYERS..");
    13. Player[] online = getServer().getOnlinePlayers();
    14. ArrayList<String> names = new ArrayList<String>();
    15. for(Player player : online) {
    16. names.add(player.getDisplayName());
    17. }
    18. String players = new String();
    19. for(String str : names) {
    20. if(!(str == names.get(names.size() - 1))) {
    21. players = players + str + ", ";
    22. }
    23. else {
    24. players = players + str;
    25. }
    26. }
    27. if(players == null) players = "None";
    28. String newmessage = messages2.toLowerCase().replaceAll("..players..", players);
    29. newmessages.add(newmessage);
    30. }
    31. else newmessages.add(messages2);
    32. }
    33. Integer interval = this.getConfig().getInt("Interval");
    34. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    35.  
    36. @Override
    37. public void run() {
    38. for(String line : newmessages) {
    39. System.out.println(line);
    40. }
    41.  
    42.  
    43. }
    44.  
    45. }, 0L, interval * 20);
    46. }


    it is supposed to replace a list like:

    - Hello
    - Players Online - ..players..

    to:

    - Hello
    - Players Online - gomeow

    But it does nothing

    some other questions:
    How do I replace something in a string ignoring case?

    Actually, I realize now that first off all, the list check should be players.isEmpty() not == null.
    Secondly, I was using ||players|| in my config instead of ..players.. *facepalm*

    On another note, I still want to know how to replace something in a string ignoring case

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  2. Offline

    zack6849

    string.toLowerCase().replace("derp", "herp");
     
  3. Offline

    gomeow

    That will make the whole string lowercase. I want it to not care what case it is
     
  4. .equalsIgnoreCase() ?
     
  5. Offline

    gomeow

    I want to turn something like this
    CaSe to hello without testing every possibility of case. Basically I want a method like .replaceALLIgnoreCase(from, to)
     
  6. Wait whut? Then you just need to do what zack6849 said. It will make your variable string lowercase, not your actual message.
    You could still do this
    string.toLowerCase().replace("derp", "HerP");
    This will replace Derp deRp etc. with HerP

    Or am I missing something? ;p
     
  7. Offline

    gomeow

    Here is some code, tell if if this should work:
    Code:java
    1. String string = "StRiNg somethingelse";
    2. String newstring = string.toLowerCase().replaceALL("somethingelse", "somethingnew");
    3. //is new string now "StRiNg somethingnew" or "string somethingnew"?
     
  8. Offline

    chasechocolate

    Should be "string somethingnew"
     
  9. Offline

    gomeow

    My point, I want it to be the former.
     
  10. Offline

    QuantumDev

    I'd do this:

    Code:
    String startString = "RaNdOm TeXt REPlAcE Me PleASe!$!";
    String finalString = "";
    String replaceString = "replace";
    int index = startString.toLowerCase().indexOf(replaceString.toLowerCase());
    finalString = startString.substring(0, index);
    finalString += replaceString;
    finalString += startString.substring(index+replaceString.length());
    It's EXTREMELY dirty and I'm sure there's a better way, but that's what I thought of off the top of my head...
     
    gomeow likes this.
  11. Offline

    gomeow

    I will go with that if I can't find another method
     
  12. Offline

    boardinggamer

    Code:java
    1. String changing = "FirStWOrD GOInGtocHanGe";
    2. String tempwords = "";
    3. for (String words : changing.split(" ")){
    4. String word = "";
    5. if (words.equalsIgnoreCase("goingtochange")){
    6. word = "Changed";
    7. } else {
    8. word = words;
    9. }
    10. if (tempwords.equals("")){
    11. tempwords = word;
    12. } else {
    13. tempwords = tempwords + " " + word;
    14. }
    15. }
    16. changing = tempwords;
    17. //changing will now be "FirStWOrD Changed"
     
  13. Offline

    gomeow

    I found this somewhere:
    Code:
    String startString = "RaNdOm TeXt REPlAcE Me PleASe!$!";
    String endString = startString.replaceALL("(?i)replace", "this doesn't fit in here");
    Haven't tested it yet though

    EDIT: Works beautifully!
     
Thread Status:
Not open for further replies.

Share This Page