Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Multiple Spaces between words in Java

807580Jul 23 2010 — edited Jul 23 2010
Hi All

String s1 = "boy";
String s2 = "girl";

String s = s1 + "multiple spaces through keyboard spacebar writing this because when this is posted it will not be visible in the browser" + s2;
system.out.println(s);

I need to have multiple spaces between s1 and s2 and need to be shown in the browser(IE) in java.

If it is a html code, I found we can use <pre> tag, &nbsp;

Thanks

Comments

JoachimSauer
HTML (and XML too, depending on a few factors) reduce multiple whitespace characters only to a single space.

If you want to represent multiple spaces, you can use the Unicode character U+00A0 NO-BREAK SPACE, commonly written as "&nbsp;" in HTML. It looks exactly the same as a normal space (U+0020 SPACE), but won't be reduced.
807580
Hi

Can you please give me a sample code on how to use this in a java class?

Thanks
JoachimSauer
JoachimSauer wrote:
commonly written as "&nbsp;"
Sorry, the forum software ate what I wrote here. Actually it is written as "& nbsp;" (but without the space, that I needed to add to keep the forum software from butchering it again).
807580
I used it in the java class but it is displaying as it is in the web page and space is not displayed.

s1 = "nbsp" + "nbsp" + "nbsp"; //both ampersend and semicolon given
s2 = "Test"
s3 = s1 + s2;

Thanks
JoachimSauer
kumar11 wrote:
I used it in the java class but it is displaying as it is in the web page and space is not displayed.
Then you are using some tag that does HTML escaping for you to print it.
807580
s1 = "nbsp" plus-sign "nbsp" plus-sign "nbsp";
JoachimSauer
kumar11 wrote:
s1 = "nbsp" plus-sign "nbsp" plus-sign "nbsp";
Learn to use the code tag ;-) Select your code, and push "CODE" just above the text area.
807580
ok thanks
807580
No I'm not using any tags in the class.
807580
JoachimSauer wrote:
JoachimSauer wrote:
commonly written as "&nbsp;"
Sorry, the forum software ate what I wrote here. Actually it is written as "& nbsp;" (but without the space, that I needed to add to keep the forum software from butchering it again).
You can display a "raw" entity by escaping the ampersand:
&#38;#38;nbsp;
I use &#38;#38; instead of &#38;amp; because &#38;amp; doesn't work inside CODE blocks, and &#38;#38; does.

Of course, all this only applies until the next site upgrade, when they break half the existing features in the process of adding a bunch of new features that nobody wanted. :P
807580
We can also use add space between objects by using CSS (e.g. margin, padding) apart from nbsp. Can anyone let me know how to use it with sample code?

Thanks
dadams07
You may want to consider posting this in an HTML forum. If I understand the objective correctly, it's a browser display issue, unrelated to Java
1 - 12
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Aug 20 2010
Added on Jul 23 2010
12 comments
705 views