?
Solved

Java - Insrting double quotes into a string

Posted on 2011-09-29
10
Medium Priority
?
292 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 2000 total points
ID: 36818696

String x = "SomeValue";

x = "\"" + x + "\"";
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month10 days, 14 hours left to enroll

770 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