Solved

Java - Insrting double quotes into a string

Posted on 2011-09-29
10
287 Views
Last Modified: 2012-08-13
I have a java String variable, and I want to concatenate double quotes to the beginning and end of the string like this:

String x = "SomeValue";

x = """" + x + """";

That is what I have tried, but it does not work.  When I am finished, the value of x should no longer be the literal string SomeValue, but SomeValue enclosed in double quotes.
0
Comment
Question by:jbaird123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
10 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 36818692
String s = "abcd \" hjjh + \"" ;
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36818694
You ned to escape double qutes with backslash in front of it
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 500 total points
ID: 36818696

String x = "SomeValue";

x = "\"" + x + "\"";
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 47

Expert Comment

by:for_yan
ID: 36818700
You can manipulate with double quotes in your strings if you add backslash immeditaely before the double quot - then
compile will not considet this double quote as a special symbol delimiting the string.
0
 
LVL 47

Accepted Solution

by:
for_yan earned 500 total points
ID: 36818705
This will look better:

String x = "SomeValue";

x = "\"" + x + "\""; 

Open in new window

0
 
LVL 47

Expert Comment

by:for_yan
ID: 36818710
This is how it executes it in the test:

        String x = "SomeValue";

x = "\"" + x + "\"";

        System.out.println(x);

        

Open in new window


Output:

"SomeValue"

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36818713
Clearer and more efficient:
s = new StringBuilder().append('"').append(s).append('"').toString();

Open in new window

0
 

Author Closing Comment

by:jbaird123
ID: 36818721
Perfect!  Thanks!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36818729
??
0
 

Author Comment

by:jbaird123
ID: 36818736
Sorry, CEHJ,

I saw your comments after I awarded points.  I guess I was trying the solution and awarding points while you were submitting your answer.

The comment above was addressed to "for_yan".

0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question