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
AbAb 

Appednd extra O or characters if the string is less than 10 characters

Hello,

I am geeting a substring like below
strText = strText.substring(0,10);
How can i make it 10 characters if the str text is less than 9 characters.

Thank you for suggestion !


 
Best Answer chosen by Ab
lnallurilnalluri
Hi Sandrine,

Check if this works.
 
string strText='test';
Integer diffLength=10-strText.length();
if(strText.length()==10){
   strText=strText.substring(0,10); 
}else{
    for(integer i=0;i<diffLength ;i++){
   strText = strText+'O';    
    }
}
system.debug('strText=>'+strText);

 

All Answers

lnallurilnalluri
Hi Sandrine,

Check if this works.
 
string strText='test';
Integer diffLength=10-strText.length();
if(strText.length()==10){
   strText=strText.substring(0,10); 
}else{
    for(integer i=0;i<diffLength ;i++){
   strText = strText+'O';    
    }
}
system.debug('strText=>'+strText);

 
This was selected as the best answer
Deepali KulshresthaDeepali Kulshrestha
Hi Sandrine,

Please check the code below :
 
public class Append_characters {
    public static void charactersAppend()
    {
    String str= 'aaaa';
    Integer strLength = str.length();
    Integer diffOfChar=10-strLength;
    if(strLength != 10)
    {
        for(integer i=0;i<diffOfChar ;i++)
        {
            str = str+'O'; 
        }
    }
    else
    {
        str=str.substring(0,10);
    }
    system.debug('Result is::::'+str);
    }
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com