How to Remove the Leading Character from a String

How do you remove the first character of a string?

Assuming that the question uses "character" to refer to what Go calls a rune, then use utf8.DecodeRuneInString to get the size of the first rune and then slice:

func trimFirstRune(s string) string {
_, i := utf8.DecodeRuneInString(s)
return s[i:]
}

playground example

As peterSO demonstrates in the playground example linked from his comment, range on a string can also be used to find where the first rune ends:

func trimFirstRune(s string) string {
for i := range s {
if i > 0 {
// The value i is the index in s of the second
// rune. Slice to remove the first rune.
return s[i:]
}
}
// There are 0 or 1 runes in the string.
return ""
}

R remove first character from string

If we need to remove the first character, use sub, match one character (. represents a single character), replace it with ''.

sub('.', '', listfruit)
#[1] "applea" "bananab" "ranggeo"

Or for the first and last character, match the character at the start of the string (^.) or the end of the string (.$) and replace it with ''.

gsub('^.|.$', '', listfruit)
#[1] "apple" "banana" "rangge"

We can also capture it as a group and replace with the backreference.

sub('^.(.*).$', '\\1', listfruit)
#[1] "apple" "banana" "rangge"

Another option is with substr

substr(listfruit, 2, nchar(listfruit)-1)
#[1] "apple" "banana" "rangge"

Delete first character of string if it is 0

You can remove the first character of a string using substring:

var s1 = "foobar";
var s2 = s1.substring(1);
alert(s2); // shows "oobar"

To remove all 0's at the start of the string:

var s = "0000test";
while(s.charAt(0) === '0')
{
s = s.substring(1);
}

How to remove first and last character of a string in Rust?

You can use the .chars() iterator and ignore the first and last characters:

fn rem_first_and_last(value: &str) -> &str {
let mut chars = value.chars();
chars.next();
chars.next_back();
chars.as_str()
}

It returns an empty string for zero- or one-sized strings and it will handle multi-byte unicode characters correctly. See it working on the playground.

What is the most succinct way to remove the first character from a string in Swift?

If you're using Swift 3, you can ignore the second section of this answer. Good news is, this is now actually succinct again! Just using String's new remove(at:) method.

var myString = "Hello, World"
myString.remove(at: myString.startIndex)

myString // "ello, World"

I like the global dropFirst() function for this.

let original = "Hello" // Hello
let sliced = dropFirst(original) // ello

It's short, clear, and works for anything that conforms to the Sliceable protocol.

If you're using Swift 2, this answer has changed. You can still use dropFirst, but not without dropping the first character from your strings characters property and then converting the result back to a String. dropFirst has also become a method, not a function.

let original = "Hello" // Hello
let sliced = String(original.characters.dropFirst()) // ello

Another alternative is to use the suffix function to splice the string's UTF16View. Of course, this has to be converted back to a String afterwards as well.

let original = "Hello" // Hello
let sliced = String(suffix(original.utf16, original.utf16.count - 1)) // ello

All this is to say that the solution I originally provided has turned out not to be the most succinct way of doing this in newer versions of Swift. I recommend falling back on @chris' solution using removeAtIndex() if you're looking for a short and intuitive solution.

var original = "Hello" // Hello
let removedChar = original.removeAtIndex(original.startIndex)

original // ello

And as pointed out by @vacawama in the comments below, another option that doesn't modify the original String is to use substringFromIndex.

let original = "Hello" // Hello
let substring = original.substringFromIndex(advance(original.startIndex, 1)) // ello

Or if you happen to be looking to drop a character off the beginning and end of the String, you can use substringWithRange. Just be sure to guard against the condition when startIndex + n > endIndex - m.

let original = "Hello" // Hello

let newStartIndex = advance(original.startIndex, 1)
let newEndIndex = advance(original.endIndex, -1)

let substring = original.substringWithRange(newStartIndex..<newEndIndex) // ell

The last line can also be written using subscript notation.

let substring = original[newStartIndex..<newEndIndex]

How to remove first character from C-string?

if (contents[0] == '\n') 
memmove(contents, contents+1, strlen(contents));

Or, if the pointer can be modified:

if (contents[0] == '\n') contents++;

Right way to remove a leading character

This should be enough:

replace(/^_/, '');

How to remove the first and the last character of a string

Here you go

var yourString = "/installers/";
var result = yourString.substring(1, yourString.length-1);

console.log(result);


Related Topics



Leave a reply



Submit