Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the most efficient way to remove first character of a string?

I have a string c1234 -- what's the most efficient and quick way to remove the first letter of a string?

like image 213
user1049097 Avatar asked Nov 21 '11 20:11

user1049097


People also ask

How do you remove the first character of a string in Java?

The idea is to use the deleteCharAt() method of StringBuilder class to remove first and the last character of a string. The deleteCharAt() method accepts a parameter as an index of the character you want to remove.

How do I remove the first 5 characters from a string?

Strings are immutable in JavaScript. Alternatively, you can use the slice() method. Use the String. slice() method to remove the first N characters from a string, e.g. const result = str.

How do I remove the first character from every cell?

1. Combine RIGHT and LEN to Remove the First Character from the Value. Using a combination of RIGHT and LEN is the most suitable way to remove the first character from a cell or from a text string. This formula simply skips the first character from the text provided and returns the rest of the characters.


1 Answers

Use slice!:

s = "Hello"
s.slice!(0) #=> "ello"

Try it in an irb:

ruby-1.9.3-p0 :001 > s = "Hello"
 => "Hello" 
ruby-1.9.3-p0 :002 > s.slice!(0) #=> "ello"
 => "H" 
ruby-1.9.3-p0 :003 > s
 => "ello" 
like image 91
halfdan Avatar answered Sep 28 '22 09:09

halfdan