ignoring case of args[1]

Discussion in 'Plugin Development' started by the_merciless, Mar 17, 2013.

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

    the_merciless

    I need to check if a string list contains args[1] from a command, however i am unable to use ignoreCase as it isnt an option. Is there an easier way to do this without looping through the entire list. Heres the code im using:

    Code:
    List<String> classes = plugin.getConfig().getStringList("Classes");
    if (classes.contains(args[1])) {
     
  2. Offline

    Tirelessly

  3. Offline

    the_merciless

    All that told me was to create a seperate class which loops through the string list. which is what i asked to avoid. nvm i think i have a simpler solution.
     
  4. Offline

    Tirelessly

    You wanted an easier way, that was an easier way.
     
  5. Offline

    the_merciless

    I just made a for loop within the command creating a list of lower case strings. then compared args[1].toLowerCase, no reason to create a class for it.

    Code:
    List<String> classes = plugin.getConfig().getStringList("Classes");
    List<String> classestolower = new ArrayList <String>();
    for (int i = 0; i < classes.size(); i++){
       String classtolower = classes.get(i).toLowerCase();
        classestolower.add(classtolower);           
    }
    if (classestolower.contains(args[1].toLowerCase())) {
     
  6. Offline

    Tirelessly

    That's fine, unless you want to use it more than once..
     
  7. Offline

    the_merciless

    Well twice im using it but i can live with that. Thanks for your help anyway :)
     
  8. Offline

    SuperEvan200

    the_merciless

    Here's a much easier way (An example from the plugin I'm making):
    Code:
    if(args[0].equalsIgnoreCase("join"))
    Here's what I think you should do:
    Code:
    if (classes.contains(args[1].equalsIgnoreCase)) {
    Tell me if that works. :)

    [EDIT]: Tested the code I gave you in eclipse, came out with errors. I'm guessing wherever you have/define the class try adding a .equalsIgnoreCase before or after it.

    Sorry if this is wrong, I started coding java plugins about a week ago.
     
  9. Offline

    the_merciless


    Thanks for the input, but .equalsIgnoreCase() is checking if it is equal to something else and your not stating something to compare it to. a nice addition to java would be something.ignoreCase()
     
  10. the_merciless
    And what exacly would that new .ignoreCase() method do ?
     
  11. the_merciless
    You can do it a different way as well. If you don't care about what case is used in the list, simple add all strings in upper case. Then you can convert args[1] to upper case as well, and you can use contains again. But you need to know that contains for a list is implemented as a for loop internally in most cases. If you want to have faster lookup use a Set like the HashSet for example. But keep in mind that a HashSet can only contain each value ones (shouldn't be a problem in your case) and that you can't order items in a HashSet, as you can do it in a List
     
  12. Offline

    the_merciless

    Err it would ignore case. Although what I probably meant to say was containsIgnoreCase()

    That's close to how I done it. Although I used lowercase.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  13. contains() is compatible with any Object, not just Strings, so adding a containsIgnoreCase() would be totally useless to the rest of 99.9% of the classes that you can use with Lists :p

    So there's no really easy way, adding/converting them to lower case is the way to go :p
     
  14. Offline

    the_merciless

    Not necessarily. .containsIgnoreCase(String) would always require a string and when not using strings, you would just continue to use .contains()

    Anyway I'm not sure exactly how it would be done but I'm sure there's a possible solution.
     
  15. the_merciless
    You're not getting it:
    Code:
    List<String> list = ... 
    list.containsIgnoreCase(...); // seems legit so far...
    
    List<ItemStack> list = ...
    list.containsIgnoreCase(...); // this method is totally and utterly useless for this list
    // and it's useless to the rest of the classes that you can use inside List<HERE>, which are *alot*, so I seriously doubt Java to be adding that.
     
  16. Normalizing the strings to be either fully upper or fully lower case is the best way I know, based on the information that was given so far. As List is generic, it doesn't provide a method, that is related to a specific type. You can implement List<String> in your own class, and add containsIgnoreCase your self to that class. Anyway the implementation will be loop based as before, so there is no benefit in doing this, other than the fact that you don't have to normalize the strings.
     
  17. Offline

    the_merciless

    Digi

    I thought I just explained that.

    List<String> list , would use list.containsIgnoreCase()

    And,

    List<ItemStack> list , would continue to use list.contains()

    And like I said I don't know exactly how to do it. These are just ideas/examples of what I mean. I'm far from an expert on java. All I'm saying is maybe someone could come up with something along the lines of this. I'm not saying my idea will work/ is the solution I'm just explaining what I would like.
     
  18. the_merciless
    I belive you can't add an extra method only for List with String type.
    I only explained to you why Java didn't/wouldn't add that method.
    That's it :p

    So you can make your own separate method, you can extend List and add a method, you can make sure they're the same case when entered and compare ... but you do need to make it yourself.
     
Thread Status:
Not open for further replies.

Share This Page