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:
1 |
String str="AsenView" |
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:
1 |
String str=new String("AsenView"); |
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:
1 2 3 4 5 6 7 |
public class StringDemo{ public static void main(String args[]){ char[] helloArray = { 'a', 's', 'e', 'n', 'v', 'i','e','w'}; String helloString = new String(helloArray); System.out.println( helloString ); } } |
the output is
1 |
asenview |
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.
1 2 3 4 5 6 7 |
public class StringDemo { public static void main(String args[]) { String site = "www.asenview.com"; int len = site.length(); System.out.println( "the length of asenview domain is " + len ); } } |
the output is
1 |
the length of asenview domain is 16 |
Concatenate String
The String class provide to methods to concatenate strings:
1 |
String1.concat(String2); |
for example
1 2 |
String str="Hello,".concat("Asenview"); System.out.print(str) |
the output is
1 |
Hellow,Asenview |
alternative, we also can use "+" to concatenate strings.
1 2 |
String str="Hello,"+"Asenview"; System.out.print(str) |
the output is
1 |
Hellow,Asenview |
Format String
we can use printf() or format() method to format string
for example
1 2 3 4 |
System.out.printf("float variable is " + "%f, integer variable is " + " %d, string variable is " + "is %s", floatVar, intVar, stringVar); |
or
1 2 3 4 5 |
String fs; fs=System.out.format("float variable is " + "%f, integer variable is " + " %d, string variable is " + "is %s", floatVar, intVar, stringVar); |
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