Solved

Java - Insrting double quotes into a string

Posted on 2011-09-29
10
284 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 71
Unable to start eclipse ? 17 128
Java Jpanels and Jframe 8 28
maven project error 5 47
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 …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now