Careeroza
Real World Example
Signup Form Validation
When you sign up on an app such as Zomato or LinkedIn, the backend cleans and validates the text you typed. String methods do most of that work.
Example
public class Main {
public static void main(String[] args) {
String name = " priya sharma ";
String email = " [email protected]";
String phone = "98765-43210";
String password = "secret";
String cleanName = name.trim();
cleanName = cleanName.substring(0, 1).toUpperCase() + cleanName.substring(1);
String cleanEmail = email.trim().toLowerCase();
String cleanPhone = phone.replace("-", "");
System.out.println("Name : " + cleanName);
System.out.println("Email : " + cleanEmail);
System.out.println("Phone : " + cleanPhone);
boolean emailOk = cleanEmail.contains("@") && cleanEmail.indexOf('.') > 0;
boolean phoneOk = cleanPhone.length() == 10 && cleanPhone.chars().allMatch(Character::isDigit);
boolean passwordOk = password.length() >= 8;
System.out.println("Email valid : " + emailOk);
System.out.println("Phone valid : " + phoneOk);
System.out.println("Password valid : " + passwordOk + (passwordOk ? "" : " (min 8 characters)"));
System.out.println("Masked phone : " + "*".repeat(6) + cleanPhone.substring(6));
}
}Output
Name : Priya sharma Email : [email protected] Phone : 9876543210 Email valid : true Phone valid : true Password valid : false (min 8 characters) Masked phone : ******3210