+ Start a Discussion
dlCamelotdlCamelot 

Converting Value to String

Hi All,

I'm trying to get an array of strings returned based on Integer n, but seem to keep getting a value returned instead of a string.  What do I need to adjust?  Thanks!

public class StringArrayTest {
    public void generateStringArray(){
        List<String>stringArray = new List<String>{};
        Integer n = 4;
        for(Integer i=n;i<stringArray.size();i++){
            String myInt = n.format();
            stringArray.add('Test' + myInt);
        }
         return stringArray;  
    }
}
ShashankShashank (Salesforce Developers) 
The method n.format() will convert integer 4 to a string '4', and hence it will still be displayed only as '4'.
Balaji BondarBalaji Bondar
Dasha,
Use below code to convert integer to string
public class StringArrayTest {
    public void generateStringArray(){
        List<String>stringArray = new List<String>{};
        Integer n = 4;
        for(Integer i=n;i<stringArray.size();i++){
            String myInt = string.valueof(n);
            stringArray.add('Test' + myInt);
        }
         return stringArray;  
    }
}
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.