如何在 PHP 中删除字符串中的所有空格

本文将介绍在 PHP 中从一个字符串中去掉所有空格的方法。剥离所有空格是指从一个给定的字符串中删除所有空格。

  • 使用 str_replace() 函数
  • 使用 preg_replace() 函数

在 PHP 中使用 str_replace() 函数去掉所有空格

我们使用内置函数 str_replace() 来替换字符串或数组中的子字符串。替换后的字符串作为一个参数传递。使用该函数的正确语法如下。

str_replace($searchString, $replaceString, $originalString, $count);

内置函数 str_replace() 有四个参数。它的详细参数如下

参数 说明
$searchString 强制 它是我们要查找和替换的子字符串或数组
$replaceString 强制 它是我们要放在 $searchString 的位置的字符串。函数将检查 $searchString 的出现情况,并用 $replaceString 替换。该函数将检查 $searchString 的出现情况,然后用 $replaceString 替换它。它也可以是一个 array
$originalString 强制 它是原始的字符串,我们想从中找到一个子字符串或一个字符来替换。
$count 可选 它告诉人们对 $originalString 进行替换的总次数

这个函数返回所有替换后得到的最终字符串。

下面的程序显示了我们如何使用 str_replace() 函数从给定的字符串中删除所有空格。

<?php
$searchString = " ";
$replaceString = "";
$originalString = "This is a programming tutorial";
$outputString = str_replace($searchString, $replaceString, $originalString);
echo("The original string is: $originalString\n");
echo("The string without spaces is: $outputString");
?>

我们已经传递了一个空格字符作为 $searchString 和一个空字符串作为 $replaceString。输出将是没有空格的字符串。

输出:

The original string is: This is a programming tutorial
The string without spaces is: Thisisaprogrammingtutorial

现在,如果我们传入 $count 参数,函数将告诉我们这个字符串的替换次数。

<?php
$searchString = " ";
$replaceString = "";
$originalString = "This is a programming tutorial";
$outputString = str_replace($searchString, $replaceString, $originalString, $count);
echo("The original string is: $originalString\n");
echo("The string without spaces is: $outputString\n");
echo("The number of replacement operations is: $count");
?>

输出:

The original string is: This is a programming tutorial
The string without spaces is: Thisisaprogrammingtutorial
The number of replacement operations is: 4

在 PHP 中使用 preg_replace() 函数去除所有空格

在 PHP 中,我们也可以使用 preg_replace() 函数来删除字符串中的所有空格。这个函数不仅可以删除空格字符,还可以删除字符串中的制表符。使用这个函数的正确语法如下。

preg_replace($regexPattern, $replacementVar, $original, $limit, $count)

函数 preg_replace() 接受五个参数. 它的详细参数如下

参数 说明
$regexPattern 强制 它是我们将在原始字符串或数组中搜索的模式
$replacementVar 强制 它是我们用来替换搜索值的字符串或数组
$original 强制 它是一个字符串或数组,我们要从其中找到值并替换它。
$limit 可选 这个参数限制了替换的数量
$count 可选 这个参数告诉了我们对原始字符串或数组进行替换的总次数

我们将使用模式 /\s+/ 来查找空格。从字符串中删除空格的程序如下。

<?php
$searchString = " ";
$replaceString = "";
$originalString = "This is a programming tutorial";
$outputString = preg_replace('/\s+/', '', $originalString);
echo("The original string is: $originalString\n");
echo("The string without spaces is: $outputString\n");
?>

输出:

The original string is: This is a programming tutorial
The string without spaces is: Thisisaprogrammingtutorial

我们知道这个字符串的总替换次数是 4,现在我们将限制替换次数。

<?php
$searchString = " ";
$replaceString = "";
$limit = 2;
$originalString = "This is a programming tutorial";
$outputString = preg_replace('/\s+/', '', $originalString,$limit);
echo("The original string is: $originalString\n");
echo("The string without spaces is: $outputString\n");
?>

输出:

The original string is: This is a programming tutorial
The string without spaces is: Thisisa programming tutorial

注意,现在只有两个替换。

如何在 Python 中删除字符串中的最后一个字符

Python 字符串是一个用双引号或单引号括起来的字符组合。Python 提供了多个函数来操作字符串。本文将介绍删除字符串最后一个字符和特定字符的不同方法。

用 Python 中的切片方法从字符串中删除最后一个字符

让我们以下面的代码为例。

my_str='python string'
final_str=my_str[:-1]
print(final_str)

Python 字符串的索引从 0 开始,Python 也有负数索引,用 -1 来表示最后一个元素。分片操作符访问字符串的最后一个元素并将其删除。

切片方法的输出是。

python strin

Python 字符串最后 3 个字符用负索引法删除

我们可以在 Python 中使用负索引。最后一个字符从索引-1 开始,按照-2、-3、-4 等顺序到达第一个字符。

代码是:

my_str="python string"
final_str = my_str[:-3]
print(final_str)

输出:

python str

Python 字符串最后 3 个字符用切片法和正向索引去除

len() 函数确定字符串的长度。该方法将选择从起始位置 0 到最后位置 N 的元素,并减去最后 3 个字符,即(N-3)。

分片方法的第一个参数默认为 0。

其代码为

my_str="python string"
size = len(my_str)
final_str = my_str[:size - 3]
print(final_str)

输出:

python str

用 for 循环方法去除 Python 字符串最后 3 个字符的方法

我们对所有的字符串字符进行循环,从第一个索引 0 开始到最后一个索引(N-1),并删除字符串最后的 3 个字符。

代码是

my_str="python string"
n = 3
final_str = ""
for i in range(len(my_str) - n):
    final_str = final_str + my_str[i]
print(final_str)

输出:

 python str

Python 字符串最后 3 个字符用正则表达式方法去除

这个方法在 Python 中用来比较两组。我们使用一个内置的名为 re 的库,它可以检查一个字符串中的特定模式。

代码是:

import re
def rmve_2nd_grp(b):
    return b.group(1)
my_str = "Python String"
result = re.sub("(.*)(.{3}$)",rmve_2nd_grp,my_str)
print(result)

在代码中,sub() 方法比较了定义的模式,并将相似的对象传递给 rmve_2nd_grp() 方法。匹配的对象有两个组,但 rmve_2nd_grp() 比较组 1 中的字符并返回字符串。

输出:

Python Str

用正则表达式方法去除 Python 字符串的最后一个字符

如果你需要删除最后一个字符,请使用以下代码。

import re
def rmve_2nd_grp(b):
    return b.group(1)
my_str = "Python String"
result = re.sub("(.*)(.{1}$)", rmve_2nd_grp, my_str)
print(result)

输出:

Python Strin