String Methods Flashcards
valueOf()
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
concat()
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!
equals()
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
equalsIgnoreCase()
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);
toLowerCase()
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$
toUpperCase()
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$
charAt(index)
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
indexOf(anyChar)
indexOf(anyString)
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
indexOf(anyChar)
indexOf(anyString)
EXAMPLE:
String str = “TechGlobal”;
int indexOfLetterG = str.indexOf(‘G’);
int indexOfGlobalWord = str.indexOf(“Global”);
System.out.println(indexOfLetterG);
System.out.println(indexOfGlobalWord);
charAt(index)
EXAMPLE:
Example
String str = “Java is fun.”;
char c = str.charAt(3);
System.out.println(c);
Expected output: a
toUpperCase()
EXAMPLE:
Example:
String str = “TechGloBAL”;
System.out.println(str.toUpperCase());
Expected output: TECHGLOBAL
toLowerCase()
EXAMPLE:
Example
String str = “JAvA is FuN”;
System.out.println(str.toLowerCase());
Expected output: java is fun
equalsIgnoreCase()
EXAMPLE:
Example
String str1 = “TechGlobal”;
String str2 = “TecHgLOBal”;
System.out.println(str1.equalsIgnoreCase(str2));
Expected output: true
equals()
EXAMPLE:
Example
String str1 = “Java”;
String str2 = “Hello”;
System.out.println(str1.equals(str2));
Expected output: false
concat()
EXAMPLE:
Example
String str = “Tech”;
String strNew = str.concat(“Global”);
System.out.println(strNew);
Expected output: TechGlobal
valueOf()
EXAMPLE:
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
indexOf(int ch)
lastIndexOf(int ch)
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
length()
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