String Methods Flashcards

1
Q

valueOf()

A

Method Task: Convert given variable to a String

    - it is static - you can cll it with class name
    - it is a return type, and it returns a string
    - it takes different arguments as it is an overloaded method and converts given args to a String

int i = 5;

    String num = String.valueOf(i); // num  = 5
    System.out.println(i + i);// 10
    System.out.println(num+num);// 55
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

concat()

A

Method Task: It is used to combine multiple strings and form a new one

    - it is non-static, and we can call it with an object created
    - it is a return type method, and it returns a new String that is the combination of the others
    - it takes a String as an argument      

    String s1 = "Tech"; // String object
    String s2 = "Global"; // String object

    String s3 = s1.concat(s2); // String s3 = s1 + s2; // TechGlobal

    System.out.println(s3.concat(" School")); // TechGlobal School

    System.out.println("Hello".concat(" World!")); // Hello World!
    System.out.println("Hello".concat(" ").concat("World!")); // Hello World!
    System.out.println("Hello".concat(" ") + "World!"); // Hello World!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

equals()

A

Method Task: It compares 2 strings with each other and tells if they are equal or not
NOTE: this comparison is case-sensitive
-it is non-static, and we call it with another String object
-it is a return type and returns a boolean
-it takes a String as an argument

    System.out.println("Melda".equals("Melda")); // true
    System.out.println("melda".equals("Melda")); // false

    String name1 = "John";
    String name2 = "James";
    String name3 = "James";

    System.out.println(name1.equals(name2)); // false
    System.out.println(name2.equals(name3)); // true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

equalsIgnoreCase()

A

Method Task: It compares 2 strings with each other and tells if they are equal or not
NOTE: This comparison is case-insensitive
-It is non-static, and we call it with another String object
-It is a return type and returns a boolean
-It takes a String as an argument

    System.out.println("Hello".equals("hello")); //false
    System.out.println("Hello".equalsIgnoreCase("hello")); // true
    System.out.println("Hello".equalsIgnoreCase(" hello")); // false

    String s1 = "Good";
    String s2 = "GOOD";

    boolean b = s1.equalsIgnoreCase(s2); // true
    System.out.println(b);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

toLowerCase()

A

Method Task: These methods are used to convert letters of a String to uppercase or lowercase

    - They are non-static methods that you can call them with objects of String class
    - They are return type methods, and they return the modified String object back
    - They don't take any argument

    String s1 = "HELLO world 10$";

    System.out.println(s1); // HELLO world 10$
    System.out.println(s1.toLowerCase()); // hello world 10$
    System.out.println(s1.toUpperCase()); // HELLO WORLD 10$
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

toUpperCase()

A

Method Task: These methods are used to convert letters of a String to uppercase or lowercase

    - They are non-static methods that you can call them with objects of String class
    - They are return type methods, and they return the modified String object back
    - They don't take any argument

    String s1 = "HELLO world 10$";

    System.out.println(s1); // HELLO world 10$
    System.out.println(s1.toLowerCase()); // hello world 10$
    System.out.println(s1.toUpperCase()); // HELLO WORLD 10$
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

charAt(index)

A

Method Task: it helps to get a character at a specific index
NOTE: index starts with zero
-it is non-static as we call it with object name
-it is a return type and returns char primitive
-it takes an argument which is int index
NOTE: It will throw StringIndexOutOfBoundsException when the given index is not in the bounds

    String name = "John";

    System.out.println(name.charAt(0));//J
    System.out.println(name.charAt(1));//o
    System.out.println(name.charAt(2));//h
    System.out.println(name.charAt(3));//n
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

indexOf(anyChar)

indexOf(anyString)

A

Method Task:They are used to find the index or last index of some char or String values in another String
-They are non-static methods and called with another String object
-They are return type, and they return int as index
-They take either String or char as arguments
NOTE: if the given char or String does not exist, then they return -1
NOTE: if you are looking for an index of String, and it exists, it will return the first index of found match
EXAMPLE: sentence.indexOf(“Chicago”); // 7

    String sentence = "I like Chicago and Miami more than any other cities";

    System.out.println(sentence.indexOf('C'));//7
    System.out.println(sentence.indexOf('c'));//10
    System.out.println(sentence.lastIndexOf('c'));//45

    sentence. indexOf("Chicago"); // 7
    sentence. indexOf("Miami"); // 19
    sentence. indexOf("Florida"); // -1
    sentence. indexOf("Florida"); // -1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

