Document Details

ProblemFreeSerpentine9248

Uploaded by ProblemFreeSerpentine9248

Bestlink College of the Philippines

Tags

java string methods string manipulation java programming computer science

Summary

This document provides examples and explanations of various Java String methods, including length(), charAt(), substring(), contains(), equals(), equalsIgnoreCase(), and more. It details how to use these methods for common string operations. The examples showcase different scenarios and outputs.

Full Transcript

JAVA STRING METHODS Java provides a wide range of methods for working with String objects. 1. length()  Description: Returns the length of the string. String str = "Hello, World!"; int length = str.length(); System.out.println(length); // Output: 13 2. charAt(int index...

JAVA STRING METHODS Java provides a wide range of methods for working with String objects. 1. length()  Description: Returns the length of the string. String str = "Hello, World!"; int length = str.length(); System.out.println(length); // Output: 13 2. charAt(int index)  Description: Returns the character at the specified index. String str = "Hello"; char ch = str.charAt(1); System.out.println(ch); // Output: 'e' 3. substring(int beginIndex)  Description: Returns a new string that is a substring starting from the specified index. String str = "Hello, World!"; String sub = str.substring(7); System.out.println(sub); // Output: "World!" 4. substring(int beginIndex, int endIndex)  Description: Returns a substring from beginIndex to endIndex - 1. String str = "Hello, World!"; String sub = str.substring(0, 5); System.out.println(sub); // Output: "Hello" 5. contains(CharSequence sequence)  Description: Checks if the string contains the specified character sequence. String str = "Hello, World!"; boolean contains = str.contains("World"); System.out.println(contains); // Output: true 6. equals(Object another)  Description: Compares the string to the specified object for equality. String str1 = "Hello"; String str2 = "Hello"; boolean isEqual = str1.equals(str2); System.out.println(isEqual); // Output: true 7. equalsIgnoreCase(String another)  Description: Compares two strings, ignoring case differences. String str1 = "HELLO"; String str2 = "hello"; boolean isEqual = str1.equalsIgnoreCase(str2); System.out.println(isEqual); // Output: true 8. indexOf(String str)  Description: Returns the index of the first occurrence of the substring. String str = "Hello, World!"; int index = str.indexOf("World"); System.out.println(index); // Output: 7 9. indexOf(String str, int fromIndex)  Description: Returns the index of the first occurrence of the substring starting from the specified index. String str = "Hello, World! World!"; int index = str.indexOf("World", 8); System.out.println(index); // Output: 14 10. lastIndexOf(String str)  Description: Returns the index of the last occurrence of the substring. String str = "Hello, World! World!"; int lastIndex = str.lastIndexOf("World"); System.out.println(lastIndex); // Output: 14 11. toUpperCase()  Description: Converts all characters in the string to uppercase. String str = "hello"; String upper = str.toUpperCase(); System.out.println(upper); // Output: "HELLO" 12. toLowerCase()  Description: Converts all characters in the string to lowercase. String str = "HELLO"; String lower = str.toLowerCase(); System.out.println(lower); // Output: "hello" 13. trim()  Description: Removes leading and trailing whitespace. String str = " Hello, World! "; String trimmed = str.trim(); System.out.println(trimmed); // Output: "Hello, World!" 14. replace(char oldChar, char newChar)  Description: Replaces all occurrences of a specified character with a new character. String str = "hello"; String newStr = str.replace('l', 'p'); System.out.println(newStr); // Output: "heppo" 15. replace(CharSequence target, CharSequence replacement)  Description: Replaces all occurrences of a substring with another substring. String str = "Hello, World!"; String newStr = str.replace("World", "Java"); System.out.println(newStr); // Output: "Hello, Java!" 16. split(String regex)  Description: Splits the string into an array based on the given regular expression. String str = "apple,orange,banana"; String[] fruits = str.split(","); for (String fruit : fruits) { System.out.println(fruit); } // Output: // apple // orange // banana 17. split(String regex, int limit)  Description: Splits the string into an array, with a maximum number of substrings. String str = "apple,orange,banana"; String[] fruits = str.split(",", 2); for (String fruit : fruits) { System.out.println(fruit); } // Output: // apple // orange,banana 18. startsWith(String prefix)  Description: Checks if the string starts with the specified prefix. String str = "Hello, World!"; boolean starts = str.startsWith("Hello"); System.out.println(starts); // Output: true 19. endsWith(String suffix)  Description: Checks if the string ends with the specified suffix. String str = "Hello, World!"; boolean ends = str.endsWith("World!"); System.out.println(ends); // Output: true 20. isEmpty()  Description: Checks if the string is empty (length is 0). String str = ""; boolean empty = str.isEmpty(); System.out.println(empty); // Output: true 21. join(CharSequence delimiter, CharSequence... elements)  Description: Joins multiple strings with the specified delimiter. String date = String.join("-", "2024", "10", "20"); System.out.println(date); // Output: "2024-10-20" 22. compareTo(String anotherString)  Description: Compares two strings lexicographically. String str1 = "apple"; String str2 = "banana"; int result = str1.compareTo(str2); System.out.println(result); // Output: negative value (because "apple" < "banana") 23. compareToIgnoreCase(String anotherString)  Description: Compares two strings lexicographically, ignoring case differences. String str1 = "apple"; String str2 = "Banana"; int result = str1.compareToIgnoreCase(str2); System.out.println(result); // Output: negative value 24. valueOf(int i)  Description: Converts a given value (e.g., int, long, float, boolean) to a string. int num = 123; String strValue = String.valueOf(num); System.out.println(strValue); // Output: "123" 25. toCharArray()  Description: Converts the string to a new character array. String str = "Hello"; char[] chars = str.toCharArray(); for (char ch : chars) { System.out.println(ch); } // Output: // H // e // l // l // o JAVA TYPE CASTING Type casting or type conversion, and it can be either implicit (automatic) or explicit (manual). 1. String to Integer (String to int) You can convert a String that represents a number to an int using Integer.parseInt() or Integer.valueOf(). String str = "123"; int num = Integer.parseInt(str); // Using parseInt() int num2 = Integer.valueOf(str); // Using valueOf() System.out.println(num); // Output: 123 2. Integer to String (int to String) You can convert an int to a String using String.valueOf() or Integer.toString(). int num = 123; String str = String.valueOf(num); // Using String.valueOf() String str2 = Integer.toString(num); // Using Integer.toString() System.out.println(str); // Output: "123" 3. String to Double (String to double) Convert a String to a double using Double.parseDouble(). 4. Double to String (double to String) You can convert a double to a String using String.valueOf(). 5. String to Float (String to float) Convert a String to a float using Float.parseFloat(). 6. Float to String (float to String) You can convert a float to a String using String.valueOf(). 7. String to Long (String to long) Convert a String to a long using Long.parseLong(). 8. Long to String (long to String) Convert a long to a String using String.valueOf(). 9. String to Boolean (String to boolean) Convert a String to a boolean using Boolean.parseBoolean(). 10. Boolean to String (boolean to String) Convert a boolean to a String using String.valueOf(). 11. Int to Double (int to double) (Implicit Conversion) An int can be converted to a double automatically by Java because double is a larger data type. int num = 5; double d = num; // Implicit conversion System.out.println(d); // Output: 5.0 12. Double to Int (double to int) (Explicit Conversion) To convert a double to an int, you need to use explicit casting. double d = 5.99; int num = (int) d; // Explicit cast System.out.println(num); // Output: 5 13. Int to Float (int to float) (Implicit Conversion) An int can be converted to a float automatically. 14. Float to Int (float to int) (Explicit Conversion) You can convert a float to an int using explicit casting. 15. Char to Int (char to int) You can convert a char to an int (ASCII value of the character). char c = 'A'; int ascii = (int) c; // ASCII value of 'A' System.out.println(ascii); // Output: 65 16. Int to Char (int to char) Convert an int (ASCII value) to a char. int ascii = 65; char c = (char) ascii; System.out.println(c); // Output: 'A' 17. Char to String (char to String) You can convert a char to a String using Character.toString() or String.valueOf(). char c = 'A'; String str = Character.toString(c); // Using Character.toString() String str2 = String.valueOf(c); // Using String.valueOf() System.out.println(str); // Output: "A" 18. String to Char (String to char) You can extract a single character from a String using charAt(). String str = "Hello"; char c = str.charAt(0); System.out.println(c); // Output: 'H' 19. Int to String (int to String) Using Concatenation You can also convert an int to a String by concatenating with an empty string. int num = 123; String str = num + ""; // Implicit conversion using concatenation System.out.println(str); // Output: "123" 20. String to Char Array (String to char[]) Convert a String to a char[] using toCharArray(). String str = "Hello"; char[] charArray = str.toCharArray(); System.out.println(Arrays.toString(charArray)); // Output: [H, e, l, l, o] 21. Char Array to String (char[] to String) Convert a char[] to a String using the String constructor. char[] charArray = {'H', 'e', 'l', 'l', 'o'}; String str = new String(charArray); System.out.println(str); // Output: "Hello"

Use Quizgecko on...
Browser
Browser