Remove Newline, Space And Tab Characters From A String In Java

  • Home
  • Whiteboard
  • Online Compilers
  • Practice
  • Articles
  • AI Assistant
  • Jobs
  • Tools
  • Corporate Training
  • Courses
  • Certifications
Menu Categories Login
  • Switch theme
  • SQL
  • HTML
  • CSS
  • Javascript
  • Python
  • Java
  • C
  • C++
  • PHP
  • Scala
  • C#
  • Tailwind CSS
  • Node.js
  • MySQL
  • MongoDB
  • PL/SQL
  • Swift
  • Bootstrap
  • R
  • Machine Learning
  • Blockchain
  • Angular
  • React Native
  • Computer Fundamentals
  • Compiler Design
  • Operating System
  • Data Structure and Algorithms
  • Computer Network
  • DBMS
  • Excel
Technical Questions and Answers
  • Data Structure Data Structure
  • Networking Networking
  • RDBMS RDBMS
  • Operating System Operating System
  • Java Java
  • MS Excel MS Excel
  • iOS iOS
  • HTML HTML
  • CSS CSS
  • Android Android
  • Python Python
  • C Programming C Programming
  • C++ C++
  • C# C#
  • MongoDB MongoDB
  • MySQL MySQL
  • Javascript Javascript
  • PHP PHP
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who
Remove newline, space and tab characters from a string in Java JavaObject Oriented ProgrammingProgramming

To remove newline, space and tab characters from a string, replace them with empty as shown below.

replaceAll("[\n\t ]", "");

Above, the new line, tab, and space will get replaced with empty, since we have used replaceAll()

The following is the complete example.

Example

 Live Demo

public class Demo {    public static void main(String[] args) {       String originalStr = "Demo\n\tText";       System.out.println("Original String with tabs, spaces and newline: \n"+originalStr);       originalStr = originalStr.replaceAll("[\n\t ]", "");       System.out.println("\nString after removing tabs, spaces and new line: "+originalStr);    } }

Output

Original String with tabs, spaces and newline: Demo Text String after removing tabs, spaces and new line: DemoText karthikeya Boyini karthikeya Boyini Updated on: 2020-06-27T06:36:38+05:30

18K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started Print Page Previous Next Advertisements

Tag » Arduino String Remove R N