String Format

Dark Kai

Well-known member
Joined
Sep 3, 2003
Messages
48
Hi, need some help from u guys/gals out there.
Ive this code :

dim Test as string = "ThisIsMyString"
test.format("{0:####-##-##-######}".test)

Im trying to format it so that the output will be = "This-Is-My-String"

this line of code will only work if i change the type of test to numaric.

Thanks in advance.
 
Not sure if this helps but if the string you will be formatting is always going to be camel cased you could use the string.split method?
 
you could also use Regular Expressions

you find the classes you need in the Namespace "System.Text.RegularExpressions"

[VB]Imports System.Text.RegularExpressions

Public Sub Test()
Dim regex = New Regex( "(?<word>[A-Z][^A-Z\s\n]+)", _
RegexOptions.None)
Dim testText as String = "ThisIsSomeCamelCaseText"
Dim result as String = regex.Replace(testText,"${word}-")
result = result.SubString(0,result.Length-1)
Console.WriteLine(result)
End Sub[/VB]

Hope this helps!

Andreas
 
Originally posted by Hamburger1984
you could also use Regular Expressions

you find the classes you need in the Namespace "System.Text.RegularExpressions"

[VB]Imports System.Text.RegularExpressions

Public Sub Test()
Dim regex = New Regex( "(?<word>[A-Z][^A-Z\s\n]+)", _
RegexOptions.None)
Dim testText as String = "ThisIsSomeCamelCaseText"
Dim result as String = regex.Replace(testText,"${word}-")
result = result.SubString(0,result.Length-1)
Console.WriteLine(result)
End Sub[/VB]

Hope this helps!

Andreas

Nice solution... but... Ouch! What a way to start a Monday morning...
 
Originally posted by samsmithnz
Nice solution... but... Ouch! What a way to start a Monday morning...

why "ouch"?!?!? you didnt see that messy code I had to tidy up this morning for one of my co-workers.... THAT was a bad way to start a Monday morning :( :p ;) :D
 
Originally posted by Hamburger1984
why "ouch"?!?!? you didnt see that messy code I had to tidy up this morning for one of my co-workers.... THAT was a bad way to start a Monday morning :( :p ;) :D

Not messy... but its hard for me to look at regular expressions so early in the morning, i hate them :)
 
Wowwww......The code works....
but I think you are only checking the char case rite ??
hehehehhehe do the same but instead of char case we check number of char:D

Thanks a lot Hamburger1984.
BTW could u explain a bit more bout this line of code Regex( "(?<word>[A-Z][^A-Z\s\n]+)", _
RegexOptions.None)
 
RegularExpressions are used to find matches in strings. they use a special pattern-syntax to find those matches (you could compare them to those patterns you use to find files in Windows - "*.txt" -> find every file with the extension ".txt")... the pattern I used means the following:

"(?<word>...)" - is a named group (you could omit this - I just needed this for the replace-statement later...

"[A-Z]" - matches one(!) uppercase Letter

"[^A-Z\s\n]+" - matches anything except(!) (thats why theres a "^") any uppercase Letter "A-Z", a whitespace "\s" or a newLine-char "\n".... the "+" just tells the how many of those letters should be matched - in this case at least one. ("*" would match any number 0-n...)

Hope this helps!

Andreas
 
Originally posted by samsmithnz
Not messy... but its hard for me to look at regular expressions so early in the morning, i hate them :)

well when I started using them I also hated them.. but since I only use them for quite easy purposes (and not at work) I started likeing them.. ;)
 
Thanks for the great info Hamburger1984 and also not forgeting others that have contributed.

So there are no way to do the formating that i wanted :( .
 
Originally posted by Dark Kai
So there are no way to do the formating that i wanted :(.
[...]
dim Test as string = "ThisIsMyString"
test.format("{0:####-##-##-######}".test)

Im trying to format it so that the output will be = "This-Is-My-String"

no I dont think you can to it as you wanted to.... the String.Format-Method is as far as I know only for formatting Numbers (Integer,Single,Double etc) but not for applying new Formats to strings... I guess youve gotta go the "hard" Regex-Way...:rolleyes:

Andreas
 
Back
Top