c#从字符串中删除所有空格[复制]

问题描述 投票:0回答:4

这个问题在这里已有答案:

我有一个sql字符串检索三列的数据,我不希望这些列之间有任何空格。我想删除列之间的空格。

 string stringSql = " SELECT distinct  " +
                   "'" + comboBox6.Text + "' as RecordType" +
                 " , left([Claimant Name] +' ',29) " +
                " , left([Claimant Address1] +' ',29)  " +
                " , left([Claimant Address2] +' ',29) as ClaimantAddress2 " +

我的客户要求就像

**1xyz1dundas**

我的输出就像

**1 xyz 1 dundas**
sql-server
4个回答
4
投票

利用替换

stringSql .Replace("  ", string.empty);

在sql中

 SELECT REPLACE(stringSql, ' ', '')

2
投票

如果你想删除空格(不只是普通空格' ',但是,比如说,表格'\t')你可以使用LINQ:

  stringSql = new String(stringSql.Where(x => !Char.IsWhiteSpace(x)).ToArray());

1
投票

在SQL-Server上:

SELECT REPLACE(yourField, ' ', '')

0
投票

MSSQL服务器:

RTRIM(LTRIM(a))

Oracle,MySQL,SQL Server 2012:TRIM功能

string stringSql = " SELECT distinct  " +
                   "'" + comboBox6.Text + "' as RecordType" +
                 " ,  RTRIM(LTRIM(left([Claimant Name] +' ',29))) " +
                " ,  RTRIM(LTRIM(left([Claimant Address1] +' ',29)))  " +
                " ,  RTRIM(LTRIM(left([Claimant Address2] +' ',29))) as ClaimantAddress2 "
© www.soinside.com 2019 - 2024. All rights reserved.