Dark Kai Posted December 15, 2003 Posted December 15, 2003 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. Quote
hog Posted December 15, 2003 Posted December 15, 2003 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? Quote My website
Hamburger1984 Posted December 15, 2003 Posted December 15, 2003 you could also use Regular Expressions you find the classes you need in the Namespace "System.Text.RegularExpressions" 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 Quote
samsmithnz Posted December 15, 2003 Posted December 15, 2003 you could also use Regular Expressions you find the classes you need in the Namespace "System.Text.RegularExpressions" 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... Quote Thanks Sam http://www.samsmith.co.nz
Hamburger1984 Posted December 15, 2003 Posted December 15, 2003 Nice solution... but... Ouch! What a way to start a Monday morning... 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 Quote
samsmithnz Posted December 15, 2003 Posted December 15, 2003 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 :) Quote Thanks Sam http://www.samsmith.co.nz
Dark Kai Posted December 16, 2003 Author Posted December 16, 2003 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) Quote
Hamburger1984 Posted December 16, 2003 Posted December 16, 2003 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 Quote
Hamburger1984 Posted December 16, 2003 Posted December 16, 2003 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.. ;) Quote
Dark Kai Posted December 16, 2003 Author Posted December 16, 2003 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 :( . Quote
Hamburger1984 Posted December 16, 2003 Posted December 16, 2003 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.