I need to delete the first 3 letters of a string and the last 3 letters of a string. I know I can use substr() to start at a certain character but if I need to strip both first and last characters i'm not sure if I can actually use this. Any suggestions?
To remove the last three characters from a string, we can use the substr() function by passing the start and length as arguments.
slice() method to remove the last 3 characters from a string, e.g. const withoutLast3 = str. slice(0, -3); . The slice method will return a new string that doesn't contain the last 3 characters of the original 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.
Using trim : trim($dataList, '*'); This will remove all * characters (even if there are more than one!) from the end and the beginning of the string.
Pass a negative value as the length
argument (the 3rd argument) to substr()
, like:
$result = substr($string, 3, -3);
So this:
<?php $string = "Sean Bright"; $string = substr($string, 3, -3); echo $string; ?>
Outputs:
n Bri
Use
substr($var,1,-1)
this will always get first and last without having to use strlen.
Example:
<?php $input = ",a,b,d,e,f,"; $output = substr($input, 1, -1); echo $output; ?>
Output:
a,b,d,e,f
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