Java String Class : Page 2 of 2

STRING EQUALITY

public class StringsDemo2 {

	public static void main(String[] args) {
		String str1 = "My name is bob";
		String str2 = "My name is bob";
		String str3 = "My name " + "is bob"; //Compile time expression
		String name = "bob";
		String str4 = "My name is " + name;
		String str5 = new String("My name is bob");
		System.out.println("str1 == str2 : " + (str1 == str2));
		System.out.println("str2 == str3 : " + (str2 == str3));
		System.out.println("str3 == str1 : " + (str3 == str1));
		System.out.println("str4 == str5 : " + (str4 == str5));
		System.out.println("str1 == str4 : " + (str1 == str4));
		System.out.println("str1 == str5 : " + (str1 == str5));
		System.out.println("str1.equals(str2) : " + str1.equals(str2));
		System.out.println("str2.equals(str3) : " + str2.equals(str3));
		System.out.println("str3.equals(str1) : " + str3.equals(str1));
		System.out.println("str4.equals(str5) : " + str4.equals(str5));
		System.out.println("str1.equals(str4) : " + str1.equals(str4));
		System.out.println("str1.equals(str5) : " + str1.equals(str5));
	}
}

Download StringDemo2.java

Output

str1 == str2 : true
str2 == str3 : true
str3 == str1 : true
str4 == str5 : false
str1 == str4 : false
str1 == str5 : false
str1.equals(str2) : true
str2.equals(str3) : true
str3.equals(str1) : true
str4.equals(str5) : true
str1.equals(str4) : true
str1.equals(str5) : true

JAVA STRING FUNCTIONS

The following program explains the usage of the some of the basic String methods like ;

1.compareTo(String anotherString)

Compares two strings lexicographically.

2.getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)

Returns the length of this string.

3.length()

Copies characters from this string into the destination character array.

4.length()

Returns the length of this string.

5.equals(Object anObject)

Compares this String to another String, ignoring case considerations.

6.equalsIgnoreCase(String anotherString)

Compares this String to another String, ignoring case considerations.

7.toUpperCase()

Converts all of the characters in this String to upper case using the rules of the default locale.

8.toLowerCase()

Converts all of the characters in this String to upper case using the rules of the default locale.

9.concat(int ch)

Concatenates the specified string to the end of this string.

10.indexOfint ch)

Returns the index within this string of the first occurrence of the specified character.

11.indexOf(int ch, int fromIndex)

Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.

12. indexOf(String str)

Returns the index within this string of the first occurrence of the specified substring.

13. indexOf(String str, int fromIndex)

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

14. lastIndexOf(int ch)

Returns the index within this string of the last occurrence of the specified character.

15. lastIndexOf(int ch, int fromIndex)

Returns the index within this string of the last occurrence of the specified character, searching backward starting at the specified index.

16. lastIndexOf(String str)

Returns the index within this string of the rightmost occurrence of the specified substring.

17. lastIndexOf(String str, int fromIndex)

Returns the index within this string of the last occurrence of the specified substring, searching backward starting at the specified index.

18. substring(int beginIndex)

Returns a new string that is a substring of this string.

19. substring(int beginIndex, int endIndex)

Returns a new string that is a substring of this string.

20. replace(char oldChar, char newChar)

Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.

21. trim()

Returns a copy of the string, with leading and trailing whitespace omitted.

22. toString()

This object (which is already a string!) is itself returned.

public class StringsDemo3 {

