Skip to Content
0
Former Member
Jul 11, 2012 at 06:31 AM

Issue with splitting string into multiple lines

22 Views

Hi Experts,

I have a long string s. I want to split this string into several lines each having 72 characters. For this I have done following programming:

String s = "For the first time it includes a supplementary report attempting to determine if extreme weather events can be linked to human-induced climate change. The research team, including members of the UK Met Office, identified recent episodes of extreme weather then used a computer to estimate the likelihood of the episode happening in a world without increased CO2." ;

s = s.replaceAll("[\r\n]+", " ");

char[] sAr = s.toCharArray();

AbstractList RecepitsList7 = new Bapitrtext.Bapitrtext_List();;

int start = 0; // start with

int i = 72 ;

// for (int i = 71; i < sAr.length; i++) {

while (i < sAr.length) {

{

Bapitrtext Recepits7 = new Bapitrtext();

Recepits7.setTextid("TEXT");

Recepits7.setTextline(s.substring(start, i).replaceAll("[\r\n]+", ""));

RecepitsList7.add(Recepits7);

start = i+1; i += 72;

} } input.setText(RecepitsList7);

The output of program is as below:

For the first time it includes a supplementary report attempting to dete

mine if extreme weather events can be linked to human-induced climate

hange. The research team, including members of the UK Met Office, iden

ified recent episodes of extreme weather then used a computer to estim

te the likelihood of the episode happening in a world without increased

Here as we can see last characters are getting deleted by program.

For example in first line determine 'r' is missing in extreme right.

In third line c is missing in extreme left (word change is needed.

Program prints hange. In 3rd line t is missing in extreme right

In 4th line, a is missing in extreme right.

Also the last line is not being printed.

In last line "CO2." " should appear.

There are two issues:

1. Why last word of each sentence is not being printed?

2. Why last sentence is not being printed?

I am struggling since last 2 days. Please help.

I made all changes like replacing 72 by 71 etc. By making such changes, I am able to print first line correctly.

However under all cases last character of 2nd line onward is not getting displayed.

Also last line is not getting displayed. I did every thing like using while instead of for loop etc. Kindly help.

Regards, Gary