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
sreejasreeja 

salesforce development for getting the right allignment

String s1 = 'Hello Max';
String s2 = s1.right(3);
System.assertEquals( 'Max', s2);

for this scenario i have created the apex calss ;

public class  testing {

 public string result{get;set;}

  public void  m1(string name){

     string trname =name.right(3);

 result = trname;
}

}


 visualforce page:

<apex:page>
 <apex:form>
  <apex:commandbutton value="editedname" action="{!m1}" />

  <apex:inputtext label="Enter your name"  value="{!name}"
  {!result}
</apex:form>  
 
 
</apex:page>

 


the scenario is , need an inputfield , if you enter in the filed the last value should have to trim and have to display in the visualforce page.. 

 iam in confusion how it can be acheived.

 
Best Answer chosen by sreeja
Khan AnasKhan Anas (Salesforce Developers) 
Hi Ramya,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="TrimStringC">
    <apex:form>

        <apex:commandbutton value="editedname" action="{!m1}" />
        
        <apex:inputtext label="Enter your name"  value="{!input}"/>
        {!result}
    </apex:form>  
 
</apex:page>

Controller:
public class TrimStringC {
    
    public string result {get;set;}
    public string input {get;set;}
    
    public void  m1 (){        
        string trname = input.substringAfter(' ');      
        result = trname;
    }
}

You can use substringAfter(separator) which returns the substring that occurs after the first occurrence of the specified separator.

More information: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm#apex_System_String_substringBefore

Or you can use below code also:
public class TrimStringC {
    
    public string result {get;set;}
    public string input {get;set;}
    
    public void  m1 (){        
        string trname = input.right(3);      
        result = trname;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Ramya,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="TrimStringC">
    <apex:form>

        <apex:commandbutton value="editedname" action="{!m1}" />
        
        <apex:inputtext label="Enter your name"  value="{!input}"/>
        {!result}
    </apex:form>  
 
</apex:page>

Controller:
public class TrimStringC {
    
    public string result {get;set;}
    public string input {get;set;}
    
    public void  m1 (){        
        string trname = input.substringAfter(' ');      
        result = trname;
    }
}

You can use substringAfter(separator) which returns the substring that occurs after the first occurrence of the specified separator.

More information: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm#apex_System_String_substringBefore

Or you can use below code also:
public class TrimStringC {
    
    public string result {get;set;}
    public string input {get;set;}
    
    public void  m1 (){        
        string trname = input.right(3);      
        result = trname;
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
sreejasreeja
hi khans, thanks for your reply ;

 i have to use the leftpad and rightpad methods like this...



public class TrimStringC {
    
    public string result {get;set;}
    public string input1 {get;set;}
public string input2 {get;set;}
    
    public void  m1 (){ 
    
        string trname1 = input1.leftPad(18);    

string trname2 = input2.leftPad(18);      
        result = trname +trname2  ;
    }
}

   visualforcepage;;

<apex:page controller="TrimStringC">
    <apex:form >

        <apex:commandbutton value="editedname" action="{!m1}" />
        
        <apex:inputtext label="Enter your name"  value="{!input1}"/>
  <apex:inputtext label="Enter your name"  value="{!input2}"/>
        
         <br/>
        {!result}
    </apex:form>  
 
</apex:page>


-- if i  i give the values of  Samsung as input1   and  iphone as input2, and click on the button , it should display as the 
'           Samsung            iphone'  , padding of the two components should be of 18digits .. 
Swathi soma 2Swathi soma 2
Hi Ramya,

You can use input.substring(input.lastIndexOf(' ')+1) to get the last word of a string which is separated by space. 

public class TrimStringC {
    
    public string result {get;set;}
    public string input {get;set;}
    
    public void  m1 (){ 
        String trname = input.substring(input.lastIndexOf(' ')+1);
        result = trname;
    }
}
sreejasreeja
hi swathi, can you help me out with the padding in the left, to diplay the value with value and spaces , like if i print any recordname, with padding 20. it should have to print with  'Salesforce          '. salesforce+10blankspaces , total of 20 with value.

 hope you understand the scenario.
thanks 
Swathi soma 2Swathi soma 2
Hi Ramya,

String input='hello ';
Integer inputLength = input.length();
Integer ExpectedStringLen = 20;
Integer i; 
for(i=0;i<ExpectedStringLen - inputLength;i++)
{
    input = ' '+input;   //append space before the string. To append space after the string input = input + ' '; 
}

I hope this works for the scenario you explained above.

Regards.