STRINGS:
A String is a sequence of characters.
It is an object of Java String Class.
Once created, it cannot be changed.
Any modification creates a new object.
Strings are one of the most commonly used data types in Java. Whether you're building login systems, validating user input, or processing data, understanding how to work with strings is very important.
In this blog, we’ll explore essential string-handling techniques in Java with simple explanations and examples.
- Handling Trailing Spaces in Password
When users enter passwords, they may accidentally add spaces at the beginning or end. This can cause login failures even if the password is correct.
To solve this, we use the trim() method.
input.trim().equals(correctPassword);
This removes unwanted spaces before comparing the password.
2. Case-Insensitive Comparison
Sometimes we don’t want uppercase and lowercase letters to affect comparison.
nameFromUI.equalsIgnoreCase(nameFromDB);
This ensures "John" and "john" are treated as the same.
- Difference Between
==andequals()
This is a very common interview question:
-
==compares memory location (reference) -
equals()compares actual values
Always use equals() when comparing strings.
4. Email Validation (Basic)
We can do a simple check to validate an email:
email.contains("@") && email.contains(".");
Note: This is basic validation. Real-world applications use regex.
- Converting String to Integer
Sometimes we need to convert user input into numbers:
int number = Integer.parseInt(str);
This allows us to perform calculations.
- Removing Extra Spaces
str.trim();
This removes spaces from both ends of the string.
- Counting Occurrences of a Word
We can count how many times a word appears using indexOf():
int count = 0;
int index = 0;
while ((index = str.indexOf("java", index)) != -1) {
count++;
index += 4;
}
- Reversing a String
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.charAt(i);
}
- Palindrome Check
A palindrome reads the same forwards and backwards.
if (str.equals(reversed)) {
System.out.println("Palindrome");
}
- Removing Duplicate Characters
String result = "";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (result.indexOf(ch) == -1) {
result += ch;
}
}
- Extracting Substring from Log
int index = log.indexOf(":");
String message = log.substring(index + 1);
- Splitting CSV Data
String[] data = str.split(",");
This is useful when working with comma-separated values.
13. Replacing Substrings
str.replace("Java", "Spring Boot");
14. Mobile Number Validation
str.matches("\\d{10}");
This ensures the number has exactly 10 digits.
- Splitting String Using Delimiter
String[] parts = str.split("#");
Understanding string handling in Java is essential for real-world applications like login systems, data validation, and text processing.
By mastering these techniques, you can write cleaner, more efficient, and more reliable Java programs.
package Learning;
public class Leraningjava {
String nam;
String nam1;
String num;
String inpu ;
String value;
String valu;
String log;
Leraningjava(String valu ){
this.nam=nam;
this.nam1=nam1;
this.num=num;
this.inpu=inpu;
this.value=value;
this.valu=valu;
this.log=log;
}
Leraningjava(){
}
public void display(String nam) {
nam=nam.trim();
System.out.println(nam);
}
public void display1(String nam,String nam1) {
System.out.println(nam.equalsIgnoreCase(nam1));
}
public void display2(String num) {
int n = Integer.parseInt(num);
System.out.println(n + 10);
}
public void display7(String name) {
String[] words=name.split(" ");
int count=0;
for(String word:words) {
if(word.equals("java")) {
count++;
}
}
System.out.println(count);
}
public void display4(String inpu) {
String rev = "";
for(int i = inpu.length()-1; i >= 0; i--){
rev += inpu.charAt(i);
}
System.out.println(rev);
}
public void display9(String value) {
String reve="";
for(int i=value.length()-1; i>=0; i--) {
reve+=value.charAt(i);
}
if(value.equals(reve)) {
System.out.println("pliomdomre");
}else {
System.out.println("not pliomfrome");
}
}
public void display8(String log) {
String result = log.split(":")[1].trim();
System.out.println(result);
}
public void display9() {
String data = "Vijay,25,Chennai";
String[] arr = data.split(",");
System.out.println("Name: " + arr[0]);
System.out.println("Age: " + arr[1]);
System.out.println("City: " + arr[2]);
}
public void display6() {
String str = "I love Java programming";
str = str.replace("Java", "Spring Boot");
System.out.println(str);
}
public void displayy() {
String str = "apple#banana#mango";
String[] arr = str.split("#");
for(String s : arr){
System.out.println(s);
}
}
public void duplicate(String valu) {
String copy="";
for (int i=0;i< valu.length();i++) {
char ch = valu.charAt(i);
if(valu.indexOf(ch)==-1) {
copy=copy+ch;
}
}
System.out.println(copy);
}
public static void main(String[]args) {
Leraningjava s=new Leraningjava("");
s.duplicate("aabbccdd");
// s.display8("Error : file not found");
// s.display9();
// s.display6();
// s.displayy();
// s.display0("aabbccdd");
// s.display(" hayes ");
// s.display1("vijay", "Vijay");
// s.display2("hi");
// s.display3("java is easy to learn java is powerful");
// s.display4("java");
// s.display9("madam");
//
//
// String obj="java";
// String obj1=new String("java");
// System.out.println(obj==obj1);
// System.out.println(obj.equals(obj1));
//
//
// String str=" Admin123 ";
// String si= " Admin123 ";
//
//
// System.out.println(str.contains(si));
//
//
}
}
// String str=" Admin123 ";
// String s= " Admin123 ";
//
// System.out.println(str.charAt(0));
// System.out.println(str.compareTo(s));
// System.out.println(str.compareToIgnoreCase(s));
// System.out.println(s.codePointAt(0));
// System.out.println(str.isBlank());
// System.out.println(str.isEmpty());
// System.out.println(str.concat(s));
// System.out.println(str.contains(s));
//
// str=str.trim();
// System.out.println(str);
// System.out.println(s.equalsIgnoreCase(str));
// String s1="java";
// String s2=new String("java");
// System.out.println(s1==s2);
// System.out.println(s1.equals(s2));
// converted string to intger:
// String num="1230";
// int s=Integer.parseInt(num);
// System.out.println(s +10);
//
//
// remove start and end:
//
// String str=" Welcome to java ";
// System.out.println(str.trim());
//
Top comments (0)