+ Start a Discussion

How to divide the string into for every 70 characters and put at the end -



   How I can divide the long text into no of lines,


 Ex: "hello good morning"   just assume r is the 70 character 


  then I would like to divide like this


     hello good mor-



Any help is Appreciated


Where you want to divide it? inside apex code?
If so, use function subString

'hamburger'.substring(4, 8); // Returns "urge"

Thanks for your reply.

no man, i have a string assume 1000 characters.
for each line i would like to display only 70 characters. 1000/70=15 lines



I would do something like this:


String input = 'hello good morning';
String tempString = '';
List<String> dividedStrings = new List<String>();
boolean cont = true;

while (cont)
tempString = input.substring(0, 70);
if (temp1.length() = 70 && input.length() > 70)
input = input.substring(70, input.length());
cont = false;


At the end you have a list with all your strings separated.


Then loop it.
something like:

Integer currentPos=0;
    String subStr = myStr.subString(currentPos, currentPos + 70);
    //do something with subStr....

i did like this
String [] strParse;
If (strInput == null) {
strParse.add '';
} else {
// Add them in 172 character blocks
do {
strParse.add strInput.subString(0, 70);
strInput = strInput.subString(70, strInput.length() - 70);
} while (strInput.length > 70);
strParse.add strInput;
Yoganand GadekarYoganand Gadekar

you can use substring method..

Heres a document link that gives details about all the strign methods:


Dhaval PanchalDhaval Panchal



Try this......


public static void splitString(String strText, Integer noOfChar){
        if(strText <> null && strText.length()>noOfChar){
              List<String> lstStr = new List<String>();
              while(strText.length() > noOfChar){
                     String strTemp = strText.left(noOfChar);
                     strText = strText.subString(noOfChar, strText.length());
              for(String str:lstStr){
       //else no need to split


You can use a regular expression to quickly parse the string. Here's the format that separates a string, breaking by spaces:


String s = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pharetra tincidunt enim, eu posuere dolor bibendum in. Sed ornare placerat sodales. Sed ac felis at ligula ornare porta at nec tortor. Quisque sed nisi aliquet diam feugiat semper. Aenean non semper ante. Sed metus lacus, gravida eget velit nec, pretium aliquet lacus. Vestibulum rhoncus libero quis lectus bibendum, ac lacinia nulla rutrum.Vestibulum vitae sollicitudin odio. Etiam accumsan purus neque, non suscipit libero convallis sed. Pellentesque tempor ultrices enim non tincidunt. In consequat porttitor nibh, sed adipiscing felis blandit sed. Duis iaculis ante ut dignissim pretium. Integer placerat quam vitae enim molestie pellentesque. Nunc ligula metus, consectetur pellentesque lacinia vitae, feugiat vel ipsum. Proin hendrerit, mauris sed adipiscing cursus, turpis nulla venenatis ligula, quis pharetra est tortor eget mi. Fusce sed neque lectus. Nullam ac arcu eu leo sollicitudin sagittis. Maecenas ante mauris, pulvinar at varius quis, pharetra vel sapien.';
Pattern p = Pattern.compile('(.{1,70}) +');
Matcher m = p.matcher(s);
String[] parts = new String[0];
while(m.find()) {

Removing the " " near the end of the pattern will break it into strings exactly 70 characters long.