- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to remove the leading and trailing quotes from a string
Firstly, let us consider a string with quotes
String originalStr = "\"Demo Text\"";
Now, consider the following logic for the beginning quote.
if (originalStr.startsWith("\"")) { originalStr = originalStr.substring(1, originalStr.length()); }
Now, consider the following logic for the ending quote.
if (originalStr.endsWith("\"")) { originalStr = originalStr.substring(0, originalStr.length() - 1); }
Example
public class Demo { public static void main(String[] args) { String originalStr = "\"Demo Text\""; System.out.println("String with double quotes= "+originalStr); if (originalStr.startsWith("\"")) { originalStr = originalStr.substring(1, originalStr.length()); } if (originalStr.endsWith("\"")) { originalStr = originalStr.substring(0, originalStr.length() - 1); } System.out.println("String after removing double quotes = "+originalStr); } }
Output
String with double quotes= "Demo Text" String after removing double quotes = Demo Text
Advertisements