In this article, we will discuss how to get length of the String using String’s length() method
1. String’s length() method:
- This String method is used to get length of the string
- sequence of characters
- number of characters
1.1 Method Signature:
public int length();
1.2 Returns:
- The length of the sequence of characters represented by this object
2. Examples on length() method:
Generally, length() method of String class is used for following purpose,
- To get length of any String
- for-loop : used to keep boundary condition, while iterating
- while-loop : used to keep boundary condition, while iterating
- do-while-loop : used to keep boundary condition, while iterating
- To create equivalent char[] array
Let’s see examples for each listed cases above,
2.1 Get length of any string
StringLengthMethod.java
package in.bench.resources.string.methods;
public class StringLengthMethod {
public static void main(String[] args) {
// sample string
String url = "BenchResources.Net";
// to check length of the string
int len = url.length();
// print to console
System.out.println("The length of string '"
+ url + "' is " + len);
}
}
Output:
The length of string 'BenchResources.Net' is 18
2.2 Boundary condition for for-loop while iterating
StringLengthForLoop.java
package in.bench.resources.string.methods;
public class StringLengthForLoop {
public static void main(String[] args) {
// sample string
String url = "BenchResources.Net";
// iterating using for-loop
for(int index = 0; index < url.length(); index++) {
System.out.println("The character at " + index
+ "-position is : " + url.charAt(index));
}
}
}
Output:
The character at 0-position is : B
The character at 1-position is : e
The character at 2-position is : n
The character at 3-position is : c
The character at 4-position is : h
The character at 5-position is : R
The character at 6-position is : e
The character at 7-position is : s
The character at 8-position is : o
The character at 9-position is : u
The character at 10-position is : r
The character at 11-position is : c
The character at 12-position is : e
The character at 13-position is : s
The character at 14-position is : .
The character at 15-position is : N
The character at 16-position is : e
The character at 17-position is : t
2.3 Boundary condition for while-loop while iterating
StringLengthWhileLoop.java
package in.bench.resources.string.methods;
public class StringLengthWhileLoop {
public static void main(String[] args) {
// sample string
String url = "BenchResources.Net";
// initialize index
int index = 0;
// get length
int length = url.length();
// iterating using while-loop
while(index < length){
// print to console
System.out.println("The character at " + index
+ "-position is : " + url.charAt(index));
// increment index-value by 1
index++;
}
}
}
Output:
The character at 0-position is : B
The character at 1-position is : e
The character at 2-position is : n
The character at 3-position is : c
The character at 4-position is : h
The character at 5-position is : R
The character at 6-position is : e
The character at 7-position is : s
The character at 8-position is : o
The character at 9-position is : u
The character at 10-position is : r
The character at 11-position is : c
The character at 12-position is : e
The character at 13-position is : s
The character at 14-position is : .
The character at 15-position is : N
The character at 16-position is : e
The character at 17-position is : t
2.4 Boundary condition for do-while-loop while iterating
StringLengthDoWhileLoop.java
package in.bench.resources.string.methods;
public class StringLengthDoWhileLoop {
public static void main(String[] args) {
// sample string
String url = "BenchResources.Net";
// initialize index
int index = 0;
// get length
int length = url.length();
// iterating using do-while-loop
do {
// print to console
System.out.println("The character at " + index
+ "-position is : " + url.charAt(index));
// increment index-value by 1
index++;
} while(index < length);
}
}
Output:
The character at 0-position is : B
The character at 1-position is : e
The character at 2-position is : n
The character at 3-position is : c
The character at 4-position is : h
The character at 5-position is : R
The character at 6-position is : e
The character at 7-position is : s
The character at 8-position is : o
The character at 9-position is : u
The character at 10-position is : r
The character at 11-position is : c
The character at 12-position is : e
The character at 13-position is : s
The character at 14-position is : .
The character at 15-position is : N
The character at 16-position is : e
The character at 17-position is : t
Difference between while-loop and do-while-loop ?
- do-while-loop checks boundary condition, after 1st loop iteration (at least 1 iteration is possible)
- while-loop checks boundary condition even before 1st iteration (no iteration is possible without satisfying loop-entry condition)
2.5 To create equivalent char[] array
StringLengthCreateCharArray.java
package in.bench.resources.string.methods;
public class StringLengthCreateCharArray {
public static void main(String[] args) {
// sample string
String url = "BenchResources.Net";
// get length
int length = url.length();
// create character array object
char[] array = new char[length];
// iterate using for-loop & store into char[] array
for (int index = 0; index < length ; index++) {
// store into char[] array
array[index] = url.charAt(index);
}
// print to console - this is demo purpose
System.out.println("The converted char[] array is : "
+ String.valueOf(array));
}
}
Output:
The converted char[] array is : BenchResources.Net
Related Articles:
- Java – String charAt(int index) method
- Java – String compareTo(String anotherString) method
- Java – String compareToIgnoreCase(String str) method
- Java – String concat(String str) method
- Java – String contains(CharSequence s) method
- Java – String contentEquals(StringBuffer sb) method
- Java – String copyValueOf(char[] data) method (2)
- Java – String endsWith(String suffix) method
- Java – String equals(Object anObject) method
- Java – String equalsIgnoreCase(Object anObject) method
- Java – String format(String format, Object… args) method
- Java – String getBytes() method (4)
- Java – String getChars() method
- Java – String hashCode() method
- Java – String indexOf() method (4)
- Java – String intern() method
- Java – String isEmpty() method
- Java – String join() method (2)
- Java – String lastIndexOf() method (4)
- Java – String length() method
- Java – String matches(String regex) method
- Java – String regionMatches() method (2)
- Java – String replace(char oldChar, char newChar) method
- Java – String replace(CharSequence target, CharSequence replacement) method
- Java – String replaceAll(String regex, String replacement) method
- Java – String replaceFirst(String regex, String replacement) method
- Java – String split(String regex) method
- Java – String split(String regex, int limit) method
- Java – String startsWith(String prefix) method
- Java – String startsWith(String prefix, int toffset) method
- Java – String subSequence(int beginIndex, int endIndex) method
- Java – String substring(int beginIndex) method
- Java – String substring(int beginIndex, int endIndex) method
- Java – String toCharArray() method
- Java – String toLowerCase() method (2)
- Java – String toUpperCase() method (2)
- Java – String toString() method
- Java – String trim() method
- Java – String valueOf() method (9)
References:
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuffer.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html
Happy Coding !!
Happy Learning !!