You need to sign in to do that
Don't have an account?
I need help with java to apex
I need help with converting this java code:
public class Example{
public static void reverseWordInMyString(String str){
String[] words = str.split(" ");
String reversedString = "";
for (int i = 0; i < words.length; i++){
String word = words[i];
String reverseWord = "";
for (int j = word.length()-1; j >= 0; j--){
reverseWord = reverseWord + word.charAt(j);
}
reversedString = reversedString + reverseWord + " ";
}
System.out.println(str);
System.out.println(reversedString);
}
}
so far this is what i got :
public class Example {
public static void reverseWordInMyString(String str){
String[] words = str.split(' ');
String reversedString = '';
Integer i;
Integer j;
for(i=0; i<words.length(reversedString); i++){
String reverseWord="";
for(j = words.length(reverseWord)-1; j>=0; j--){
reverseWord=reverseWord + word.charAt(j);
}
reversedString = reversedString + reverseWord + "";
}
System.debug(str);
System.debug(reversedString);
}
}
All Answers
public class Example
{
public static void reverseWordInMyString( String str )
{
List<String> words = str.split( '\\s' );
List<String> sdrow = new List<String>();
for ( String word : words ) sdrow.add( word.reverse() );
String reversedString = String.join( sdrow, ' ' );
System.debug( str );
System.debug( reversedString );
}
}
</pre>