StringUtils.java
package com.sridhar.string;
public class StringUtils{
//equals() - Determines the equality of two strings, ignoring case
public static boolean isEqual(String name1, String name2) {
return name1.equals(name2);
}
//equalsIgnoreCase() - Determines the equality of two strings, ignoring case
public static boolean isEqualsIgnoreCase(String name1, String name2) {
return name1.equalsIgnoreCase(name2);
}
/*toUpperCase() - Returns a string, with lowercase characters converted to
uppercase*/
public static String convertToUpperCase( final String name) {
return name.toUpperCase();
}
/*toLowerCase() - Returns a string, with uppercase characters converted to
lowercase*/
public static String convertToLowerCase( final String name) {
return name.toLowerCase();
}
//trim() - Removes whitespace from both ends of a string
public static String trimSpaces( final String name) {
return name.trim();
}
//length() - Returns the number of characters in a string
public static int calculateLength( final String name) {
return name.length();
}
//isEmpty() - it returns true, if the string object is empty otherwise returns false
public static boolean isEmpty( final String name) {
return name.isEmpty();
}
//charAt() - Returns the character located at the specified index
public static char getCharAt(final String name, int indexPos)
{
return name.charAt(indexPos);
}
/*concat() - returns a string with the value of
the String passed in to the method appended to the end of the String*/
public static String concat(String string, String string2) {
return string.concat(string2);
}
//replace() - Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
public static String replaceString(String something, String oldStr, String newStr) {
return something.replace(oldStr,newStr);
}
}
TestStringUtils.java
package com.sridhar.string;
public class TestStringUtils {
public static void main(String[] args) {
String content = "Chennai";
//charAt()
char character=StringUtils.getCharAt(content,4);
System.out.println("Character in the position of 4 is: "+character);
//concat()
String concatenatedString = StringUtils.concat("call", " taxi");
System.out.println("Concatenated String is: "+concatenatedString);
//equals()
boolean equalsResult = StringUtils.isEqual("java", "JAVA");
System.out.println("Java is equal to JAVA: "+ equalsResult ); // is "false"
//equalsIgnoreCase()
boolean ignoreCaseresult = StringUtils.isEqualsIgnoreCase("java", "JAVA");
System.out.println("Java is equal to JAVA: "+ ignoreCaseresult ); // is "true"
//length()
int stringLength = StringUtils.calculateLength("123456");
System.out.println("Length of the string 0123456 is: "+stringLength);
//replace()
String replacedString = StringUtils.replaceString("Hardware","Hard","Soft");
System.out.println("The String Hardware is replaced with: "+replacedString);
//toUpperCase()
String upperCaseString = StringUtils.convertToUpperCase("Core Java");
System.out.println("Converted uppercase String is: "+upperCaseString);
//toLowerCase()
String lowerCaseString = StringUtils.convertToLowerCase("Core Java");
System.out.println("Converted uppercase String is: "+lowerCaseString);
//trim()
String trimmedString = StringUtils.trimSpaces(" Some Content ");
System.out.println("Trimmed String is : "+ trimmedString);
//isEmpty()
boolean isEmptyResult = StringUtils.isEmpty("Some Content");
System.out.println("is that the string is empty :"+ isEmptyResult);
}
}