Java String.replaceAll() The method replaceAll() replaces all occurrences of a String in another String. Available Signatures [source,java,gutter:,false] public String replaceAll(String regex, String replacement) Example [source,java,gutter:,true] @Test public void whenCallReplace_thenCorrect() { String s = “I learn Spanish”; assertEquals(“I learn French”, s.replaceAll(“Spanish”, “French”)); }

Java String.substring() The method substring() comes with two signatures. If we pass the beginIndex and the endIndex to the method, then it obtains a part of a String given the starting index and the length of the result. We can also pass the beginIndex only and obtain the part of… Continue Reading substring

Java String.startsWith() The method startsWith() is a convenience method that checks whether a String starts with another String. We can also pass the index of the first character to start checking from. Available Signatures [source,java,gutter:,false] public boolean startsWith(String prefix) public boolean startsWith(String prefix, int toffset) Example [source,java,gutter:,true] @Test public void… Continue Reading starts-with

Java String.trim() The method trim() removes any whitespace at the beginning and at the end of a String. If the String contains only spaces, then the method returns an empty String. Available Signatures [source,java,gutter:,false] public String trim() Example [source,java,gutter:,true] @Test public void whenTrim_thenCorrect() { assertEquals(“foo”, ” foo “.trim()); }

Check If a String Contains Multiple Keywords 1. Introduction In this quick tutorial, we’ll find out how to detect multiple words inside of a string. 2. Our Example Let’s suppose we have the string: String inputString = “hello there, Baeldung”; Our task is to find whether the inputString contains the “hello” and… Continue Reading string-contains-multiple-words

Guide to java.util.Formatter 1. Overview In this article, we’ll discuss the String formatting in Java using the java.util.Formatter class, which provides support for the layout justification and alignment. 2. How to Use the Formatter Remember C’s printf? Formatting a String in Java feels very similar. The format() method of the… Continue Reading java-string-formatter