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
BittusBittus 

How to reverse a string using array concept in apex

hiii all.... i need to reverse a given string with apex class using array concept 
KaranrajKaranraj
You can able to reverse a string in apex using reverse() string method in apex class. Check the below example
String strSample = 'Hello';
String strReversedSample = strSample.reverse();
//Output of the strReversedSample will be 'olleH'


 
Prady01Prady01
Yes Winter 13 adds a new String method 'reverse' to string manupulation bank
but here is one more way
String original= 'abcdef';
String revStr = ' ';

for (Integer i = original.length()-1; i >= 0; i--)
{
	revStr += original.substring(i, i+1);
}

system.debug(revStr );
Hope this helps!
Thanks
Prady01
BittusBittus
hi all... thnq for ur reply... can u please give the program for string reversal with array concept like in java for apex class
KaranrajKaranraj
Can you please share you java sample code so that it will be easy for us to assit you build similar one in apex class. If you like to simply reverse the string you can go with the string reverse() method.
BittusBittus
Hii All... this is the code #include #include int main() { char str[100], temp; int i, j = 0; printf("\nEnter the string :"); gets(str); i = 0; j = strlen(str) - 1; while (i < j) { temp = str[i]; str[i] = str[j]; str[j] = temp; i++; j--; } printf("\nReverse string is :%s", str); return (0); }
Tasnim TrainingTasnim Training
Hi, this is a little different, but I was able to convert Java to Apex to manipulate a string to tell whether or not the string is a palindrome (the same word backwards and forwards).

Java:
 
public class Palindrome
{
 public static void main(String args[])
 {
 String original = "yasay";
 String reverse = "";
 
 for(int i = original.length() - 1; i >= 0; i--)
 {
 reverse = reverse + original.charAt(i);
 }
 
 if(original.equals(reverse))
 {
 System.out.println("String is Palindrome");
 }
 else
 {
 System.out.println("String is not Palindrome");
 }
 }
}

Apex:
 
public class Palindrome
{
    public Palindrome (String original, String reverse) {
        { 
            for(Integer i = original.length() - 1; i >= 0; i--)
            {
                reverse += original.substring(i, i+1);
            }
            if(original.equals(reverse))
            {
                System.debug('333 String is Palindrome');
            }
            else
            {
                System.debug('333 String is not Palindrome');
            }
        }
    }
}

 
megha agarwal 20megha agarwal 20
I tried Palindrome of string with Reverse function
public class Palindrome {
    public void palindromeornot(String originalone){
        string reverseone='';
          reverseone =  originalone.reverse();
        
            if(originalone.equals(reverseone)){
                system.debug('string is palindrome');
            }
            else{
                  system.debug('string is not palindrome');
            
        }
    }
}

for Annoymous window
Palindrome p=new Palindrome();
p.palindromeornot('yay');
hope this might help someone.
Malika Pathak 9Malika Pathak 9

Hi Bittus,

public class ReverseString {
public static void getreverse(string originalString)
{
String [] StringList= new List<String>();
StringList= originalString.split(''); // convert String to Array
string reversedString='';
for(integer i=StringList.size()-1;i>=0;i--)
{
reversedString=reversedString+originalString.substring(i,i+1);
}
System.debug('originalString--> '+originalString);
System.debug('reversedString--> '+reversedString);
}
}
if you find this answer is helpful for you then mark this as a best answer.
Channabasayya K 16Channabasayya K 16
If you don't want to use any inbuilt string function to achieve this, use this

String originalString = 'Hello world';
String reversedString = '';

for (Integer i = originalString.length(); i > 0; i--) {
    reversedString += originalString.substring(i-1,i);
}

System.debug('Reversed string : ' + reversedString);
Ravi Bisane 1Ravi Bisane 1
//How to Reverse the String
public class BasicProgramming1 {
    public static void myMethod1(){
    String Dataforcheck= 'PQRST';
String revStr = ' ';

for (Integer i = Dataforcheck.length()-1; i >= 0; i--)
{
    revStr = revStr + Dataforcheck.substring(i, i+1);
}

system.debug(revStr );
    }

}
// Run in EAW
BasicProgramming1.myMethod1();