String Class in Java

Strings are treated as objects in Java. And to create and manipulate strings, a seperate class is available in Java.

Here is the explanation with a complete and thorough example:String Class in Java

package com.jasper.main;

/**
 * The class StringClass includes the small and basic topics of most widely used
 * Java Class 'String' like creation, methods and operations available. Further
 * in the same topic we enclose discussion and facts on behavior of String and
 * Memory used by String
 * 
 * @author Sushant Ashu
 * @Date 12-09-2012
 * @Volume String.1
 */
public class StringClass {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		creatingString();
		operationOnString();
	}

	/**
	 * There are several ways of creating a String object. The following method
	 * shows those ways.
	 */
	private static void creatingString() {
		String s1 = "String 1";
		String s2 = new String("String 2");

		System.out.println("Value of String s1 = " + s1);
		System.out.println("Value of String s2 = " + s2);
	}

	/**
	 * Basic Functions available.
	 */
	private static void operationOnString() {
		String s1 = "My name is Tom";
		String s2 = "My NAME IS TOM";

		/**
		 * charAt() Returns the char value at the specified index. An index
		 * ranges from 0 to length() - 1. The first char value of the sequence
		 * is at index 0, the next at index 1, and so on, as for array indexing.
		 */
		System.out.println(s1.charAt(3));

		/**
		 * contains() returns true if and only if this string contains the
		 * specified sequence of char values.Here nam is present in s1 so it
		 * will return true
		 */
		System.out.println(s1.contains("name"));

		/**
		 * endsWith() checks if the string ends with the specified character
		 * sequence. Here it will return false as s1 ends with 'Tom'
		 */
		System.out.println(s1.endsWith("is"));

		/**
		 * replace(oldChar,newChar) method takes two argument . The first one is
		 * the string which is to be replaced and the secon is the one by which
		 * it should be replaced.
		 * 
		 * If the character oldChar does not occur in the character sequence
		 * represented by this String object, then a reference to this String
		 * object is returned. Otherwise, a new String object is created that
		 * represents a character sequence identical to the character sequence
		 * represented by this String object, except that every occurrence of
		 * oldChar is replaced by an occurrence of newChar.
		 * 
		 * Examples:
		 * 
		 * "Hello Tom".replace('e', 'o') returns "Hollo Tom"
		 * 
		 */
		System.out.println(s1.replace("y", "4"));

		/**
		 * toUpperCase():This method converts the String to Upper Case
		 * characters
		 */
		System.out.println(s1.toUpperCase());

		/**
		 * toLowerCase():This method converts the String to Lower Case
		 * characters
		 */
		System.out.println(s1.toLowerCase());

		/**
		 * Other than these functions there are lots of functions available with
		 * description on each one of these in the Java API.(Job Left for the
		 * readers)
		 * 
		 * http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html
		 */
		System.out.println(s1.compareTo(s2));
		System.out.println(s1.compareToIgnoreCase(s2));
		System.out.println(s1.concat(s2));
		System.out.println(s1 + s2);
		System.out.println(s1.equals(s2));
		System.out.println(s1.equalsIgnoreCase(s2));
		System.out.println(s1.hashCode());
		System.out.println(s1.indexOf("name"));
		System.out.println(s1.isEmpty());
		System.out.println(s1.length());
		System.out.println(s1.startsWith("My"));
		System.out.println(s1.substring(2));
		System.out.println(s1.substring(2, 5));
		System.out.println(s1.trim());
	}

}

Leave a Reply

Your email address will not be published. Required fields are marked *