I have a programming homework. It says that I need to reverse the string first, then change it to uppercase and then remove all the whitespaces. I actually did it, but our professor didn't say anything about using replaceAll()
method. Is there any other way to do it beside replaceAll()
?
Here is my code:
public static void main(String[] args) {
String line = "the quick brown fox";
String reverse = "";
for (int i = line.length() - 1; i >= 0; i--) {
reverse = reverse + line.charAt(i);
}
System.out.println(reverse.toUpperCase().replaceAll("\\s", ""));
}
Use the String. replace() method to remove all whitespace from a string, e.g. str. replace(/\s/g, '') . The replace() method will remove all whitespace characters by replacing them with an empty string.
In the above program, we use String's replaceAll() method to remove and replace all whitespaces in the string sentence . To learn more, visit Java String replaceAll(). We've used regular expression \\s that finds all white space characters (tabs, spaces, new line character, etc.) in the string.
The replaceAll() method of the String class replaces each substring of this string that matches the given regular expression with the given replacement. You can remove white spaces from a string by replacing " " with "".
Use erase-remove Idiom to Remove Spaces From String in C++ One of the most useful methods for range manipulation in C++ is the erase-remove idiom which consists of two functions - std::erase (built-in function for the most STL containers) and std::remove (the part of the STL algorithms library).
To remove all whitespace characters from the string, and not just the space character, use \s instead. The \s matches against all newline characters, tab characters, space characters, etc., This would translate to a simple code below: 2. Using Split () with Join () method
Using Split () with Join () method Another approach is to split the string using whitespace as a delimiter and then join it back together with an empty string. The following example demonstrates this by removing all contiguous space characters from the string. To remove all whitespace characters from the string, use \s instead.
In the above example, First, we initialize the string by giving the tabs ( ) at both sides of the string. In code line 5, 6, and 7, we use the lstrip (), rstrip (), and strip () method to remove the spaces at the left side that is leading whitespace, at the right side that is trailing whitespace, and spaces at both sides respectively.
In addition to using replace, on SQL Server 2017+ to avoid multiple nested functions, you can use translate if there are multiple characters in a string you want to remove: declare @string varchar (50)='12345 67-9\x' select Replace (Translate (@string, ' -\','???'),'?','') You can refer to Remove all spaces from a string in SQL Server.
You can check each character in turn using Character.isWhitespace
. Additionally, it is generally better to use a StringBuilder
when concatenating inside a loop.
public static void main(String[] args) {
String line = "the quick brown fox";
StringBuilder sb = new StringBuilder(line.length());
for (int i = line.length() - 1; i >= 0; i--) {
char c = line.charAt(i);
if(!Character.isWhitespace(c)) sb.append(Character.toUpperCase(c));
}
System.out.println(sb);
}
@Khelwood's answer as code:
public static void main(String[] args) {
String line = "the quick brown fox";
String reverse = "";
for (int i = line.length() - 1; i >= 0; i--) {
char currentChar = line.charAt(i);
if (currentChar != ' ') {
reverse += currentChar;
}
}
System.out.println(reverse.toUpperCase());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With