简体   繁体   中英

Remove trailing delimiting character from a delimited string

What is fastest way to remove the last character from a string?

I have a string like

a,b,c,d,e,

I would like to remove the last ',' and get the remaining string back:

OUTPUT: a,b,c,d,e

What is the fastest way to do this?

Contrary to the question asked, rtrim() will remove any number of characters, listed in the second argument, from the end of the string. In case you expect just a single comma, the following code would do:

$newarraynama = rtrim($arraynama, ",");

But in my case I had 2 characters, a comma and a space, so I had to change to

$newarraynama = rtrim($arraynama, " ,");

and now it would remove all commas and spaces from the end of the string, returning a, b, c, d, e either from a, b, c, d, e, , a, b, c, d, e,,, , a, b, c, d, e, or a, b, c, d, e , ,, , ,

But in case there could be multiple commas but you need to remove only the last one , then rtrim() shouldn't be used at all - see other answers for the solution that directly answers the question.

However, rtrim() could be a good choice if you don't know whether the extra character could be present or not. Unlike substr -based solutions it will return a, b, c, d, e from a, b, c, d, e

You can use substr :

echo substr('a,b,c,d,e,', 0, -1);
# => 'a,b,c,d,e'

In case the last character could be multi-byte, then mb_substr() should be used instead.

An alternative to substr is the following, as a function:

substr_replace($string, "", -1)

Is it the fastest? I don't know, but I'm willing to bet these alternatives are all so fast that it just doesn't matter .

Note that this function is not multibyte-safe, and will produce the undesired result if the last character will happen to be a multi-byte one.

You can use

substr(string $string, int $start, int[optional] $length=null);

See substr in the PHP documentation. It returns part of a string.

使用字符串锚“$”的正则表达式结尾

$string = preg_replace("/,$/", '', $string);

if you do you really want to replace a last char:

$string = "a,b,c,d,e,";

$string[strlen($string)-1] = "";

echo $string; //output: a,b,c,d,e

"The fastest best code is the code that doesn't exist".

Speaking of edge cases, there is a quite common issue with the trailing comma that appears after the loop, like

$str = '';
foreach ($array as $value) {
    $str .= "$value,";
}

which, I suppose, also could be the case in the initial question. In this case, the fastest method definitely would be not to add the trailing comma at all:

$str = '';
foreach ($array as $value) {
    $str .= $str ? "," : "";
    $str .= $value;
}

here we are checking whether $str has any value already, and if so - adding a comma before the next item, thus having no extra commas in the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM