String Format

Dark Kai

Freshman
Joined
Sep 3, 2003
Messages
48
Hi, need some help from u guys/gals out there.
I've this code :

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

I'm 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"

Visual Basic:
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

Hope this helps!

Andreas
 
Hamburger1984 said:
you could also use Regular Expressions

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

Visual Basic:
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

Hope this helps!

Andreas

Nice solution... but... Ouch! What a way to start a Monday morning...
 
Hamburger1984 said:
why "ouch"?!?!? you didn't 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(!) (that's why there's 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
 
samsmithnz said:
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 :( .
 
Dark Kai said:
So there are no way to do the formating that i wanted :(.
[...]
dim Test as string = "ThisIsMyString"
test.format("{0:####'-'##'-'##'-'######}".test)

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

no I don't 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 you've gotta go the "hard" Regex-Way...:rolleyes:

Andreas
 
Back
Top