Results 1 to 8 of 8
  1. #1
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default hwo to compare two char type values?

    how can i compare two char type variables, ignoring the Case?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: hwo to compare two char type values?

    Perhaps Character.toLowerCase(c1) == Character.toLowerCase(c2)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    8

    Default Re: hwo to compare two char type values?

    You could just convert them to string and then use the equalsIgnoreCase(String x) method..
    Java Code:
    import java.util.*;
    
    public class Test{
    
    	public static void main(String args[]){
    		char a  = 'x';
    		char b  = 'X';
    		String temp = "" + a ;
    		if(temp.equalsIgnoreCase(b+"")){
    			System.out.println("Equals");
    		}
    	}
    }
    Edit : But i guess jim829 answer is better ;)

  4. #4
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: hwo to compare two char type values?

    Character means exactly? and do i have to import any class before using this function? and whats its return type?
    Last edited by rahil.khan; 02-08-2015 at 08:12 PM.

  5. #5
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: hwo to compare two char type values?

    Quote Originally Posted by ShadowWalker View Post
    You could just convert them to string and then use the equalsIgnoreCase(String x) method..
    Java Code:
    import java.util.*;
    
    public class Test{
    
    	public static void main(String args[]){
    		char a  = 'x';
    		char b  = 'X';
    		String temp = "" + a ;
    		if(temp.equalsIgnoreCase(b+"")){
    			System.out.println("Equals");
    		}
    	}
    }
    Edit : But i guess jim829 answer is better ;)
    i can't use string cause i have used lots of Char functions.

  6. #6
    rahil.khan is offline Member
    Join Date
    Oct 2014
    Location
    NEW DELHI
    Posts
    41
    Rep Power
    0

    Default Re: hwo to compare two char type values?

    thnx jim829, it worked..
    thanx ShadowWalker for your reply...

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: hwo to compare two char type values?

    Quote Originally Posted by ShadowWalker View Post
    You could just convert them to string and then use the equalsIgnoreCase(String x) method..
    Java Code:
    import java.util.*;
    
    public class Test{
    
    	public static void main(String args[]){
    		char a  = 'x';
    		char b  = 'X';
    		String temp = "" + a ;
    		if(temp.equalsIgnoreCase(b+"")){
    			System.out.println("Equals");
    		}
    	}
    }
    Edit : But i guess jim829 answer is better ;)
    You bet; your 'solution' creates a StringBuilder (line #8), moves an empty String literal to it, adjusts it (by appending a single character) and creates another String. The same holds to line #9, and it calls a method that calls another method (in the Character class) and finally the garbage collector has to get rid of two StringBuilders and two Strings; there is no such thing as a free lunch when it comes to Strings ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    183
    Rep Power
    8

    Default Re: hwo to compare two char type values?

    Quote Originally Posted by JosAH View Post
    You bet; your 'solution' creates a StringBuilder (line #8), moves an empty String literal to it, adjusts it (by appending a single character) and creates another String. The same holds to line #9, and it calls a method that calls another method (in the Character class) and finally the garbage collector has to get rid of two StringBuilders and two Strings; there is no such thing as a free lunch when it comes to Strings ...

    kind regards,

    Jos
    Good to know. As i see i have a lot of things to learn... :)

Similar Threads

  1. fetch and compare display tag values
    By Hepey in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-16-2012, 10:59 AM
  2. Compare substring to char
    By Muskar in forum New To Java
    Replies: 2
    Last Post: 11-21-2010, 08:24 PM
  3. How to compare char
    By scottpush in forum New To Java
    Replies: 2
    Last Post: 02-27-2010, 11:51 PM
  4. Compare a char
    By Torgero in forum New To Java
    Replies: 3
    Last Post: 02-01-2009, 03:24 PM
  5. Char type for first character typed?
    By Sageinquisitor in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 07:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •