I want to find out what character the character string appears from the left

I would like to find out what character "get" appears from the left in any character string and output the number.

An example output is

count_code("getexxcode")
 =>1
count_code("aaagetbbb")
=> 4
count_code("cozexxget")
=> 7

Create a method using the index method. reference: Ruby 3.0.0 Reference Manual: index method

Example of use:

str.index(The character string you want to search,Where to start the search)

First, create a method.

def count_code(str)

end

I would like to use the index method in it to output as shown in the output example.

def count_code(str)
  puts str.index("get", 0 )
end

You can now output. However, if nothing is done, it will not look like the output example, and the result will be as follows.

count_code("getexxcode")
 => 0
count_code("aaagetbbb")
=> 3
count_code("cozexxget")
=> 6

Finally,

def count_code(str)
  puts str.index("get", 0 ) + 1
end

By giving as, it becomes like the output example.

Recommended Posts

I want to find out what character the character string appears from the left
I want to output the character number from the left where an arbitrary character string appears.
I want to find out if the specified character string is supported by the target character code
[Ruby] I want to output only the odd-numbered characters in the character string
[Java] I want to calculate the difference from the date
I tried to find out what changed in Java 9
[Ruby] I want to make an array from a character string with the split method. And vice versa.
(ยด-`) .. oO (I want to easily find the standard output "Hello".
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
Change only one character from the String type string to uppercase
I want to find out which version of java the jar file I have is available
I want to convert InputStream to String
What I did in the migration from Spring Boot 1.4 series to 2.0 series
What I did in the migration from Spring Boot 1.5 series to 2.0 series
Find out what many threads are doing from the jstack results
[Android] I want to get the listener from the button in ListView
Deletes after the specified character from the character string
I want to output the day of the week
Run R from Java I want to run rJava
Ruby: I tried to find out where Nokogiri goes to see the encoding himself
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I want to graduate from npm install properly [2020]
I want to var_dump the contents of the intent
I want to get a list of only unique character strings by excluding fixed character strings from the file name
I want to simply write a repeating string
I tried to get the distance from the address string to the nearest station with ruby
I want to play with Firestore from Rails
I want to get only the time from Time type data ...! [Strftime] * Additional notes
I want to write quickly from java to sqlite
I want to truncate after the decimal point
I want to get the value in Ruby
I want to find the MD5 checksum of a file in Java and get the result as a string in hexadecimal notation.
Get the value from the array and find out what number it is included in
I want to reduce the number of unnecessary queries. From considering counter_cache to introducing counter_culture.
Tokoro I rewrote in the migration from Wicket 7 to 8
I want to judge the range using the monthly degree
What should I do to reload the updated Dockerfile?
I want to display the name of the poster of the comment
I want to dark mode with the SWT app
What I got from continuing to develop open source
[Java] How to cut out a character string character by character
[Java] How to erase a specific character from a character string
I want to call the main method using reflection
[Rough commentary] I want to marry the pluck method
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to redirect sound from Ubuntu with xrdp
I want to simplify the log output on Android
I want to connect to Heroku MySQL from a client
I want to add a delete function to the comment function
I want to fetch another association of the parent model from the intermediate table with has_many
In Java, I want to trim multiple specified characters from only the beginning and end.
I want to return a type different from the input element with Java8 StreamAPI reduce ()
I want to recreate the contents of assets from scratch in the environment built with capistrano
I can't find the docker image after updating to docker desktop 2.4.0.0
[Beginner] I want to modify the migration file-How to use rollback-
What I was addicted to when introducing the JNI library
I want to find a relative path in a situation using Path
I want to set the conditions to be displayed in collection_check_boxes
[Rails] [bootstrap] I want to change the font size responsively
[JDBC] I tried to access the SQLite3 database from Java.