LINQ 中的字符替换?

发布于 2024-11-03 05:43:41 字数 750 浏览 0 评论 0原文

我试图整理出一个解决方案: 在驼峰式标记上的单词之间插入空格

本质上,他想将“ThisIsATest”变成“This Is A Test”。我想,“哦,这很简单,我可以用 LINQ 来完成”,但我却遇到了困难。

有人可以帮助我吗?

Dim s As String = String.Join("", From myChar As Char In myStr _
                                  Select If(Char.IsUpper(myChar), (" " & myChar).ToString, myChar.ToString))

这是我开始走的路,但我很难将结果转化为我可以使用的东西。我什至添加了 .ToString 来尝试获取字符串数组,但我仍然收到错误。

无法转换类型的对象 'WhereSelectEnumerableIterator`2[System.Char,System.String]' 输入“System.String[]”。

我相信这意味着我得到了 System.Char、System.String 的集合,而不仅仅是我想要的 System.String。

我做错了什么?

I was trying to put together a solution to:
Insert spaces between words on a camel-cased token

Essentially, he wants to turn 'ThisIsATest' into 'This Is A Test'. I thought, 'Oh, that's easy, I can do it with LINQ' but I struggled with it.

Can someone help me?

Dim s As String = String.Join("", From myChar As Char In myStr _
                                  Select If(Char.IsUpper(myChar), (" " & myChar).ToString, myChar.ToString))

Is the path I started to go down, but I'm having trouble getting the results into something I can work with. I even added the .ToString to try and get back an array of Strings, but I'm still getting an error.

Unable to cast object of type
'WhereSelectEnumerableIterator`2[System.Char,System.String]'
to type 'System.String[]'.

I believe that means I'm getting a collection of System.Char, System.String instead of just a System.String like I want.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

马蹄踏│碎落叶 2024-11-10 05:43:41

您可以使用正则表达式并拆分大写字符。

Dim myString as string = "ThisIsATest"
Dim outStr As String = Regex.Replace(myString,"[A-Z]"," $0")

区分大小写的替换将找到每个大写字符并在该字符前面插入一个空格。

You can use RegEx and split on the uppercase characters.

Dim myString as string = "ThisIsATest"
Dim outStr As String = Regex.Replace(myString,"[A-Z]"," $0")

The case-sensitive replace will locate every uppercase character and insert a space in front of the character.

清音悠歌 2024-11-10 05:43:41

您需要将第二个参数转换为 String[]。您可以使用.ToArray()

Dim outStr As String = String.Join("", (From myChar As Char In myStr _
                                               Select If(Char.IsUpper(myChar), (" " & myChar).ToString, myChar.ToString)).ToArray())

You need to convert the second parameter to String[]. You can use .ToArray()

Dim outStr As String = String.Join("", (From myChar As Char In myStr _
                                               Select If(Char.IsUpper(myChar), (" " & myChar).ToString, myChar.ToString)).ToArray())
却一份温柔 2024-11-10 05:43:41

C#:

var myStr = "TestString";
var outStr = string.Concat(myStr.Select(c => char.IsUpper(c) ? " " + c : c.ToString()));

自动翻译为 VB.NET:

Dim myStr = "TestString"
Dim outStr = String.Concat(myStr.[Select](Function(c) If(Char.IsUpper(c), " " & Convert.ToString(c), c.ToString())))

C#:

var myStr = "TestString";
var outStr = string.Concat(myStr.Select(c => char.IsUpper(c) ? " " + c : c.ToString()));

auto-translated to VB.NET:

Dim myStr = "TestString"
Dim outStr = String.Concat(myStr.[Select](Function(c) If(Char.IsUpper(c), " " & Convert.ToString(c), c.ToString())))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文