+ Start a Discussion
mahi68mahi68 

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

Hi,

 

   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-

     -ning

 

Any help is Appreciated

liron169liron169
Hello,

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

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

mahi68mahi68
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
hpereirahpereira

Hello,

 

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);
dividedStrings.add(tempString);
if (temp1.length() = 70 && input.length() > 70)
input = input.substring(70, input.length());
else
cont = false;
}

 

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

liron169liron169

Then loop it.
something like:

Integer currentPos=0;
while(currentPos<myStr.length())
{
    String subStr = myStr.subString(currentPos, currentPos + 70);
    //do something with subStr....
    currentPos+=70;
}

mahi68mahi68
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:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_string.htm

Dhaval PanchalDhaval Panchal

Hello,

 

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());
                     lstStr.add(strTemp);
              }
              system.debug('RESULT:>>>');
              for(String str:lstStr){
                     system.debug(str);
              }
       }
       //else no need to split
}

sfdcfoxsfdcfox

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()) {
	parts.add(m.group(0));
}

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