java stringsss
java stringsss
1. length()
Example:
java
Copy code
String str = "Hello";
System.out.println(str.length()); // Output: 5
●
2. charAt(int index)
Example:
java
Copy code
String str = "Hello";
System.out.println(str.charAt(1)); // Output: e
●
Example:
java
Copy code
String str = "Hello World";
System.out.println(str.substring(0, 5)); // Output: Hello
●
4. contains(CharSequence sequence)
●
Example:
java
Copy code
String str = "Hello World";
System.out.println(str.toLowerCase()); // Output: hello world
System.out.println(str.toUpperCase()); // Output: HELLO WORLD
●
6. trim()
Example:
java
Copy code
String str = " Hello World ";
System.out.println(str.trim()); // Output: Hello World
●
Example:
java
Copy code
String str1 = "Hello";
String str2 = "hello";
System.out.println(str1.equals(str2)); // Output: false
System.out.println(str1.equalsIgnoreCase(str2)); // Output: true
●
Example:
java
Copy code
String str = "Hello World";
System.out.println(str.replace('o', 'a')); // Output: Hella Warld
●
9. split(String regex)
Example:
java
Copy code
String str = "Java is fun";
String[] words = str.split(" ");
for (String word : words) {
System.out.println(word);
}
// Output:
// Java
// is
// fun
●
Example:
java
Copy code
String str = "Hello World";
System.out.println(str.indexOf('o')); // Output: 4
System.out.println(str.lastIndexOf('o')); // Output: 7
●
1. Reverse a String
Problem: Write a program to reverse a string without using the reverse function. Solution:
java
Copy code
public class ReverseString {
public static void main(String[] args) {
String str = "Interview";
StringBuilder reversed = new StringBuilder();
for (int i = str.length() - 1; i >= 0; i--) {
reversed.append(str.charAt(i));
}
System.out.println("Reversed String: " + reversed);
}
}
java
Copy code
import java.util.Arrays;
if (Arrays.equals(arr1, arr2)) {
System.out.println("The strings are anagrams.");
} else {
System.out.println("The strings are not anagrams.");
}
}
}
java
Copy code
import java.util.HashMap;
System.out.println(freq);
}
}
java
Copy code
public class PalindromeCheck {
public static void main(String[] args) {
String str = "madam";
String reversed = new StringBuilder(str).reverse().toString();
if (str.equals(reversed)) {
System.out.println("The string is a palindrome.");
} else {
System.out.println("The string is not a palindrome.");
}
}
}
java
Copy code
public class LongestPalindrome {
public static void main(String[] args) {
String str = "babad";
String result = "";
java
Copy code
public class VowelConsonantCount {
public static void main(String[] args) {
String str = "Java Programming";
int vowels = 0, consonants = 0;