2

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?

Community
  • 1
  • 1
Rob P.
  • 14,921
  • 14
  • 73
  • 109
  • Your question is a related to http://stackoverflow.com/questions/323314/best-way-to-convert-pascal-case-to-a-sentence. There are quite a few different options there to solve your problem. – alex Apr 26 '11 at 21:35
  • Thanks everyone. Seeing it done correctly, it makes sense. – Rob P. Apr 26 '11 at 21:44

3 Answers3

5

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.

Leons
  • 2,679
  • 1
  • 21
  • 25
  • +1: Although not LINQ, this is a much cleaner. I would take this over a LINQ solution for readability. – IAbstract Apr 26 '11 at 21:48
  • @Chuck Savage - that is a good question. It depends on what the purpose is and is definitely something to consider during an implementation. – Leons Apr 26 '11 at 22:52
2

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())
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • 1
    You have a space in front of the test string. – IAbstract Apr 26 '11 at 21:49
  • 1
    @IAbstract - My plan was to .Trim the whole thing rather than try to deal with the edge case of the initial capital letter; but you are 100% correct. – Rob P. Apr 26 '11 at 21:50
1

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())))
max
  • 33,369
  • 7
  • 73
  • 84
  • 1
    I haven't looked at the C# solution, but the VB.Net solution is returning: System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Char,System.String ] – IAbstract Apr 26 '11 at 21:51