Solved

Java - Insrting double quotes into a string

Posted on 2011-09-29
10
283 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 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 …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

743 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

10 Experts available now in Live!

Get 1:1 Help Now