[Ruby] I want to output only the odd-numbered characters in the character string

I want to output only the odd-numbered characters in the character string

For example, if you want to output only the odd-numbered characters in the alphabet.

str = "abcdefghijklmnopqrstuvwxyz"

str.each_char.with_index{|c, index|
   print c if index % 2 == 0
}
# => acegikmoqsuwy

I will explain each of them. I would be grateful if you could point out any mistakes you made.

each_char By using the chars method (also known as each_char) of the String class, you can process the characters in the string while extracting them one by one.

x = "TOKYO"
y = x.chars 
# => ["T", "O", "K", "Y", "O"]

each.with_index It is used when you want to number each data while turning each. In this case, each character extracted first is stored in the variable c. Then, if the variable index numbered by each.with_index is divisible by 2, the variable c is output. If it is divisible by 2, it will be an even number, but since the programming number includes 0, it has been reversed.

str = "abcdefghijklmnopqrstuvwxyz"

str.each_char.with_index{|c, index|
   print c if index % 2 == 0
}
# => acegikmoqsuwy

References

instance method String#each_char https://docs.ruby-lang.org/ja/latest/method/String/i/each_char.html A program that capitalizes and outputs only odd numbers http://sinyt.hateblo.jp/entry/2013/12/22/183217 How to use each_with_index https://qiita.com/tsuchinoko_run/items/5cef7dd9d8baf48ffde7

Recommended Posts

[Ruby] I want to output only the odd-numbered characters in the character string
I want to get the value in Ruby
I want to output the character number from the left where an arbitrary character string appears.
# 1_JAVA I want to get the index number by specifying one character in the character string.
I want to change the value of Attribute in Selenium of Ruby
In Java, I want to trim multiple specified characters from only the beginning and end.
I want to output the day of the week
I want to use arrow notation in Ruby
[Ruby] I want to extract only the value of the hash and only the key
I want to find out what character the character string appears from the left
I want to embed any TraceId in the log
I want to simplify the log output on Android
I want to convert characters ...
[Ruby] I want to make an array from a character string with the split method. And vice versa.
I want to set the conditions to be displayed in collection_check_boxes
(´-`) .. oO (I want to easily find the standard output "Hello".
I want to perform high-speed prime factorization in Ruby (ABC177E)
I want to change the log output settings of UtilLoggingJdbcLogger
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to create a Parquet file even in Ruby
Change only one character from the String type string to uppercase
I want to transition to the same screen in the saved state
[Ruby] I want to reverse the order of the hash table
I want to simplify the conditional if-else statement in Java
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
I want to convert InputStream to String
String output method memo in Ruby
I want to find out if the specified character string is supported by the target character code
I want to give edit and delete permissions only to the poster
[Ruby] I want to put an array in a variable. I want to convert to an array
I want to display the images under assets/images in the production environment
I want to remove the top margin in Grouped UITableView (swift)
[Java] I want to perform distinct with the key in the object
[Android] I want to get the listener from the button in ListView
[Swift] When you want to know if the number of characters in a String matches a certain number ...
[Swift] Copy the character string to the clipboard
I want to use @Autowired in Servlet
I want to write JSP in Emacs more easily than the default.
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I tried to solve the tribonacci sequence problem in Ruby, with recursion.
[Ruby] I want to display posted items in order of newest date
I want to get the IP address when connecting to Wi-Fi in Java
I thought about the best way to create a ValueObject in Ruby
I tried to make full use of the CPU core in Ruby
I want to display an error message when registering in the database
[Ruby] I tried to summarize the methods that frequently appear in paiza
How to debug the processing in the Ruby on Rails model only on the console
[Ruby] I tried to summarize the methods that frequently appear in paiza ②
[Ruby] Returns characters in a pyramid shape according to the entered numbers
I want to sort by tab delimited by ruby
[Delete the first letter of the character string] Ruby
I want to send an email in Java.
I tried to organize the session in Rails
Character string comparison: I was caught in the skill check problem of Paiza
I tried to solve the tribonatch sequence problem in Ruby (time limit 10 minutes)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
[Ruby] I want to do a method jump!
I want to var_dump the contents of the intent
I want to pass APP_HOME to logback in Gradle
I want to get a list of only unique character strings by excluding fixed character strings from the file name