public static String convertNewLines ( String string ) { int index ; // Replace with \n StringBuffer newStringBuffer = new StringBuffer ( ) ; while ( ( index = string . indexOf ( ' ' ) ) != - 1 ) { if ( index > 0 ) { newStringBuffer . append ( string . substring ( 0 , index ) ) ; } newStringBuffer . append ( ' ' ) ; newStringBuffer . append ( ' ' ) ; if ( ( index + 1 ) < string . length ( ) ) { string = string . substring ( index + 1 ) ; } else { string = "" ; } } newStringBuffer . append ( string ) ; string = newStringBuffer . toString ( ) ; // Replace with \r newStringBuffer = new StringBuffer ( ) ; while ( ( index = string . indexOf ( ' ' ) ) != - 1 ) { if ( index > 0 ) { newStringBuffer . append ( string . substring ( 0 , index ) ) ; } newStringBuffer . append ( ' ' ) ; newStringBuffer . append ( ' ' ) ; if ( ( index + 1 ) < string . length ( ) ) { string = string . substring ( index + 1 ) ; } else { string = "" ; } } newStringBuffer . append ( string ) ; return newStringBuffer . toString ( ) ; }
Converts carriage returns and new lines in a string into \ r and \ n .