Java String Class

Strings are widely used in Java programming. all strings are java objects, and java provides the String class for developer to create and operate strings.

Create String

A simple way to create a string:

The compiler will create a string object automatically when it find a string constant,as the code shown above,will create a string object that the value is "Asenview".
Also we can create string object by keyword and constructor´╝îfor example,use the constructor to create a string object:

Should know that,the string created by String is storage in the public pool menory,and the string object created by new function is storage in the heap menory.
There are 11 constructors in the String class, it means that we can create strings in many ways. for example,create a string by using character array:

the output is

please node that the string can’t be modify after created by string class,if you want to make some modification to a string,you should create the string by StringBuffer or StringBuilder class.

String length

The String length can be getted by the String.length() method,it will return the character count number of the specific string.

the output is

Concatenate String

The String class provide to methods to concatenate strings:

for example

the output is

alternative, we also can use "+" to concatenate strings.

the output is

Format String

we can use printf() or format() method to format string
for example


String methods

No. Method Description
1 char charAt(int index) It returns char value for the particular index
2 int length() It returns string length
3 static String format(String format, Object… args) It returns a formatted string.
4 static String format(Locale l, String format, Object… args) It returns formatted string with given locale.
5 String substring(int beginIndex) It returns substring for given begin index.
6 String substring(int beginIndex, int endIndex) It returns substring for given begin index and end index.
7 boolean contains(CharSequence s) It returns true or false after matching the sequence of char value.
8 static String join(CharSequence delimiter, CharSequence… elements) It returns a joined string.
9 static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements) It returns a joined string.
10 boolean equals(Object another) It checks the equality of string with the given object.
11 int compareTo(Object o) Compare this string to another object.
12 int compareTo(String anotherString) Compares two strings in lexicographical order.
13 String concat(String s) concatenate two string.

refer Class String