String trim, length, is empty, and substring
In this chapter you will learn:
- Trim a string, remove the leading and ending spaces
- How to get sub string from a string
- How to check if a string is an empty string
- How to Get the length of a string
Trim a string, remove the leading and ending spaces
String trim()
returns a copy of the string, with leading and trailing whitespace omitted.
public class Main {
public static void main(String[] argv) {
String str = " d e m o 2 s.com ";
System.out.println(">"+str+"<");
str = str.trim();//from j av a 2 s . c o m
System.out.println(">"+str+"<");
}
}
The output:
Get sub string from a string
String class has methods to get only part of a string. They are useful
when we want to get specific part of a string. We usually use the indexOf
and lastIndexOf
method to get the index first and then use the substring
method get the string we are looking for.
CharSequence subSequence(int beginIndex, int endIndex)
returns a new character sequence that is a subsequence of this sequence.String substring(int beginIndex)
returns a new string that is a substring of this string.String substring(int beginIndex, int endIndex)
returns a new string that is a substring of this string.
public class Main {
public static void main(String[] argv) {
String str = "demo2s.com";
System.out.println(str.substring(2));
System.out.println(str.substring(1,3));
}/* j a v a2s.c om*/
}
The output:
if string is empty
boolean isEmpty()
returns true if, and only if, length() is 0.
It is useful to check if a string has no value in it, in other words, if a string is ""
.
However isEmpty
would not work if a string object is null
.
public class Main {
public static void main(String[] argv) {
String str = "";
System.out.println(str.isEmpty());/*from ja v a 2 s.c o m*/
System.out.println(str.length());
str = "demo2s.com";
System.out.println(str.isEmpty());
System.out.println(str.length());
}
}
The output:
To check if a string object is null
, use the following
public class Main {
public static void main(String[] argv) {
String str = "";
System.out.println(str.isEmpty());// j a v a 2 s . c om
System.out.println(str.length());
str = null;
if(str == null){
System.out.println("string is empty");
}
str = "demo2s.com";
System.out.println(str.isEmpty());
System.out.println(str.length());
}
}
Get string length
The length of a string is the number of character in a string.
int length()
returns the length of this string.
public class Main {
public static void main(String[] argv) {
String str = "";
System.out.println(str.isEmpty());/*from j av a2s.c o m*/
System.out.println(str.length());
str = "demo2s.com";
System.out.println(str.isEmpty());
System.out.println(str.length());
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- Replaces all occourances of given character with new one and returns new String object
- Replaces only first occourances of given String with new one and returns new String object
- Replaces all occourances of given String with new one and returns new String object
- How to replace escaped string
- Replace \r\n with the <br> tag