How to convert "[WARNING]: \t Information \r\n" To "Information" using Stringsplit()?

I want split the message in such a way that I can get just the information from the received line

Received : [WARNING]: \t Information \r\n

Expected : Information

public static String message(String logLine) {
        String[] str = logLine.split(" ");
        return str[1];
}

>Solution :

This code splits the log line by spaces, and returns the second element in the resulting array. However, it does not address the \t and \r\n characters in the log line. To handle these characters and get the expected output, you can modify the code as follows:

public static String message(String logLine) {
        String[] str = logLine.split(":");
        return str[1].trim().replaceAll("\t", "").replaceAll("\r\n", "");
}

This code splits the log line by colon (:), and trims the leading and trailing white spaces of the second element in the resulting array. Then, it replaces the tab and newline characters with an empty string, so you can get the expected output.

Leave a Reply