[Ruby] Methods that can be used with strings

String replacement

--Character string.sub (/ converted character /, "character string you want to replace) ・ ・ ・ Convert only the first character

irb(main):026:0> str
=> "123123123"
irb(main):027:0> str.sub(/12/,"ab")
=> "ab3123123"
irb(main):028:0> str
=> "123123123"

When I converted the letter 12 to ab, it was replaced with ab3123123 However, the instance itself has not been replaced

--Character string.gsub (/ converted character /, "character string you want to replace) ・ ・ ・ Convert all corresponding characters g means global

irb(main):029:0> str.gsub(/12/,"ab")
=> "ab3ab3ab3"

Again, the instance itself is not replaced

--String.sub! (/ Converted character /, "Character string you want to replace) ... Only the first character of the original instance itself is changed

!! Is a destructive method ... transforms the instance itself

irb(main):030:0> str.sub!(/12/,"ab")
=> "ab3123123"
irb(main):031:0> str
=> "ab3123123"

--String.gsub! (/ Converted character /, "Character string you want to replace) ... Changes all characters in the original variable itself

irb(main):034:0> str = "123123123"
=> "123123123"
irb(main):035:0> str.gsub!(/12/,"ab")
=> "ab3ab3ab3"
irb(main):036:0> str
=> "ab3ab3ab3"

Search for strings

--Character string.index ("Character string you want to search") ・ ・ ・ Search and tell you where the character string you want to search is.

irb(main):038:0> "12345".index("2")
=> 1

Of course, you can also search for strings stored in variables.

irb(main):041:0> str = "abcde"
=> "abcde"
irb(main):042:0> str.index("c")
=> 2

Looking at this, you thought that "1" was not strange even though you were searching for "2" with "12345", right? Stored elements are assigned numbers such as subscripts Since the subscript starts from 0, the character string "12345" is assigned from "0" instead of being assigned from "1". That's why the search result for "2" became "1".

Delete string

--Character string.delete ("Character string you want to delete") ・ ・ ・ Delete the specified character string --Character string.delete! ("Character string you want to delete") ... (also from the instance) Delete the specified character string

irb(main):044:0> str.delete("cd")
=> "abe"
irb(main):045:0> str
=> "abcde"

Of course! If you add, it will be a destructive method, so the instance will also be changed (deleted).

--Character string.chop ・ ・ ・ You can delete the end of the line of the character string.

irb(main):049:0> str.chop
=> "abcd"

This too! Is a destructive method

--Character string.chomp ・ ・ ・ Line feed code can also be deleted

For example, if you enter a character string using gets without chomo

irb(main):054:0> gets.to_s
My name is Taro
=> "My name is Taro\n"

And \ n and line feed code are built in

If you use chomp it

irb(main):053:0> gets.to_s.chomp
My name is Taro
=> "My name is Taro"

\ n is not included

Create an array by splitting characters

String .split

irb(main):055:0> "aa bb cc".split
=> ["aa", "bb", "cc"]

If you insert a space, the array will be split and created. As an aside, when creating a tag function with rails, if there is no gem, use split to create multiple (experience story) It is also possible to specify a character and divide it, for example, if ","

You can split by specifying a comma with the string .split (",")

Example:
irb(main):058:0> "aa,bb,cc".split(",")
=> ["aa", "bb", "cc"]

How to return the created array to a character string Use the array .join

Example: Put it in what["aa", "bb", "cc"]Is stored
irb(main):059:0> what.join
=> "aabbcc"

If you specify the array .join ("")

irb(main):061:0> what.join(" ")
=> "aa bb cc"

Return to the string in the split state

String conversion

Character string .upcase ・ ・ ・ Convert to uppercase Character string .downcase ・ ・ ・ Convert to lowercase

irb(main):063:0> length = "aaa"
=> "aaa"
irb(main):064:0> length.upcase
=> "AAA"
irb(main):065:0> length.upcase!
=> "AAA"
irb(main):066:0> length
=> "AAA"
irb(main):067:0> length.downcase
=> "aaa"
irb(main):068:0> length.downcase!
=> "aaa"
irb(main):069:0> length
=> "aaa"

Character string .reverse ・ ・ ・ Reverses the character string horizontally

irb(main):072:0> length = "abc"
=> "abc"
irb(main):073:0> length.reverse
=> "cba"

Character string.slice (range) ・ ・ ・ Cuts the specified range of the character string

irb(main):076:0> length = "abcdef"
=> "abcdef"
irb(main):077:0> length.slice(2..4)
=> "cde"

Cut out the string cde in the range 2-4 This slice (2..4) .. is used when doing a range

last

I tried to summarize what was in the notebook from one end Maybe the interpretation is wrong, so please point it out if you like.

Recommended Posts

[Ruby] Methods that can be used with strings
Organize methods that can be used with StringUtils
Ruby array methods that can be used with Rails (other than each)
Learning Ruby with AtCoder Beginners Selection [Some Sums] Increase the methods that can be used
Summary of css selectors that can be used with Nookogiri
Create a page control that can be used with RecyclerView
Firebase-Realtime Database on Android that can be used with copy
Until ruby can be used on windows ...
Simple slot machine implementation that can be used with copy and paste
[Rails] "pry-rails" that can be used when saving with the create method
Performance analysis and failure diagnostic tools that can be used with OpenJDK
About the matter that hidden_field can be used insanely
Convenient shortcut keys that can be used in Eclipse
Four-in-a-row with gravity that can be played on the console
Ruby on Rails 5 quick learning practice guide that can be used in the field Summary
Syntax and exception occurrence conditions that can be used when comparing with null in Java
[Ruby] Handle instance variables with instance methods
Ruby methods often used in Rails
Static analysis tool that can be used on GitHub [Java version]
Build an environment where pip3 can be used with CentOS7 + Python3
Summary of ORM "uroboroSQL" that can be used in enterprise Java
File form status check sheet that can be deleted with thumbnails
I made a question that can be used for a technical interview
Power skills that can be used quickly at any time --Reflection
Summary of JDK that can be installed with Homebrew (as of November 2019)
Introduction to Java that can be understood even with Krillin (Part 1)
Set the access load that can be changed graphically with JMeter (Part 2)
Java file input / output processing that can be used through historical background
[Ruby basics] Methods that frequently use blocks
About the problem that the server can not be started with rails s
Set the access load that can be changed graphically with JMeter (Part 1)
List of methods used when manipulating strings
Object-oriented that can be understood by fairies
[ERROR message display] A simplified version that can be used at any time with the rails partial template.
[Swift] Color Picker that can be used with copy and paste (palette that allows you to freely select colors)
[Android] I want to create a ViewPager that can be used for tutorials
About the case that ("b" .. "aa") could not be used in Ruby Range
Technology excerpt that can be used for creating EC sites in Java training
I made a THETA API client that can be used for plug-in development
[Rails 6] method :: delete cannot be used with link_to
Compiled kotlin with cli with docker and created an environment that can be executed with java
Ruby Learning # 15 Methods
About Ruby methods
Basic functional interface that can be understood in 3 minutes
How foreign keys can be saved even with nil
Write a class that can be ordered in Java
Scala String can be used other than java.lang.String method
Private methods can or cannot be overridden by public methods
In Ruby, methods with? Do not always return true/false.
[Java 8] Sorting method in alphabetical order and string length order that can be used in coding tests
The world of Azure IoT that can be played on the DE10-Nano board: Ajuchika with FPGA !!?