[Ruby] Various Ruby string operations

less than 1 minute read

Introduction

Keep a record of the problems that were solved and the answers to study the string manipulation method of Ruby.

Question 1

Convert “This” “is” “a” “pen” to “This is a pen”.

Answer 1

"This" + "is" + "a" + "pen"

Supplement

”+” can concatenate character strings.

Problem 2

Convert “BENZ, BMW, Audi” to [“BENZ”, “BMW”, “Audi”].

Answer 2

"BENZ, BMW, Audi".split(",")

Supplement

[] represents an array. The split method can split a string. It can be separated by commas when used as an answer.

Problem 3

Convert “apple” to “Apple”.

Answer 3

"apple".capitalize

Supplement

The capitalize method can capitalize the first letter.

Problem 4

Convert “qiita” to “atiiq”.

Answer 4

"qiita".reverse

Supplement

The reverse method can reverse the character string.

Problem 5

Convert “Good morning” to “GOOD MORNING”.

Answer 5

"Good morning".upcase

Supplement

The upcase method can convert lowercase letters to uppercase.

Quote

Ruby 2.7.0 Reference Manual

Tags:

Updated: