0% found this document useful (0 votes)
11 views5 pages

Commonly Used Special Cases in JAVA

This document discusses common special cases when using strings in Java including case-insensitive comparison, trimming leading and trailing spaces, checking string length, checking for substrings, and parsing numeric values from strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Commonly Used Special Cases in JAVA

This document discusses common special cases when using strings in Java including case-insensitive comparison, trimming leading and trailing spaces, checking string length, checking for substrings, and parsing numeric values from strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Commonly used Special Cases in JAVA

1. Case-insensitive comparison:
String text1 = "hello";
String text2 = "HeLLo";

// Compare two strings ignoring case


if (text1.equalsIgnoreCase(text2)) {
System.out.println("The strings are equal, ignoring case.");
} else {
System.out.println("The strings are not equal, considering case.");
}
The `equalsIgnoreCase()` method compares two strings, ignoring the case. In this
example, it will print "The strings are equal, ignoring case." because both `text1` and
`text2` have the same characters, regardless of case.

Pearson Higher Nationals in Computing York Graduate Campus


2. Trimming leading and trailing spaces:
String input = " Hello, World! ";

// Trim leading and trailing spaces from the string


String trimmed = input.trim();

System.out.println("Original input: \"" + input + "\"");


System.out.println("Trimmed input: \"" + trimmed + "\"");

The `trim()` method removes any leading and trailing white spaces from a string. In this
example, it will print:

Original input: " Hello, World! "


Trimmed input: "Hello, World!"

Pearson Higher Nationals in Computing York Graduate Campus


3. String length check:
String input = "Hello";

// Check if the string has a length greater than 5


if (input.length() > 5) {
System.out.println("The string has more than 5 characters.");
} else {
System.out.println("The string has 5 or fewer characters.");
}

The `length()` method returns the number of characters in a string. In this example, it
will print "The string has 5 or fewer characters." because the length of `input` is 5.

Pearson Higher Nationals in Computing York Graduate Campus


4. Checking if a string contains a substring:
String text = "Hello, world!";

// Check if the string contains the substring "world"


if (text.contains("world")) {
System.out.println("The string contains the word \"world\".");
} else {
System.out.println("The string does not contain the word \"world\".");
}
The `contains()` method checks if a string contains a specified substring. In this example,
it will print "The string contains the word \"world\"." because `text` contains the word
"world".

Pearson Higher Nationals in Computing York Graduate Campus


5. Parsing numeric values:
String numberStr = "42";

try {
int number = Integer.parseInt(numberStr);
System.out.println("Parsed number: " + number);
} catch (NumberFormatException e) {
System.out.println("Error: Invalid number format.");
}

The `parseInt()` method parses a string into an integer. In this example, it will print
"Parsed number: 42" because `numberStr` contains the valid integer "42". If
`numberStr` were "abc" instead, it would catch a `NumberFormatException` and print
"Error: Invalid number format."

Pearson Higher Nationals in Computing York Graduate Campus

You might also like