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
nbansal10nbansal10 

Split long account name

Hi there

Would like to know if anyone has encountered this?  If some account's name is over 50 characters long, we would like to capture as much the first 50 characters but complete words in Name, and then the splii over to Name2.    We would not want to split a word in half. 

Any suggestions would be great.

Thanks!
nbknbk
You can try below.
if(name.length() >= 50)
{
         string name2 = name.substring(0,50);
}
string name=name;
CheyneCheyne
You can use the substring method to to split the name in two parts, with the first string being 50 characters long, but that of course might split the name in the middle of a word. All you should have to do is figure out where the last word begins in the first name and move that over to the beginning of the second name. This code is untested, but I think it should give you a good start.

//Initialize name1 to equal name. 
String name1 = name;
String name2;

//If name is less than 50 characters, then we don't have to do anything
if (name.length() >= 50) {
    //This is the first 50 characters
    name1 = name.substring(0, 50);

    //This is the rest of the name
    name2 = name.substring(50, name.length());
    
    //If name2 starts with a space, then we have already split it between words, so we're done
    if (!name2.substring(0, 1) == ' ') {
        //Get the index of the last occurrence of the space character in name1
        Integer lastSpaceIndex = name1.lastIndexOf(' ');
        if (lastSpaceIndex != -1) {
            //Take everything after the last space in name1 and prepend it to name2
            name2 = name1.substring(lastSpaceIndex, name1.length()) + name2;
            
            //Now remove everything after the last space in name1
            name1 = name1.substring(0, lastSpaceIndex);
        }
    }
}