Thursday, November 21, 2024

How to enable/disable form elements using jQuery? Examples

In last couple of tutorials you have learned how to get all selected checkbox and radio buttons, and in this tutorial, you will learn how to enable/disable form elements using jQuery. A form element e.g. a textfield, radio buttons and checkbox, all represented using input tag can be enabled or disabled by adding an attribute called "disabled". It's similar to "checked" attribute we have seen for checkbox and radio buttons. If this attribute is not present then element is enable, but if this attribute is present then you can use disabled=true to disable the form element and disabled=false to enable the form elements as shown in our example. 

Thursday, November 14, 2024

How to Compare Two Strings Without Case Sensitivity in Java? Example

How to Compare Two Strings Without Case Sensitivity in Java

Java provides several ways to compare strings without taking case sensitivity into account. In this article, we'll explore different methods to perform case-insensitive string comparisons in Java.

Method Use Case Efficiency
equalsIgnoreCase() Checking equality High
compareToIgnoreCase() Ordering comparison High
String.CASE_INSENSITIVE_ORDER Sorting collections High
equals() + toUpperCase() Not recommended Low

1. Using equalsIgnoreCase()

The equalsIgnoreCase() method

This method is ideal for checking if two strings are equal without considering their case. It returns true if both strings contain the same characters, regardless of their case.

String str1 = "Hello";
String str2 = "hello";
boolean isEqual = str1.equalsIgnoreCase(str2); // Returns true

2. Using compareToIgnoreCase()

The compareToIgnoreCase() method

Use this method when you want to check which string is greater or smaller, or to find out their relative ordering in lexicographical or alphabetical order, without considering case.

String str1 = "Apple";
String str2 = "banana";
int result = str1.compareToIgnoreCase(str2); // Returns a negative value

3. Using String.CASE_INSENSITIVE_ORDER

The String.CASE_INSENSITIVE_ORDER comparator

This comparator is useful for sorting a list of strings in case-insensitive order.

List<String> fruits = Arrays.asList("Apple", "banana", "Cherry");
Collections.sort(fruits, String.CASE_INSENSITIVE_ORDER);
// Result: [Apple, banana, Cherry]

4. Using equals() + toUpperCase()

Combining equals() and toUpperCase()

This is a tricky way to compare two strings in Java in a case-insensitive manner. First, convert both strings to uppercase, then compare them using equals().

String str1 = "Hello";
String str2 = "hello";
boolean isEqual = str1.toUpperCase().equals(str2.toUpperCase()); // Returns true

Note: This method is not recommended as it creates new String objects, which can be inefficient for large-scale comparisons.

Conclusion

When comparing strings without case sensitivity in Java, it's generally best to use the built-in methods like equalsIgnoreCase() for equality checks and compareToIgnoreCase() for ordering comparisons. 

These methods are optimized and provide clear, readable code. For sorting collections of strings, the String.CASE_INSENSITIVE_ORDER comparator is an excellent choice.

Remember, all these methods compare strings in lexicographical order. Choose the method that best fits your specific use case to ensure efficient and accurate string comparisons in your Java applications.

Thursday, November 7, 2024

How to convert XML to JSON in Java? Example

Since XML and JSON are two popular data interchange formats, you will often need to convert one into other. For example, many SOAP web services returns XML response and if you are using Java and wants to convert that XML response to JSON, then you can use JSON library from https://json.org. It provides a class called XML.java, which provides static methods to convert an XML text into a JSONObject. It also support JSON to XML conversion, which will learn in second part of the article. In order to use this class, you can dowload JAR files from Maven Central repository, or you can add required dependency in your pom.xml file.