+ Start a Discussion
shasha777shasha777 

List using reverse string

please tell me how to write a programme for list using reverse string

 

sandeep@Salesforcesandeep@Salesforce

lets say List is List<String > TestList = new List<String>{'test1', 'paper'};

 

for list of reversed strings will be 

List<String> newList = new List<String.();

for ( String s : TestList)

{

    newList.add(reverse( s));

}

 

system.debug('======Reversed List======='+newList);

 

public String reverse( String string ) {

byte[] array = string.getBytes();
byte swap;

for( int i = 0, j = array.length - 1; i < array.length / 2; i++, j-- ) {
swap = array[ j ];
array[ j ] = array[ i ];
array[ i ] = swap;
}

return new String( array );
}

 

Please mark my answer as solution if it was helpful and give KUDOS. 

Tejpal KumawatTejpal Kumawat

Hi shasha777,

 

Please try this one ::

 

List<String > actualList = new List<String>{'tejpal', 'kumawat'};
List<String> reverseList = new List<String>();
for ( String s : actualList){
reverseList.add(s.reverse());
}

system.debug('Reverse List:::::::::::::::'+reverseList);