📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Check out Java String class API guide at https://www.javaguides.net/2018/08/java-string-class-api-guide.html.
- String java.lang.String.repeat(int count) - As the name suggests, the repeat() instance method repeats the string content.
- String java.lang.String.strip() - The strip() instance method returns a string with all leading and trailing whitespaces removed.
- String java.lang.String.stripLeading() - This method returns a string with all leading white space removed.
- String java.lang.String.stripTrailing() - This method returns a string with all trailing white space removed.
- boolean java.lang.String.isBlank() - The isBlank() instance method returns true if the string is empty or contains only whitespace. Otherwise, it returns false.
- Stream java.lang.String.lines() - The lines() instance method returns a Stream of lines extracted from the string, separated by line terminators.
Video Tutorial
repeat() API
package net.javaguides.examples;
/**
* Java 11 (JDK11) New String APIs/Methods with Examples
* @author Ramesh Fadatare
*
*/
public class StringAPIInJDK11 {
public static void main(String[] args) {
String input = "Java";
/**
* the repeat() instance method repeats the string content
*/
String output = input.repeat(2) + "Guides".repeat(3);
System.out.println(output); // Output: JavaJavaGuidesGuidesGuides
}
}
JavaJavaGuidesGuidesGuides
strip(), stripLeading() and stripTrailing() methods
- String java.lang.String.strip() - The strip() instance method returns a string with all leading and trailing whitespaces removed.
- String java.lang.String.stripLeading() - This method returns a string with all leading white space removed.
- String java.lang.String.stripTrailing() - This method returns a string with all trailing white space removed.
package net.javaguides.examples;
/**
* Java 11 (JDK11) New String APIs/Methods with Examples
* @author Ramesh Fadatare
*
*/
public class StringAPIInJDK11 {
public static void main(String[] args) {
String input1 = "\n\t hello \u2005";
/**
* The strip() instance method returns a string
* with all leading and trailing whitespaces removed
*/
String output1 = input1.strip();
System.out.println(output1);
// stripLeading() usage
System.out.println(input1.stripLeading());
// stripTrailing() usage
System.out.println(input1.stripTrailing());
}
}
hello
hello ?
hello
isBlank() API
package net.javaguides.examples;
/**
* Java 11 (JDK11) New String APIs/Methods with Examples
* @author Ramesh Fadatare
*
*/
public class StringAPIInJDK11 {
public static void main(String[] args) {
String input = "\n\t\u2005 ";
/**
* The isBlank() instance method returns true if the string is empty or contains
* only whitespace. Otherwise, it returns false
*/
boolean result = input.isBlank();
System.out.println(result);
}
}
true
lines() API
package net.javaguides.examples;
import java.util.stream.Stream;
/**
* Java 11 (JDK11) New String APIs/Methods with Examples
*
* @author Ramesh Fadatare
*
*/
public class StringAPIInJDK11 {
public static void main(String[] args) {
/**
* The lines() instance method returns a Stream of lines extracted from the
* string, separated by line terminators
*/
String multilineStr = "This is\n a multiline\n string.";
Stream < String > stream = multilineStr.lines();
stream.forEach(action - > System.out.println(action));
long lineCount = multilineStr.lines().count();
System.out.println(lineCount);
}
}
This is
a multiline
string.
3
Conclusion
Check out the Java String class API guide at https://www.javaguides.net/2018/08/java-string-class-api-guide.html.
Comments
Post a Comment
Leave Comment