Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any other way to remove all whitespaces in a string?

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", ""));
}
like image 954
NEMES1S Avatar asked Dec 19 '20 21:12

NEMES1S


People also ask

How do you get rid of all whitespace in a string?

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.

Which string method would you use to remove all whitespaces from a 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.

How do I get rid of whitespaces?

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 "".

How do I remove all whitespaces from a string in C++?

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).

How to remove all whitespace characters from a string in JavaScript?

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

How do you split a string with whitespace in Python?

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.

How to remove spaces at the left side of a string?

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.

How to remove all spaces from a string in SQL Server?

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.


Video Answer


2 Answers

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);
}
like image 98
Unmitigated Avatar answered Sep 30 '22 22:09

Unmitigated


@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());

}
like image 29
Michael Chatiskatzi Avatar answered Sep 30 '22 20:09

Michael Chatiskatzi