indexOf(anyChar)
indexOf(anyString)

EXAMPLE:

A

String str = “TechGlobal”;

int indexOfLetterG = str.indexOf(‘G’);

int indexOfGlobalWord = str.indexOf(“Global”);

System.out.println(indexOfLetterG);

System.out.println(indexOfGlobalWord);

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

charAt(index)

EXAMPLE:

A

Example
String str = “Java is fun.”;
char c = str.charAt(3);
System.out.println(c);

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

toUpperCase()

EXAMPLE:

A

Example:
String str = “TechGloBAL”;
System.out.println(str.toUpperCase());

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

toLowerCase()

EXAMPLE:

A

Example
String str = “JAvA is FuN”;
System.out.println(str.toLowerCase());

Expected output:
java is fun
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

equalsIgnoreCase()

EXAMPLE:

A

Example
String str1 = “TechGlobal”;
String str2 = “TecHgLOBal”;

System.out.println(str1.equalsIgnoreCase(str2));

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

equals()

EXAMPLE:

A

Example
String str1 = “Java”;
String str2 = “Hello”;
System.out.println(str1.equals(str2));

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

concat()

EXAMPLE:

A

Example
String str = “Tech”;
String strNew = str.concat(“Global”);
System.out.println(strNew);

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

valueOf()

EXAMPLE:

A
Example
int number = 125;
boolean check = true;
String strNumber = String.valueOf(number);
String strCheck = String.valueOf(check);
System.out.println(strNumber);
System.out.println(strCheck);
Expected output:
 125
true
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

indexOf(int ch)

lastIndexOf(int ch)

A

Method Task: They are used to find the index or last index of some char or String values in another String
-They are non-static methods and called with another String object
-They are return type, and they return int as index
-They take either String or char as arguments
NOTE: if the given char or String does not exist, then they return -1
NOTE: if you are looking for an index of String, and it exists, it will return the first index of found match

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

length()

A

Method Task: Count the total number of the characters in a String and return it as an int
-It is a non-static method and can be called with object name
-It is a return type method, and it returns an int (total number of characters)
-It does not take any arguments
NOTE: It is like human thinking - HOW MANY

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

trim()

A

Method Task: It is used to remove whitespaces from both ends of a String
-it is non-static, and we call it with an object name
-it is return type and return a String
-it does not take any argument
NOTE: trim() method will not remove any extra space between the words,
it only removes extra spaces before and after the first and last chars in the String

20
Q

substring(int beginIndex)

A

Method Task: They are used to extract a substring from a larger string
-They are non-static, and we call them with objects
-They are return type, and they return another string
-There are 2 overloaded substring methods
1. substring(int beginIndex)
-This method takes begin index as an argument, and it extracts a substring starting from given index
to the end

21
Q

substring(int beginIndex, int endIndex)

A

Method Task: They are used to extract a substring from a larger string
-They are non-static, and we call them with objects
-They are return type, and they return another string
-There are 2 overloaded substring methods
2. substring(int beginIndex, int endIndex)
-This method will take 2 args to define which index to start and which index to stop to extract
a substring
NOTE: beginIndex is inclusive but endIndex is exclusive
NOTE: if your beginIndex is equal to endIndex, then it will return empty string
NOTE: if beginIndex is less than endIndex, then it will throw StringIndexOutOfBoundsException

22
Q

startsWith(String prefix)

endsWith(String suffix)

A

Method Task: They are used to finding out if the string starts with or ends with another letter or string

    - They are non-static as we call them with objects
    - They return boolean
    - They take String as an argument
23
Q

contains(CharSequence s)

A

Method Task: It is used to find out if a String contains another string or character

    - It is non-static, and we can call it with an object
    - it is a return type, and it returns a boolean
    - It takes a String argument
24
Q

replace(char oldChar, char newChar)

replace(CharSequence target, CharSequence replacement)

A

Method Task: It is used to replace some values in a String with another value
NOTE: it replaces all the occurrences.
-it is non-static
-it is a return type and returns another which is modified
-it takes 2 arguments (char or String)

25
Q

