chopping of string

G

gk

gk, 04.10.2007 15:00:






Why not simply tell the browser to wrap the content of the table cell?
That would be the CSS attribute: white-space:normal. Usually the
browsers do that be default unless there are no (white)spaces in the
retrieved data.

Thomas

I dont think , this might work ....does it work in firefox too ? may
be it will work in IE but not other browsers.

I surfed net and found the best thing is to do it in the server
side ...from server side , it needs to be modified .
 
G

gk

gk said:
gk wrote:
i need a method which will take a string , if the string has more than
30 chars then it will chop that big string into chunk of 30 chars
seperated by <br> tag in them.
how to do it ?
A_very_big_string will be midified into string_30_chars
+"<br>"+string_30_chars+"<br>"+string_30_chars+"<br>"+remaining
and the final modified string should be returned
Are you /sure/ that's the spec you want?
Consider
((((
Software development is an exciting but messy business.
))))
where the thirty-character axe will [1] slice this into
((((
Software development is an exc<br>iting but messy business.
))))
which will display as something like
((((
Software development is an exc
iting but messy business.
))))
I'd rather my words weren't ch
opped around like that, but yo
ur use-case may differ.
[1] If I can count.
I have no other choice but to do that way .

You have /several/ other choices, starting ...
because , my jsp table's column is getting stretched from the DB's
long text.

... with not worrying about it, and continuing with setting a
width on the table column and leaving the chopping to the
renderer.



No , thats does not work.....browser cant render it. I have already
tried with a fixed column width =25%.....this column is getting
stretched automatically when big texts are coming up

so, i want to make it formatted before putting into the table.
i would like to add <br> tags so that in the final html , it will
wrapped and will give newlines.
so, i need that method.

That method chops words into parts. I can't imagine [1] that this
is a good thing, and it could produce misleading text.
do you know any other easy alternative ?

Chop the string at spaces (.split) and then reassemble lines
from the components until they'd be too long. Remember to
cater for word(-like thing)s that are 30+ characters long.
Maybe a little more code, but less chance of mangling text.

--
Chris "carve thee not" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
 
A

Andrew Thompson

i need a method ..

'No you don't'. What is important to remember
here, is the *goal*. The goal might be stated
as: "I need a way to present long strings to the
user in HTML, in a way that makes sense to them."

For the latter, try this..

<html>
<body>
<form>
<textarea cols=30 rows=5>
I need a method which will take a string , if
the string has more than 30 chars then it will
chop that big string into chunk of 30 chars.
</textarea>
</form>
</body>
</html>

...validation and refinement, left to the OP.

Andrew T.
 
R

Roedy Green

<textarea cols=30 rows=5>
I need a method which will take a string , if
the string has more than 30 chars then it will
chop that big string into chunk of 30 chars.
</textarea>

Just what CSS and HTML is supported in Java's rendering engine?
 
A

Andrew Thompson

Roedy said:
Just what CSS and HTML is supported in Java's rendering engine?

That is not relevant to the OP's immediate question,
since they were (ultimately) referring to delivering text
to a JSP, or to my reading of it 'HTML in a browser'.

OTOH, Java built in support for HTML includes coverage
of HTML 3.2 elements (everything shown above) as well
as a surprising amount of CSS (more than understood
by your average Java programmer).

To get a basic idea of how a page will look, though, I
find it easiest to give the URL to a JEditorPane via the
constructor, then toss the JEP into a JScrollPane and
show it in a JOptionPane.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1
 
T

Thomas Kellerer

gk, 05.10.2007 04:58:
I dont think , this might work ....does it work in firefox too ? may
be it will work in IE but not other browsers.

I surfed net and found the best thing is to do it in the server
side ...from server side , it needs to be modified .

This is standard HTML and works in Firefox and IE. But as I said, in
order for the browser to be able to wrap the text it needs whitespaces.
So if your data does not contain whitespaces (or some words are actually
longer that the table cell) then it won't work.

Thomas
 
R

Roedy Green

That is not relevant to the OP's immediate question,
since they were (ultimately) referring to delivering text
to a JSP, or to my reading of it 'HTML in a browser'.

Right. I was conflating this question with another where the guy was
trying to print HTML using Java's rendering engine.
 
R

Roedy Green

To get a basic idea of how a page will look, though, I
find it easiest to give the URL to a JEditorPane via the
constructor, then toss the JEP into a JScrollPane and
show it in a JOptionPane.

I wrote a snippet to display an HTML URL in Java then pointed it to my
home page. The rendering is hopeless. It was not even smart enough to
ignore comments.

see http://mindprod.com/jgloss/htmlrendering.html
 
A

Andrew Thompson

Roedy said:
..
..I was conflating this question with another where the guy was
trying to print HTML using Java's rendering engine.

You might try experimenting with this JWS based example.
<http://www.physci.org/jws/#prs>
I have not been able to do any testing with it for lack of
a printer.

Unfortunately it is File(Service) and JTextArea based
rather than URL and JEditorPane, as I was suggesting
earlier, but it should not take much to adapt the GUI.

The build files can be downloaded at the anchor,
specifically
<http://www.physci.org/jws/printservice.zip>
..and launched in a sandbox by..
<http://www.physci.org/jws/printtest.jnlp>

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1
 
