+91 81900 98289

String Methods

toLowerCase and toUpperCase change case and return a new string. trim removes spaces, tabs, and newlines at both ends. It does not remove spaces between words.

replace swaps every match of one sequence. substring(start, end) takes the start index and stops before the end index. contains, startsWith, and endsWith answer yes-or-no questions about the text.

Example

trim removes the outer spaces first. toLowerCase then returns java. replace changes the av in the middle.

public class Clean { public static void main(String[] args) { String raw = " Java "; String tidy = raw.trim().toLowerCase(); System.out.println(tidy); System.out.println(tidy.replace("av", "AV")); } }
Call trim before you compare a value that came from a form or a file. A trailing space makes equals return false.