I have a input string like:
$str = ':this is a applepie :) ';
How can I remove the first occurring :
with PHP?
Desired output: this is a applepie :)
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.
Remove the First Character From the String in Python Using the str. lstrip() Method. The str. lstrip() method takes one or more characters as input, removes them from the start of the string, and returns a new string with removed characters.
Remove first character in Excel To delete the first character from a string, you can use either the REPLACE function or a combination of RIGHT and LEN functions. Here, we simply take 1 character from the first position and replace it with an empty string ("").
The substr()
function will probably help you here:
$str = substr($str, 1);
Strings are indexed starting from 0, and this functions second parameter takes the cutstart. So make that 1, and the first char is gone.
To remove every :
from the beginning of a string, you can use ltrim:
$str = '::f:o:'; $str = ltrim($str, ':'); var_dump($str); //=> 'f:o:'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With