Reverse String

From SwiftAPI

Jump to: navigation, search

Contents

C#

string a = "Hello World";
char[] chars = a.ToCharArray();
Array.Reverse(chars);
string b = new String(chars);
// b == "dlroW olleH"

Java

String a = "Hello World";
String b = new StringBuffer(a).reverse().toString();
// b == "dlroW olleH"

Perl

$a = "Hello World";
$b = reverse($a);
# $b == "dlroW olleH"

PHP

php.net

string strrev ( string $string )
$a = "Hello World";
$b = strrev($a);
// $b == "dlroW olleH"

Python

a = "Hello World"
b = a[::-1]
# b == "dlroW olleH"

Ruby

Ruby-Doc.org

str.reverse => new_str
a = "Hello World"
b = a.reverse
# b == "dlroW olleH"

Swift

let a = "Hello World"
let b = String(reverse(a))
// b == "dlroW olleH"
Personal tools