【问题标题】:Remove white spaces from string and convert into title case从字符串中删除空格并转换为标题大小写
【发布时间】:2014-12-13 01:17:54
【问题描述】:

这是我想要输出的示例...

我有这个输入 = "自动发送电子邮件"

但我想要这个 输出 = "AutomaticEmailSent"

提前致谢!

【问题讨论】:

  • 这是在 C# 还是 SQL 中?
  • 明白,有什么尝试吗?
  • 到目前为止您尝试了什么?此外,您想在 c# 或 sql 中完成此操作吗?

标签: sql


【解决方案1】:

使用TextInfo.ToTitleCase

// Defines the string with mixed casing. 
string myString = "Automatic email sent";

// Creates a TextInfo based on the "en-US" culture.
TextInfo myTI = new CultureInfo("en-US",false).TextInfo;

// Changes a string to titlecase, then replace the spaces with empty
string outputString = myTI.ToTitleCase(myString).Replace(" ", "");

【讨论】:

  • 问题更新为sql 标签,c# 不再相关。
【解决方案2】:

this answer 窃取一个函数,该函数接受文本输入并使其大小写正确(也称为标题大小写):

create function ProperCase(@Text as varchar(8000))
returns varchar(8000)
as
begin
   declare @Reset bit;
   declare @Ret varchar(8000);
   declare @i int;
   declare @c char(1);

   select @Reset = 1, @i=1, @Ret = '';

   while (@i <= len(@Text))
    select @c= substring(@Text,@i,1),
               @Ret = @Ret + case when @Reset=1 then UPPER(@c) else LOWER(@c) end,
               @Reset = case when @c like '[a-zA-Z]' then 0 else 1 end,
               @i = @i +1
   return @Ret
end

那么你可以把这个功能和REPLACE结合起来:

SELECT REPLACE(dbo.ProperCase(column), ' ', '')
FROM MyTable

【讨论】:

  • 非常感谢!
  • 不要忘记为您认为有用的答案投票,如果其中一个解决了您的问题,请将其标记为答案。
【解决方案3】:

SQL 服务器

declare @value varchar(64) =  rtrim(' ' +  'Automatic email sent')
;with t(n) as (
    select n = charindex(' ', @value, 0)
    union all 
    select n = charindex(' ', @value, n + 1)
    from t
    where n > 0
)
select @value = stuff(@value, n + 1, 1, upper(substring(@value, n + 1, 1))) from t where n > 0
select replace(@value, ' ', '')

【讨论】:

    猜你喜欢
    • 2011-05-01
    • 2010-11-15
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多