LINQ 中的字符替换?
我试图整理出一个解决方案: 在驼峰式标记上的单词之间插入空格
本质上,他想将“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用正则表达式并拆分大写字符。
区分大小写的替换将找到每个大写字符并在该字符前面插入一个空格。
You can use RegEx and split on the uppercase characters.
The case-sensitive replace will locate every uppercase character and insert a space in front of the character.
您需要将第二个参数转换为 String[]。您可以使用
.ToArray()
You need to convert the second parameter to String[]. You can use
.ToArray()
C#:
自动翻译为 VB.NET:
C#:
auto-translated to VB.NET: