DEV Community

Abhishek Sharma Gaur
Abhishek Sharma Gaur

Posted on

Manipulation of string in Javascript, Python and Ruby

A string is known as a sequence of characters, and string manipulation is something we programmers do all the time. Here are some of the basic string manipulation operations and how we implement them in Python, Javascript and Ruby.

To Uppercase: Converting all the characters in a string into uppercase.

To Lowercase: Converting all the characters in a string into lowercase.

Length of string: A very midget but crucial function used in most of the code. Is to find the length of a string.

Finding a character: Another crucial function to detect a substring inside a string.

Reverse a string: Mostly used in the form of a stack, a native function with O(n) time complexity to help out resolving code convolutions.

Below we have a Javascript implementation

let my_name = "Abhishek Sharma"
console.log('my_name in toLowerCase', my_name.toLowerCase());
console.log('my_name in toUpperCase', my_name.toUpperCase());
console.log('my_name string length', my_name.length);
console.log('find character in string', my_name.includes("b"), my_name[2]);
console.log('my_name in reverse', my_name.split('').reverse().join(''));

Enter fullscreen mode Exit fullscreen mode

Below we have a Python implementation

my_name = "Abhishek Sharma"
print('my_name in toLowerCase', my_name.casefold());
print('my_name in toUpperCase', my_name.capitalize());
print('my_name string length', len(my_name));
print('find character in string', my_name.rfind("b"), my_name[2]);
print('my_name in reverse', my_name[::-1]);

Enter fullscreen mode Exit fullscreen mode

Below we have a Ruby implementation

my_name = "Abhishek Sharma"

puts "my_name in toLowerCase #{my_name.downcase}"
puts "my_name in toUpperCase #{my_name.upcase}"
puts "my_name string length #{my_name.size} #{my_name.length}"
puts "find character in string #{my_name.include? 'b'} #{my_name[1]}"
puts "my_name in reverse #{my_name.reverse}"

Enter fullscreen mode Exit fullscreen mode

Top comments (0)