Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted
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?
My website
Posted

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

Posted
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...

Posted
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

Posted

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)

Posted

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

Posted
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.. ;)

Posted
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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...