D

Dag Sunde

gk said:
No , thats does not work.....browser cant render it. I have already
tried with a fixed column width =25%.....this column is getting
stretched automatically when big texts are coming up

Then either your HTML or CSS is wrong, because that is perfectly doable,
and will work in "all" browsers when done right.

You might ask how to do it in a html and/or CSS group, or if it is a *very*
short sample, post an URL, so we can take a look at your HTML/CSS...
 
A

Andrew Thompson

C

Chris ( Val )

I think Chris is pointing out that the test is "<",
not "<=". (Consider the case where the length of
the input string is 1.)

Yes, that is correct.

I have a good grounding in C++, and every time I
see that kind of thing it raises alarm bells :)

Given that arrays are zero based, there is almost
never a need to use the '<=' operator for the
conditional expression either :)

Cheers,
Chris
 
G

gk

Thomas said:
gk, 05.10.2007 04:58:

This is standard HTML and works in Firefox and IE. But as I said, in
order for the browser to be able to wrap the text it needs whitespaces.
So if your data does not contain whitespaces (or some words are actually
longer that the table cell) then it won't work.


why do you mean by "...some words are actually longer that the table
cell..." ???

I have a column of width 25% where as total table width=100%.
Now, if i get a long word from DB say with 1500 chars , will it be
wrapped ? or it will continue the long same line ?

In this case , i want it wrapped and i also dont like to use
 
L

Lew

gk said:
In this case , i want it wrapped and i also dont like to use
<textarea> with annoying scrollbar.

What's really annoying is when text is larger than the display area and there
isn't a scroll bar.
 
A

Andrew Thompson

gk said:
gk, 05.10.2007 04:58:
[quoted text clipped - 14 lines]
So if your data does not contain whitespaces (or some words are actually
longer that the table cell) then it won't work.

why do you mean by "...some words are actually longer that the table
cell..." ???

If 25% of the screen is this long
' ', that is the table cell width, but if the word is..
SuperCalaFragilisticExpyAladocious
..long, it will be a word that is 'longer that the table cell'.

Ultimately though, it sounds better not to set table
width or cell widths as %, pixels, colspan or anything
else, dump the text directly to the cells, and let the
browser render it as it sees fit. After all, browser
manufacturers put a lot of effort into getting marked-up
content to display in a way that is both visually pleasing,
and logical, to the end user.

Of course, I can almost guess that now you will jump
in with how you want fixed /height/ rows..

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1
 
T

Thomas Kellerer

Andrew Thompson wrote on 05.10.2007 18:44:
Ultimately though, it sounds better not to set table
width or cell widths as %, pixels, colspan or anything
else,
Unfortunately it seems that web designer seem to more and more see the browser
(and thus HTML) as a kind of GUI tool where you can create layout that are
optimized to the pixel (we want exactly 2 pixel border between each element, the
table should start 53pixels off the left margin and 62 pixels down from the top,
etc...)

Thomas
 
A

Andrew Thompson

Thomas said:
Andrew Thompson wrote on 05.10.2007 18:44:
Unfortunately it seems that web designer seem to more and more see the browser
(and thus HTML) as a kind of GUI tool where you can create layout that are
optimized to the pixel (we want exactly 2 pixel border between each element, the
table should start 53pixels off the left margin and 62 pixels down from the top,
etc...)

I agree. HTML is not WYSIWYG, it is WUGIWUW*,
and for good reason. It's all about the content - layout
and styles be damned and be cast out - if they get
between me and the information.

* What User Gets, Is What User Wants.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1
 
R

Roedy Green

Yes, I note they are visible, but had you validated the
page recently? One thing Java HTML rendering is
*extremely* bad at, is dealing with malformed or invalid
mark-up.
Yes. I am quite fanatical about validation. Style sheets get
validated by two different programs, and HTML markup via
HTMLValidator.
The fool JEditorPane is rendering all comments as text.
 
A

Andrew Thompson

Roedy said:
Yes. I am quite fanatical about validation.

The 2nd link (to the validator) identifed 8 validation errors
at the moment I checked it.
..Style sheets get
validated by two different programs, and HTML markup via
HTMLValidator.
The fool JEditorPane is rendering all comments as text.

This example* does /not/ render HTML comments.
'Do you see me?' Can you produce an SSCCE** that
supports your assertion?

* Admittedly, it uses a JLabel instead of a JEditorPane,
but they use the same rendering engine.

** With HTML included, as opposed to coming off any
potentially changeable source, such as a web page.

<sscce>
import javax.swing.*;

/** Do comments in well formed HTML get rendered by
Java's inbuilt rendering engine? This test, when run
on Win XP Pro, suppresses the comment text from
appearing. I.E. It is correctly rendered. */
class CheckHTMLRender {

static String message =
"<HTML>" +
"<BODY>" +
"<H1>" +
"Test HTML comments" +
"</H1>" +
"<!-- Do you see me? -->" +
"<p>The comment above should not be visible." +
"</BODY>" +
"</HTML>";

public static void main(String[] args) {
JLabel output = new JLabel(message);
JOptionPane.showMessageDialog(null, output);
}
}
</sscce>

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top