Preserve linebreaks from text to html

Hi,

I am using a JTextArea to allow the user to type in text, which is then displayed alongwith some other text in a JEditorPane.

The content type for the JEditorPane is text/html

The content is displayed with all the formatting that I am applying, but the linebreaks entered by the user are lost.

Anyway that I can preserve these? I cant use String.replace() because that only takes chars, not Strings. Else I might have tried to replace "\n" with "
"

Any ideas?...

[548 byte] By [dewangs] at [2007-9-26 2:37:25]
# 1

use this string replace method.

public static String replaceString( String source, String replaceThis, String withThis, boolean all ) {

StringBuffer result = new StringBuffer();

int length = source.length();

int lengthThis = replaceThis.length();

int index, lastIndex = 0;

do {

index = source.indexOf( replaceThis, lastIndex );

if( index < 0 ) break;

result.append( source.substring( lastIndex, index ) );

result.append( withThis );

lastIndex = index + lengthThis;

} while( all );

if( lastIndex < length ) {

result.append( source.substring( lastIndex ) );

}

return new String( result );

}

parthasarkar at 2007-6-29 10:06:43 > top of Java-index,Archived Forums,Java Programming...
# 2

well, you can try this code but you should debug it first ...

String result = ....;

do

{

int i = result.indexOf( '\n' );

if( i != -1 )

{

String t1 = result.substring( 0, i );

String t2 = result.substring( i+1 );

result = t1 + "
" + t2;

}

} while( i != -1 );

// use result ...

Better suggestions ?

If you are filtering very long strings with plenty of newLines, you could consider using StringBuffer.

igarn at 2007-6-29 10:06:43 > top of Java-index,Archived Forums,Java Programming...
# 3

Very similar to other posts:

StringTokenizer st = new StringTokenizer(yourString, "\n".intern());

int resultLen = yourString.length() + (st.countTokens() * 3); //
- \n

StringBuffer sb = new StringBuffer(resultLen);

sb.append(st.nextToken());

while (st.hasMoreTokens()) {

sb.append("
".intern());

sb.append(st.nextToken());

}

String result = sb.toString();

Here I tried to be as carefull as I can with the use of Strings, so I've used the intern() method and StringBuffer.

U063667 at 2007-6-29 10:06:43 > top of Java-index,Archived Forums,Java Programming...
# 4

[nobr]hey, thanks!

I sat to think about it and came up with pretty much the same thing myself!

public static String htmlFormatLineBreaks(String text)

{

StringTokenizer st = new StringTokenizer(text,"\n");

StringBuffer buf = new StringBuffer(text.length());

if(st.countTokens() == 1)

{

return text;

}

else

{

int ct = 0;

while(st.hasMoreTokens())

{

if(ct > 0)

{

buf.append("<BR>");

}

buf.append(st.nextToken());

ct++;

}

return buf.toString();

}

}

U063667,

Your code does include the length of the BR in the StringBuffer size, I hadnt added that...

What exactly is the string.intern() ? I have not used that before.[/nobr]

dewangs at 2007-6-29 10:06:43 > top of Java-index,Archived Forums,Java Programming...
# 5
intern() is a String's method to get the canonical representation of a String.The String class keeps a pool of strings that you can reuse, saving time and memory. One thing not to forget is that object creation in Java is a very expensive task.
U063667 at 2007-6-29 10:06:43 > top of Java-index,Archived Forums,Java Programming...