	public static void main(String[] args) {
		String str1 = "My name is bob";
		char str2[] = new char[str1.length()];
		String str3 = "bob";
		String str4 = "cob";
		String str5 = "BoB";
		String str6 = "bob";
		System.out.println("Length of the String str1 : " + str1.length());
		System.out.println("Character at position 3 is : "
				+ str1.charAt(3));
		str1.getChars(0, str1.length(), str2, 0);
		System.out.print("The String str2 is : ");
		for (int i = 0; i < str2.length; i++) {
			System.out.print(str2[i]);
		}
		System.out.println();
		System.out.print("Comparision Test : ");
		if (str3.compareTo(str4) < 0) {
			System.out.print(str3 + " < " + str4);
		} else if (str3.compareTo(str4) > 0) {
			System.out.print(str3 + " > " + str4);
		} else {
			System.out.print(str3 + " equals " + str4);
		}
		System.out.println();
		System.out.print("Equals Test");
		System.out.println("str3.equalsIgnoreCase(5) : "
				+ str3.equalsIgnoreCase(str5));
		System.out.println("str3.equals(6) : " + str3.equals(str6));
		System.out.println("str1.equals(3) : " + str1.equals(str3));
		str5.toUpperCase(); //Strings are immutable
		System.out.println("str5 : " + str5);
		String temp = str5.toUpperCase();
		System.out.println("str5 Uppercase: " + temp);
		temp = str1.toLowerCase();
		System.out.println("str1 Lowercase: " + str1);
		System.out.println("str1.concat(str4): " + str1.concat(str4));
		String str7temp = "  \t\n Now for some Search and Replace Examples    ";
		String str7 = str7temp.trim();
		System.out.println("str7 : " + str7);
		String newStr = str7.replace('s', 'T');
		System.out.println("newStr : " + newStr);
		System.out.println("indexof Operations on Strings");
		System.out.println("Index of p in " + str7 + " : "
				+ str7.indexOf('p'));
		System.out.println("Index of for in " + str7 + " : "
				+ str7.indexOf("for"));
		System.out.println("str7.indexOf(for, 30) : "
				+ str7.indexOf("for", 30));
		System.out.println("str7.indexOf('p', 30) : "
				+ str7.indexOf('p', 30));
		System.out.println("str7.lastIndexOf('p') : "
				+ str7.lastIndexOf('p'));
		System.out.println("str7.lastIndexOf('p', 4) : "
				+ str7.lastIndexOf('p', 4));
		System.out.print("SubString Operations on Strings");
		String str8 = "SubString Example";
		String sub5 = str8.substring(5); // "ring Example"
		String sub3_6 = str8.substring(3, 6); // "Str"
		System.out.println("str8 : " + str8);
		System.out.println("str8.substring(5) : " + sub5);
		System.out.println("str8.substring(3,6) : " + sub3_6);
	}
}

Download StringDemo3.java

Output

Length of the String str1 : 14
Character at position 3 is : n
The String str2 is : My name is bob
Comparision Test : bob < cob
Equals Teststr3.equalsIgnoreCase(5) : true
str3.equals(6) : true
str1.equals(3) : false
str5 : BoB
str5 Uppercase: BOB
str1 Lowercase: My name is bob
str1.concat(str4): My name is bobcob
str7 : Now for some Search and Replace Examples
newStr : Now for Tome Search and Replace ExampleT
Indexof Operations on Strings
Index of p in Now for some Search and Replace Examples : 26
Index of for in Now for some Search and Replace Examples : 4
str7.indexOf(for, 30) : -1
str7.indexOf(‘p’, 30) : 36
str7.lastIndexOf(‘p’) : 36
str7.lastIndexOf(‘p’, 4) : -1
SubString Operations on Stringsstr8 : SubString Example
str8.substring(5) : ring Example
str8.substring(3,6) : Str

Below is a program to check for Alpha Numeric character’s in the string.

public class TestAlphaNumericCharacters {

	private void isAlphaNumeric(final String input) {
		boolean isCharFlag = false;
		boolean isNumberFlag = false;
		final char[] chars = input.toCharArray();
		for (int x = 0; x &lt; chars.length; x++) {
			char c = chars[x];
			//            	 lowercase &amp;&amp; uppercase alphabet
			if ((c &gt;= 'a') &amp;&amp; (c &lt;= 'z') || (c &gt;= 'A') &amp;&amp;         (c &lt;= 'Z')) {
				isCharFlag = true;
				continue;
			}
			if ((c &gt;= '0') &amp;&amp; (c &lt;= '9')) { // numeric
				isNumberFlag = true;
				continue;
			}
		}
		System.out.println("characters are present:" + isCharFlag);
		System.out.println("Numbers are present :" + isNumberFlag);
	}
	public static void main(String[] args) {
		TestAlphaNumericCharacters tANC = new TestAlphaNumericCharacters();
		tANC.isAlphaNumeric("beginn3ers");
	}
}
<span style="font-size: small;"><strong><a href="http://www.javabeginner.com/test-alphanumeric-characters.zip">Download</a></strong> <strong class="style5"><span style="font-weight: 400;">TestAlphaNumericCharacters.java</span></strong></span>

Output

characters are present:true
Numbers are present :true

Below is a java program to Reverse a string (Reverse by words / characters)

import java.util.*;

public class StringReverse {

	public static void main(String[] args) {
		String input = "beginner java tutorial";
		Stack stack = new Stack();                                                                                  //A Stack is a Last In First Out Data Structure
		StringTokenizer stringTokenizer = new StringTokenizer(input);
		while (stringTokenizer.hasMoreTokens()) {
			stack.push(stringTokenizer.nextElement());
		}
		System.out.println("Original String: " + input);
		System.out.print("Reversed String by Words: ");
		while (!stack.empty()) {
			System.out.print(stack.pop());
			System.out.print(" ");
		}
		System.out.println();
		System.out.print("Reversed String by characters: ");
		StringBuffer rev = new StringBuffer(input).reverse();
		System.out.print(rev);
	}
}

Download StringReverse.java

Output

Original String: beginner java tutorial
Reversed String by Words: tutorial java beginner
Reversed String by characters: lairotut avaj rennigeb

Like us on Facebook