chopping of string

G

gk

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
 
R

Roedy Green

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.

You need a loop, a StringBuilder and the substring method.

Is this homework?

For a production system, you would want to insert your <BR> at word
boundaries.
 
C

Chris Dollin

gk said:
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.
 
G

gk

gk said:
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.

--
Chris "careful with that axe, gk" Dollin

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



I have no other choice but to do that way .

because , my jsp table's column is getting stretched from the DB's
long text.

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.

do you know any other easy alternative ?
 
T

Thomas Kellerer

gk, 04.10.2007 15:00:
I have no other choice but to do that way .

because , my jsp table's column is getting stretched from the DB's
long text.

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.

do you know any other easy alternative ?

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
 
C

Chris Dollin

gk said:
gk said:
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.
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.
 
B

Brian

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

A simple suggestion:

public String chopString(String s) {
String tmp = "";
for (int i = 0; i < s.length() - 1; i++) {
if (tmp.length() % 30 == 0)
tmp += "<br>";
tmp += s.substring(i, i + 1);
}
return tmp;
}

Hope it is usefull :)

/Brian
 
C

Chris ( Val )

A simple suggestion:

public String chopString(String s) {
String tmp = "";
for (int i = 0; i < s.length() - 1; i++) {

What is the reason for the '-1' placed after
the length() call?
 
S

Steve Wampler

Brian said:
A simple suggestion:

If splitting on word boundaries isn't an issue (and Chris Dollin's
solution is a better approach for that problem) then there's no
need to look at every character:

public String chopString(String s, int blkSize) {
String ns = "";
while (s.length() > blkSize) {
ns += s.substring(0,blkSize)+"<br>";
s = s.substring(blkSize);
}
ns += s;
return ns
}
 
B

Brian

If splitting on word boundaries isn't an issue (and Chris Dollin's
solution is a better approach for that problem) then there's no
need to look at every character:

public String chopString(String s, int blkSize) {
String ns = "";
while (s.length() > blkSize) {
ns += s.substring(0,blkSize)+"<br>";
s = s.substring(blkSize);
}
ns += s;
return ns
}

Agree - this is a much better aproach than mine :)

/Brian
 
S

Steve Wampler

Brian said:
The first char is placed at index(0) and the last char at
index(length-1)

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

Patricia Shanahan

Brian said:
The first char is placed at index(0) and the last char at
index(length-1)

/Brian

so there should be an iteration with i equal to length-1, but the
combination of the "<" test and the "-1" ensures that the maximum value
of i for any iteration is length-2.

Patricia
 
J

Joshua Cranmer

Steve said:
If splitting on word boundaries isn't an issue (and Chris Dollin's
solution is a better approach for that problem) then there's no
need to look at every character:

public String chopString(String s, int blkSize) {
String ns = "";
while (s.length() > blkSize) {
ns += s.substring(0,blkSize)+"<br>";
s = s.substring(blkSize);
}
ns += s;
return ns
}

Repeatedly appending Strings together can have a /huge/ performance hit.
I managed to speed one of my programs up by 7x when the String
concatenation was the limiting factor. Better code:

StringBuilder ns = new StringBuilder();
while (s.length() > blkSize) {
ns.append(s.substring(0,blkSize)).append("<br>");
s = s.substring(blkSize);
}
ns.append(s);
return ns;
 
P

Piotr Kobzda

Chris said:
gk wrote:

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.

More advanced chopping algorithms may utilize locale dependent
java.text.BreakIterator instead of String splitting. In the OP's
scenario the line breaking iterator (i.e. created using
BreakIterator.getLineInstance()), which will take care of proper
handling of punctuation and hyphenated words, seems to be the right choice.


piotr
 
S

Steve Wampler

Joshua said:
Repeatedly appending Strings together can have a /huge/ performance hit.
I managed to speed one of my programs up by 7x when the String
concatenation was the limiting factor. Better code:

StringBuilder ns = new StringBuilder();
while (s.length() > blkSize) {
ns.append(s.substring(0,blkSize)).append("<br>");
s = s.substring(blkSize);
}
ns.append(s);
return ns;

Very good point. My baby duck syndrome is showing.
 
R

Roedy Green

public String chopString(String s) {
String tmp = "";
for (int i = 0; i < s.length() - 1; i++) {
if (tmp.length() % 30 == 0)
tmp += "<br>";
tmp += s.substring(i, i + 1);
}
return tmp;
}

Here is another implementation:


public class BreakString
{

/**
* break up a String into lines, and separate them with <br>,
* breaking into lineLength chunks, ignoring word boundaries.
* This code is optimised for speed by attempting to keep the inner
loop
* as tight as possible, at the expense of some extra setup work.
* @param s string to break
* @param lineLength length of desired lines (not counting the
<br>).
* @return string with <br> separators inserted.
*/
public static String breakString(String s, int lineLength)
{
final int stringLength = s.length();

if ( stringLength <= lineLength )
{
return s;
}

// count of one or more lines, including partial lines.
// Last line may be full or partial.
final int lines = (stringLength + lineLength - 1 ) / lineLength;

// compute space needed for original string
// plus a <br> on all but the last line.
final StringBuilder sb =
new StringBuilder(stringLength
+ ( "<br>\n".length()
* ( lines-1 ) ) );

// do all but the last line, possibly 0 lines.
final int startOfLastLine = (lines-1) * lineLength;
for ( int i=0; i < startOfLastLine; i+= lineLength )
{
// copy over one complete line.
sb.append( s.substring( i, i+lineLength ) );
sb.append( "<br>\n" );
}

// copy over the last line, full or partial without <br>
sb.append( s.substring( startOfLastLine, stringLength ) );

return sb.toString();
}

/**
* main, test driver
* @param args not used.
*/
public static void main ( String[] args )
{
System.out.println(breakString("123456789012345678901234567",
3));
System.out.println(breakString("123456789012345678901234567890",
30));
System.out.println(breakString("1234567890123456789012345678901", 30
));

System.out.println(breakString("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123",
30) );
}
}

}
 
R

Roedy Green

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.

A word processor would consider two other things in the algorithm:
1. word breaks
2. hyphenation for long words.

For a production system, you might look for a hyphenation word-wrap
package to do this properly.
 
L

Lew

Roedy said:
does thing copy the whole string each time?

Typical implementation is to point s to the new location in the
already-allocated buffer, so not usually, no.

No guarantees.
 

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,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top