【发布时间】:2013-06-24 04:39:30
【问题描述】:
我在进行简单的搜索和替换时遇到了很多麻烦。我尝试了提供的解决方案 How do I remove white space in a Perl string? 但无法打印。
这是我的示例代码:
#!/usr/bin/perl
use strict;
my $hello = "hello world";
print "$hello\n"; #this should print out >> hello world
#now i am trying to print out helloworld (space removed)
my $hello_nospaces = $hello =~ s/\s//g;
#my $hello_nospaces = $hello =~ s/hello world/helloworld/g;
#my $hello_nospaces = $hello =~ s/\s+//g;
print "$hello_nospaces\n"
#am getting a blank response when i run this.
我尝试了几种不同的方法,但都无法做到。
我的最终结果是在 linux 环境中自动移动文件的某些方面,但有时文件名称中有空格,所以我想从变量中删除空格。
【问题讨论】:
-
另外,Linux 和许多其他操作系统完全支持文件名中的空格,只要开发人员小心。删除空格可能会产生不利影响,例如在文件“helloworld.txt”和“hello world.txt”之间产生歧义。
-
感谢大家的快速回复。我一直在尝试改变 s 和 g 之间的语法。抱歉,我不能投票,我没有 15 个代表点
标签: string perl variables whitespace