Java String Manipulation Methods
Java String Manipulation Methods
The program uses a for-loop to iterate through the string, utilizing 'Character.isUpperCase()', 'Character.isLowerCase()', and 'Character.isDigit()' to classify each character, incrementing respective counters. Such classification is crucial for tasks needing validation and formatting, like case-sensitive credentials or numeric-only input fields .
The program iterates over each character of the string, checking if it is a digit using the 'Character.isDigit()' method. It increments a counter for each digit found in the string, effectively counting the total number of digits present .
The program counts spaces in the string, using this count as the baseline for word separation. By iterating through the string and incrementing a space counter, it calculates the number of words as 'space count + 1'. While generally reliable, this method assumes well-formed input with words properly separated by spaces, which might not handle consecutive spaces accurately .
The 'convertCase' method takes two string inputs. For the first string, 'obj1', it converts all characters to lowercase using the 'toLowerCase()' method. For the second string, 'obj2', it converts all characters to uppercase using the 'toUpperCase()' method. The results display the lowercase version of 'obj1' and the uppercase version of 'obj2' .
The logic involves locating the first digit of the string by using 'charAt(0)' and replacing it with '0' using the 'replace' method. This could be useful for scenarios like standardizing formats where only the first numeric occurrence needs alteration, such as product codes or identifiers that conform to a specific template .
The method described reads a string from the console and targets the sixth character (index 5, considering zero-based indexing) of the string. This character is identified using 'charAt(5)' and is replaced with the digit '9' using the 'replace' method. The choice of the sixth character seems arbitrary and is likely meant to demonstrate the replace functionality for a specific character index .
The method converts the string to uppercase for uniform comparison, then iterates through each character, checking for vowels using 'if' conditions (A, E, I, O, U). Characters not matching vowels but within 'A' to 'Z' are counted as consonants. This approach efficiently counts both vowels and consonants, leveraging simple checks for uppercase letters .
The method calculates the length of the input string and iterates through it with a for-loop. For each character at index 'i', the character is printed along with its ASCII integer value, which is obtained by casting the character to an 'int', using '(int)ch' .
The program first compares the first and last characters of the string using 'charAt(0)' and 'charAt(len-1)'. If they match, it proceeds to reverse the string using a for-loop that starts from the end and appends each character to a new string, 'Rev'. If the characters do not match, it outputs a message stating the mismatch .
The program concatenates two strings entered by the user through a user-defined method 'join'. It first appends a space to the first string using 'concat(" ")' and then concatenates the second string to it, resulting in a single merged string output .