Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading