The nth and n + 1st characters of a Ruby string


It will be a memo for learning.


Since the character string s is input, output the nth character and the n + 1st character. n + No output if there is no first character

Input example 1
Output example 1
e a

Source code

n = gets.chomp.to_i
str = gets.chomp

puts "#{str[n - 1]} #{str[n]}" if str[n]


n = gets.chomp.to_i
str = gets.chomp

・ Read what number the character string is in n -Specify a character string with str

puts "#{str[n - 1]} #{str[n]}" if str[n]

-This statement calls the nth character of str and the n + 1 character. -By describing str [n] in the if statement, when the first character or the last character is called, it is not output.


The explanation of the if statement may be difficult to understand. I would appreciate it if you could point out any mistakes.

Recommended Posts

The nth and n + 1st characters of a Ruby string
Count the number of occurrences of a string in Ruby
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
Extract a part of a string with Ruby
[Delete the first letter of the character string] Ruby
[Java] The confusing part of String and StringBuilder
[Ruby] Cut out a string using the slice method
[Technical memo] About the advantages and disadvantages of Ruby
[Ruby] Class nesting, inheritance, and the basics of self
[ruby] How to assign a value to a hash by referring to the value and key of another hash
[Java] Cut out a part of the character string with Matcher and regular expression
[Ruby] Get in the habit of using the dup method when making a copy of a string variable
Determine that the value is a multiple of 〇 in Ruby
[Ruby] Questions and verification about the number of method arguments
A note about the seed function of Ruby on Rails
I tried JAX-RS and made a note of the procedure
[Ruby] Creating code using the concept of classes and instances
[Ruby] About the difference between 2 dots and 3 dots of range object.
[Ruby] How to retrieve the contents of a double hash
Install Ruby 3.0.0 Preview 1 with a combination of Homebrew and rbenv
Cut out a Ruby string
[Swift] When you want to know if the number of characters in a String matches a certain number ...
Generate a serial number with Hibernate (JPA) TableGenerator and store it in the Id of String.
[Ruby] Relationship between parent class and child class. The relationship between a class and an instance.
Want to know what Ruby n is the power of 2? (Power judgment of 2)
I tried to summarize the methods of Java String and StringBuilder
[Illustration] Finding the sum of coins with a recursive function [Ruby]
How to check for the contents of a java fixed-length string
About the behavior of ruby Hash # ==
[Ruby] See the essence of ArgumentError
This and that of the JDK
A memorandum of the FizzBuzz problem
Various methods of the String class
I read the source of String
[Ruby] Display the contents of variables
How to operate IGV using socket communication, and the story of making a Ruby Gem using that method
[Ruby] I want to output only the odd-numbered characters in the character string
A collection of phrases that impresses the "different feeling" of Java and JavaScript
Make a daily build of the TOPPERS kernel with Gitlab and Docker
The story of forgetting to close a file in Java and failing
[Ruby] I want to extract only the value of the hash and only the key
[Ruby basics] About the role of true and break in the while statement
Get a rough idea of the differences between protocols, classes and structs!
[Java] How to get to the front of a specific string using the String class
[Ruby] Returns characters in a pyramid shape according to the entered numbers
[Ruby] I want to make an array from a character string with the split method. And vice versa.
A brief summary of DI and DI containers
Make a note of Ruby keyword arguments
Dharma doll drop of characters (ruby edition)
Folding and unfolding the contents of the Recyclerview
About the operation of next () and nextLine ()
Explanation of Ruby Time and Date objects
Summary of hashes and symbols in Ruby
[Ruby] Cut off the contents of twitter-ads
Ruby from the perspective of other languages
[Ruby] Classification and usage of loops in Ruby
Comparison of JavaScript objects and Ruby classes
About the mechanism of the Web and HTTP
[Java] Check the number of occurrences of characters
Find the difference from a multiple of 10
Item 63: Beware the performance of string concatenation