Adding java string basic methods

This commit is contained in:
Mayank Agarwal
2023-03-04 00:15:35 +05:30
parent db023b8db8
commit b59fdf2e29
2 changed files with 80 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
import java.util.*;
public class JavaContinue {
public static void main(String[] args) {
String str1 = "Hello, World!";
String str2 = new String("Hello, World!");
}
public static void stringPool() {
String str1 = "Hello, World!";
String str2 = "Hello, World!";
}
public static void accessingStringData(String[] args) {
String str = "Hello, World!";
char firstChar = str.charAt(0);
}
public static void manipulateString(String[] args) {
String str1 = "Hello, ";
String str2 = "World!";
String result = str1 + str2;
String str = "Hello, World!";
String upperCaseStr = str.toUpperCase();
String lowerCaseStr = str.toLowerCase();
}
}

View File

@@ -0,0 +1,52 @@
import java.util.*;
public class JavaContinue {
public static void main(String[] args) {
String str = "Welcome To Javadevjournal!";
int length = str.length();
System.out.println("The length of the string is: " + length);
}
public static void javaMethods() {
// Using string literals
String myString = "Hello, World!";
// Using the new keyword
String myString = new String("Hello, World!");
// Using the valueOf() method
int myInt = 42;
String myString = String.valueOf(myInt);
// Using StringBuilder or StringBuffer
StringBuilder builder = new StringBuilder();
builder.append("Hello");
builder.append(", ");
builder.append("World!");
String myString = builder.toString();
}
public static void charAtExample(String[] args) {
String str = "Welcome To Javadevjournal!";
char ch = str.charAt(1);
System.out.println("The character at index 1 is: " + ch);
}
public static void substringExample(String[] args) {
String str = "Welcome To Javadevjournal!";
String subStr = str.substring(11, 24);
System.out.println("The substring is: " + subStr);
}
public static void indexOfExample(String[] args) {
String str = "Welcome to Javadevjournal";
int index = str.indexOf("o");
System.out.println("The index of the first 'o' is: " + index);
}
public static void replaceMethod(String[] args) {
String str = "Welcome to Javadevjournal!";
String newStr = str.replace("o", "0");
System.out.println("The new string is: " + newStr);
}
}