[RUBY] Deletes after the specified character from the character string

str = "A30-60_10000"

Erase the back

str.sub(/_.*/, "")

Result ↓

=> "A30-60"

Erase the front

str.sub(/.*_/, "")

Result ↓

"10000"

reference https://qiita.com/flmil/items/8cbfbf49844ddef79000

Recommended Posts

Deletes after the specified character from the character string
Set the date and time from the character string with POI
Change only one character from the String type string to uppercase
Divide the character string starting from the decimal point (using index.Of, substring)
[Java] Divide a character string by a specified character
[Swift] Copy the character string to the clipboard
I want to find out what character the character string appears from the left
[Delete the first letter of the character string] Ruby
I want to output the character number from the left where an arbitrary character string appears.
Search for character string B from character string A (duplicate count)
I want to find out if the specified character string is supported by the target character code
Get the next business day after the specified date in JAVA
Dynamically access the property specified by the string in Spring BeanWrapper
[Java] How to convert a character string from String type to byte type
Correct the character code in Java and read from the URL
[Java] Delete the specified number of characters from the end of StringBuilder
`Array # index` cannot be searched from the middle,` String # index` can be
[Java beginner] Conversion from character string to numerical value-What is the parseInt method of the Integer class? ~