vb.net - 删除新行上的空格,但保留该行
问题描述
我希望从不包含文本的行中删除空格,但不删除该行。由于空格字符很难识别,我将用“#”(hastag 字符)替换空格字符以更容易地展示示例。字符串看起来像这样:
"This is
########a long string
with many lines
#######
and the above is empty
####this is empty
#############
#######hello"
我希望输出将删除仅包含空格字符的行上的空格。我仍在使用“#”(hastag 字符)来展示空间。最终输出应如下所示:
"This is
########a long string
with many lines
and the above is empty
####this is empty
#######hello"
如果没有将主题标签字符用作空格字符,则预期输出应如下所示:
"This is
a long string
with many lines
and the above is empty
this is empty
hello"
因此,为了完全澄清,我希望删除不包含文本的行上的空格字符,但不删除该行。
解决方案
将您的示例与 octothorpes (# 的另一个名称)一起使用并用代码中的空格替换它们,我们可以使用该String.IsNullOrWhiteSpace
函数检查这些行并将它们替换为空字符串:
Module Module1
Sub Main()
Dim s = "This is
########a long string
with many lines
#######
and the above is empty
####this is empty
#############
#######hello"
s = s.Replace("#", " ")
Dim t = String.Join(vbCrLf, s.Split({vbCrLf}, StringSplitOptions.None).
Select(Function(a) If(String.IsNullOrWhiteSpace(a), "", a)))
Console.WriteLine(t)
Console.ReadLine()
End Sub
End Module
输出:
This is
a long string
with many lines
and the above is empty
this is empty
hello
推荐阅读
- templates - Concourse-CI 模板文件
- unix - 当我使用 telnet 协议连接到服务器时,我收到错误“无法打开与主机的连接,连接失败”
- spring - ApplicationEventPublisher 在使用 WebApplicationContext 的过滤器中不能用作 Bean
- excel - Excel sumif 返回大数字而不是零
- neural-network - 哪种神经网络架构可以为文本分类提供更好的准确性?
- ruby - 使用 Ruby 进行多重排序
- c# - Deploy a webiste on IIS and reuse the automatically created DB from the project
- python - 在scrapy脚本中使用python中的strip方法删除空格,避免提取中没有空格的方法
- algorithm - 预购插入
- android - 在同一个 android 应用程序中为不同域维护不同的工作流程