function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Walter@AdicioWalter@Adicio 

trying to split a long string that has no spaces in it

I have a list in a page with an outputField and I notice if someone saves a long string with no spaces, most common is a URL, the string will not wrap and it breaks the page width.

 

 

If this string below was found anywhere in the field, is it possible to break that string apart at every X number of characters?

 

 

"this_is_a_long_line_with_no_line_breaks_it_should_be_split_at_every_172_characters_in_order_to_fit_into_the_page_with_out_breaking_the_page_width_repeat_this_is_a_long_line_with_no_line_breaks_it_should_be_split_at_every_172_characters_in_order_to_fit_into_the_page_with_out_breaking_the_page_width"

 

Where do I start? Would I make a string that is the X number of characters and get the size of that string then parse through the field for any strings with a length that matches my string to find what I want to break apart?

 

Then devide the found string by the length of my string for the number of times I need to loop through and split the found string with a space at every X number of characters?

Best Answer chosen by Admin (Salesforce Developers) 
boBNunnyboBNunny

Something like below should work:

 

String strInput = 'this_is_a_long_line_with_no_line_breaks_it_should_be_split_at_every_172_characters_in_order_to_fit_into_the_page_with_out_breaking_the_page_width_repeat_this_is_a_long_line_with_no_line_breaks_it_should_be_split_at_every_172_characters_in_order_to_fit_into_the_page_with_out_breaking_the_page_width';

String [] strParse;

If (strInput == null) {
 strParse.add '';
} else {
 // Add them in 172 character blocks
 do {
  strParse.add strInput.subString(0, 172);  
  strInput = strInput.subString(172, strInput.length() - 172);
 } while (strInput.length > 172);
 strParse.add strInput;  
}

All Answers

boBNunnyboBNunny

Something like below should work:

 

String strInput = 'this_is_a_long_line_with_no_line_breaks_it_should_be_split_at_every_172_characters_in_order_to_fit_into_the_page_with_out_breaking_the_page_width_repeat_this_is_a_long_line_with_no_line_breaks_it_should_be_split_at_every_172_characters_in_order_to_fit_into_the_page_with_out_breaking_the_page_width';

String [] strParse;

If (strInput == null) {
 strParse.add '';
} else {
 // Add them in 172 character blocks
 do {
  strParse.add strInput.subString(0, 172);  
  strInput = strInput.subString(172, strInput.length() - 172);
 } while (strInput.length > 172);
 strParse.add strInput;  
}

This was selected as the best answer
nagalakshminagalakshmi

Hi,

 

I have a string, i need to split that string by 70 chars.  I did with do while. Like

 

String s = 'The Sales Cloud includes a real-time sales collaborative tool called Chatter , provides sales representatives with a customer profile and account history, allows the user to manage marketing campaign spending and performance across a variety of channels from a single application, tracks opportunity-related data including milestones, decision makers, customer communications, and other information unique to the companys sales process. Automatic email reminders can be scheduled to keep teams up to date.';
integer l = s.length();
system.debug(l+'l==');
string ss;
integer i=2;
integer j = 70;
string tot ='';
    do{
        
        ss = s.substring(j, i);
        string sp = ss;
        if(j==0)
            tot = sp;
        else
         tot = tot + '::'+sp ;
          j = i;
         i = i + 70;
    }while ( i <= l );
system.debug(tot + 'tot===');
string spl = tot.replace('::','<br>');
system.debug( spl + 'br==');

 

Is there any simple methods? please let me know..

 

Thanks,

Laskhmi