Java String Methods

Java has a lot of String Methods that allow us to work with Strings. string is a combination of characters. In Java, objects of String are immutable which means a constant whose value we cannot change once created.

The String class has a following built-in methods that we can use on strings.

charAt() Method :-

  • The charAt() method returns the character at the specified index in a string.
  • The index of the first character value will be 0.

              Syntax       public char charAt(int index)

codePointAt() Method :-

  • Returns the Unicode of the character at the specified index.
  • Here at index 2 position character is ‘c’.
  • the Unicode value of ‘c’ is 99.

codePointBefore method :-

  • Returns the Unicode of the character before the specified index.
  • Here at index 1 position character is ‘b’ but the Unicode value of ‘a’ is 97 will be, because it take 1 index before.

compareTo method :-

  • Compares two strings lexicographically.
  • Here s1 come before s2 and s1 is greater than s2.

compareToIgnoreCase method :-

  • Compares two strings lexicographically, ignoring case differences.
  • Here s1 is in lowercase and s3 is in uppercase character. In s1 String is more character than s2 String.

concat method :-

  • Concatenates a string to the end of another string.
  • Here concat s1 and s2 String and make an another String.

contentEquals method :-

  • The contentEquals() method check a string to find out if it contains the same sequence of characters in the given string.
  • Returns true if the both String have same characters exist and false if not same.

endWith method :-

  • Checks whether a string ends with the specified character or not.
  • Here s1 String endWith ‘gh’ thats why they return true.

equals method :-

  • equals method compares two strings. it returns true if the strings are equal, and false if not.
  • Here s1 String not equals with “asd” thats why they return false.

hashCode method :-

  • It returns the hash code of a string.

              Syntax                public int hashCode()

indexOf method :-

  • Returns the position of the first found occurrence of given characters in a string.
  • Here position of “c” inside string s1 is 2.

intern method :-

  • This method returns the canonical representation for the string object.
  • They will store s1 string object inside string constant pool.

isEmpty method :-

  • This method checks whether a string is empty or not.it returns true if the string is empty and false if not.
  • Here s1 string have not any data.

               Syntax                  public boolean isEmpty()

valueOf method :-

  • The valueOf() method converts different types like character,int,boolean of values into string.
  • Here we convert character ‘k’ into String.

               convert char to String –

                Syntax              public static String valueOf(char[] c)

length method :-

  • Returns the total number of characters in the String.

               Syntax            public int length()

split method :-

  • Splits a string into an array of substrings.
  • Here we divided String s1 in two part.it will be break at position on “d”.

               Syntax              Public String [] split ( String regex, int limit)

				
					public class StringMethodDemo {

	public static void main(String[] args) {

		String s1 = "abcdefgh";
		String s2 = "abcdef";
		String s3 = "ABCDEF";
		String s4 = "";

		// using charAt method
		System.out.println("from charAt method output :- " + s1.charAt(0));

		// using codePointAt method
		System.out.println("from codePointAt method output :- " + s1.codePointAt(2));

		// using codePointBefore method
		System.out.println("from codePointBefore method output :- " + s1.codePointBefore(1));

		// using compareTo method
		System.out.println("from compareTo method output :- " + s1.compareTo(s2));

		// using compareToIgnoreCase method
		System.out.println("from compareToIgnoreCase method output :- " + s1.compareToIgnoreCase(s3));

		// using concat method
		System.out.println("from concat method output :- " + s1.concat(s2));

		// using contentEquals method
		System.out.println("from contentEquals method output :- " + s1.contentEquals(s2));

		// using endWith method
		System.out.println("from endWith method output :- " + s1.endsWith("gh"));

		// using equals method
		System.out.println("from equals method output :- " + s1.equals("asd"));

		// using hashCode method
		System.out.println("from hashCode method output :- " + s1.hashCode());

		// using indexOf method
		System.out.println("from indexOf method output :- " + s1.indexOf("c"));

		// using intern method
		System.out.println("from intern method output :- " + s1.intern());

		// using isEmpty method
		System.out.println("from isEmpty method output :- " + s4.isEmpty());

		// using valueOf method
		System.out.println("from valueOf method output :- " + String.valueOf('k'));

		// using length method
		System.out.println("from length method output :- " + s1.length());

		// using split method
		String[] arrOfStr = s1.split("d", 2);
		for (String str : arrOfStr) {
			System.out.println("from split method output :- " + str);
		}

	}
}

				
			

Output :-
from charAt method output :- a
from codePointAt method output :- 99
from codePointBefore method output :- 97
from compareTo method output :- 2
from compareToIgnoreCase method output :- 2
from concat method output :- abcdefghabcdef
from contentEquals method output :- false
from endWith method output :- true
from equals method output :- false
from hashCode method output :- 1259673732
from indexOf method output :- 2
from intern method output :- abcdefgh
from isEmpty method output :- true
from valueOf method output :- k
from length method output :- 8
from split method output :- abc
from split method output :- efgh