String Methods Flashcards
Declaring a String
String str = “Welcome”;
Find Character at nth position of the string
Char Ch = str.charAt(nth);
Convert a String into Char Array
Char[] array = str.toCharArray();
Covert a String to Int
Method 1 : Using Integer.parseInt String str3="1234"; int num3 = Integer.parseInt(str3) ============================== Method 2: Using Integer.valueOf String str="1122"; int num = Integer.valueOf(str);
Convert a int to String
Method 1: Using Integer.toString(int i) int ivar2 = 200; String str2 = Integer.toString(ivar2); ================================= Method 2: Using String.valueOf(int i) int ivar = 111; String str = String.valueOf(ivar);
Convert string to Double
String str3 = 11.22;
Method 1 : Double var = Double.parseDouble(str3);
Method 2 : Double var3 = Double.valueOf(str3);
Convert ASCII to Char
I/P - int num[] = {65, 120, 98, 75, 115};
String str1 =null;
String str2 =null;
for(int i: num){
Method 1 -> str1 = Character.toString((char)i);
Method 1 -> str2 = String.valueof(Character.toChar()int);
System.out.print(str+ “ “);
}
O/P - A x b K s
Convert Char to ASCII
char character = ‘a’;
int ascii = (int) character;
Covert a string to ArrayList
String num = "22,33,44,55,66,77"; String[] str = num.split(","); List arraylist = new ArrayList(); arraylist = Arrays.asList(str)
Find the Length of the string
int Length = str.length();
find the index of first occurrence of the specified character ch in the string
int Position = str.indexOf(Char);
Add One string to other
String Combined = str1.concat(str2)
Splitting the string
Char[] array = str.split(“,or / or any Regex”);
Convert All the characters to Upper Case
String newUpper = str.toUpperCase();
Convert All the characters to Lower case
String newLower = str.toLowerCase();