How to find out if character is in caps or not?

  • Thread starter Thread starter Martin Ho
  • Start date Start date
M

Martin Ho

What I really need is a short simple solution for this scenario:

Imagine word like this:
"TheSimpleSolution"
I need to turn it to:
"The Simple Solution"

I need a solution, something which would turn connected words and
separate them using spaces. Basically, a script which would add the
space ahead of each capitalized character.

Please, be so good and let me know, I know I am pretty much asking for
ready script, but whatever you mention, whatever help or sugestion
will be much appreciated.

Thanks a lot.
Martin
 
Here she goes Martin...

Public Function SplitWords(ByVal s as string) as string
Dim strAns as string
Dim c as char

For Each c in s
If Char.IsUpper(c) Then
strAns += " "
End If

strAns += c
Next

Return strAns
End Function

-Fabricio
(e-mail address removed)
 
Fabricio,

Seeing your code I am sure you know the Stringbuilder and just are thinking
something as "why use that for such a simple string".

I have seen that even for short strings the stringbuilder gives beter result
and sometimes OPs are giving a simple sample to use it for long strings.

For Martin, with the stringbuilder the code from Fabricio can be.

Public Function SplitWords(ByVal s as string) as string
Dim strAns as new system.text.stringbuilder
Dim c as char
For Each c in s
If Char.IsUpper(c) Then
strAns.append( " ")
End If
strAns.append(c)
Next
Return strAns.tostring
End Function
Cor
 
* (e-mail address removed)-spam.invalid (Martin Ho) scripsit:
Imagine word like this:
"TheSimpleSolution"
I need to turn it to:
"The Simple Solution"

I need a solution, something which would turn connected words and
separate them using spaces. Basically, a script which would add the
space ahead of each capitalized character.

\\\
Imports System.Text
..
..
..
MsgBox(ExpandCaps("ThisIsVeryGood"))
..
..
..
Public Function ExpandCaps(ByVal Text As String) As String
Dim Buffer As New StringBuilder(CInt(Text.Length * 1.3))
For Each c As Char In Text
If Char.IsUpper(c) Then
Buffer.Append(" "c)
End If
Buffer.Append(c)
Next c
Return Buffer.ToString()
End Function
///
 
I don't use the Stringbuilder object much... actually I've never used it.
When you say it "gives better results" do you mean performance. Is it more
efficient than a built-in string object. Also, how's its overhead compared
to the string object? Thanks for the info Cor.

-Fabricio
(e-mail address removed)

P.S. What's OP?
 
* "=?Utf-8?B?RmFicmljaW8=?= said:
I don't use the Stringbuilder object much... actually I've never used it.
When you say it "gives better results" do you mean performance. Is it more
efficient than a built-in string object.

Strings in .NET are immutable, contantenating two strings will create a
new string object that consists of the concatenated strings. 'StringBuilder'
overcomes this issue.

I suggest to take a look at the documentation for the 'System.String'
and 'System.Text.StringBuilder' classes.
 
Fabricio,

I can can add a lot to the message from Herfried,

What he not explained was OP which means Original Poster

However when you have still question, feel free to ask.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top