String Methods Flashcards

1
Q

Declaring a String

A

String str = “Welcome”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Find Character at nth position of the string

A

Char Ch = str.charAt(nth);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Convert a String into Char Array

A

Char[] array = str.toCharArray();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Covert a String to Int

A
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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Convert a int to String

A
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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Convert string to Double

A

String str3 = 11.22;
Method 1 : Double var = Double.parseDouble(str3);
Method 2 : Double var3 = Double.valueOf(str3);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Convert ASCII to Char

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Convert Char to ASCII

A

char character = ‘a’;

int ascii = (int) character;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Covert a string to ArrayList

A
String num = "22,33,44,55,66,77";
 String[] str = num.split(",");
 List arraylist = new ArrayList();
 arraylist = Arrays.asList(str)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Find the Length of the string

A

int Length = str.length();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

find the index of first occurrence of the specified character ch in the string

A

int Position = str.indexOf(Char);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Add One string to other

A

String Combined = str1.concat(str2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Splitting the string

A

Char[] array = str.split(“,or / or any Regex”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Convert All the characters to Upper Case

A

String newUpper = str.toUpperCase();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Convert All the characters to Lower case

A

String newLower = str.toLowerCase();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

string matches

A

Input :
String str = new String(“Java String Methods”);
System.out.print(“Regex: (.)String(.) matches string? “ );
Output: TRUE

17
Q

Equality Check of two strings

A

stringOne.equal(stringTwo) returns TRUE if matches else false

18
Q

Some basic Validation for String Input Questions (Applies to Most)

A
  1. Is the string null ? -> (input == null)
  2. Is the string empty ? -> (input.isEmpty()) or (input.length == 0)
  3. Does the string have only one char ? -> (input.length == 1)
19
Q

Whats the difference between string.isEmpty() and string == null?

A

The empty string is a string with zero length. The null value is not having a string at all.
The expression s == null will return false if s is an empty string.
The second version will throw a NullPointerException if the string is null.
Here’s a table showing the differences:
+——-+———–+———————-+
| s | s == null | s.isEmpty() |
+——-+———–+———————-+
| null | true | NullPointerException |
| “” | false | true |
| “foo” | false | false |
+——-+———–+———————-+