isEmpty()

A

Method Task: It is used to check if a given string is empty or not

    - it is non-static, and we call it with an object
    - it is a return type, and it returns a boolean
    - it does not take any arguments
26
Q

substring()

EXAMPLE:

A

String sentence = ScannerHelper.getASentenceFromUser();
String first = sentence.substring(0,sentence.indexOf(‘ ‘));
String last = sentence.substring(sentence.lastIndexOf(‘ ‘)+1);

    System.out.println("The first word is = '"+ first + "'");
    System.out.println("The last word is = '"+ last+"'");
27
Q

Given a string, return a version without the first and last char, so “Hello” yields “ell”. The string length will be at least 2.

A

String str = “hello”

return str.substring(1 , str.length( )- 1); // ell

28
Q

Index Error

A

StringIndexOutOfBoundsException

29
Q

Find the last character:

A

String word = “tenacity”;

word.charAt(word.length()-1); // y

30
Q

Find the first character:

A

String word = “tenacity”;

word.charAt(0); // t

31
Q

Find the last 3 characters:

A

String word = “tenacity”;

word.substring(word.length()-3);

32
Q

Find the first 3 characters:

A

String word = “tenacity”;

word.substring(0,3)

33
Q

Find the last word:

A

String sentence = “java is fun”

sentence.substring(sentence.lastIndexOf(‘ ‘)+1);

34
Q

Find the first word:

A

String sentence = “java is fun”;

sentence.substring(0 , sentence.indexOf(‘ ‘));

35
Q

contains()

EXAMPLE:

A

Example
String str = “TechGlobal”;
boolean checkIfStrContainsGlo = str.contains(“Glo”);
System.out.println(checkIfStrContainsGlo);

Expected output:
true

String s = “Good Morning”;

s. contains(“Good”); // true
s. contains(“good”); // false

36
Q

replace()

EXAMPLE:

A

Example
String str = “TechGlobalo”;
String capitalizeAllOLetters= str.replace(‘o’, ‘O’);
String removeTech = str.replace(“Tech”, “”);
System.out.println(capitalizeAllOLetters);

Expected output:
 TechGlObalO

String s = “banana”;

    System.out.println(s.replace('a' , '$'));
37
Q

isEmpty()

EXAMPLE:

A

Example
String str = “”;
boolean isStrEmpty= str.isEmpty();
System.out.println(isStrEmpty);

Expected output:
true

String name = “John”;

    System.out.println(name.isEmpty()); // false
    System.out.println(name.replace(name, "").isEmpty()); // true
38
Q

Method Chaining

A

if the chained method goes inside the argument you must add the object before the dot
example: word.charAt(word.length()-1);

if the chained method does NOT go inside the argument it does NOT get an object before the dot
example: word.toLowerCase().charAt(0);

39
Q

A method is…

A
  • It is also a function
  • Every method will have a task to execute when it is invoked
  • Reusable
40
Q

THERE ARE 2 TYPES OF METHODS IN JAVA

A
  1. void methods
    - does not return anything but executes a block of code
    - return keyword cannot be used with this method
    - can be created with void keyword
    - void methods can be static or non-static
  2. return type methods
    - does return something
    - return keyword MUST be used, otherwise it is a compiler error
    - cannot use void keyword
    - return type methods can be static or non-static

NOTE:
Each time you invoke a method, all the code block will be executed

41
Q

METHOD COMPONENTS

syntax:

A
accessModifier returnType methodName(){
// the body goes here
}
42
Q

METHOD COMPONENTS

Access Modifiers:

A
accessModifier returnType methodName(){
// the body goes here
}

access modifiers:
PUBLIC -> anywhere in the project

DEFAULT -> only in the same package

PRIVATE       -> only same class
PROTECTED   -> we will learn later
43
Q

METHOD COMPONENTS

Return Type:

A
accessModifier returnType methodName(){
// the body goes here
}

returnType
-It specifies what type of value a method returns. For example, if a method has an int return
type then it returns an integer value
-If the method does not return a value, its return type is void

44
Q

Get the 2 middle letters if an even lettered name

A

”” + str.charAt(str.length()/2-1) + str.charAt(str.length()/2)

45
Q

Get the middle letter if an odd lettered name

A

”” + str.charAt(str.length()/2)