Solved

Java - Insrting double quotes into a string

Posted on 2011-09-29
10
286 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
V.simple question about Android packages and libraries 1 30
hibernate jars 4 45
difference between sorce folder and folder in eclipise 3 29
CSV file parsing thru Java 13 34
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

820 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