Link to home
Start Free TrialLog in
Avatar of kasowitz
kasowitz

asked on

Change Text Arrangement? LEN?

Hi,

I knew how to do this at one time, but it now escapes me in VB.NET.

I have a text string "20060312" , when I stick this string into a textbox I want to rearrange the same text to look like "03122006" .

Any suggestion/help as to what is the quickest and most proper way to do this ? Does it involve using LEN statements? Thanks for all your help !
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

Hi kasowitz,

if your string is always 8 characters as in your coment you could use

mystring.substring(5,4) & mystring.substring(1,4)

hope this helps a bit
bruintje
Avatar of kasowitz
kasowitz

ASKER

Thanks for your reply bruintje,

I am having trouble using your example code..I get the following error:

startIndex cannot be larger than length of string.
Parameter name: startIndex


Any idea what Im doing wrong ?

if it were a date datatype you could use the format function:

Format(date, "mmddyyyy")
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
substring is zero-based.  change it to mystring.substring(4,4) & mystring.substring(0,4)
You can regex the string too...

        Dim DateRegEx As System.Text.RegularExpressions.Regex = _
            New System.Text.RegularExpressions.Regex("^(?<Year>([0-9]{4}))(?<Day>([0-9]{2}))(?<Month>([0-9]{2}))$")

        With DateRegEx.Match(TextBox1.Text)
            TextBox1.Text = .Groups("Day").Value & .Groups("Month").Value & .Groups("Year").Value
        End With

Might be a bit overkill though
i admit cooking and answering questions here shouldn't go together :)
Hmm, still not working for me. Same error.
I am actually grabbing my string from a SQL Data reader, and unfortunately the field type is text and not date. Here is my code...any ideas ?

Admit = myReader2("Field9").ToString.Substring(4, 4) & myReader2("Field9").ToString.Substring(0, 4)

textbox1.text = admit


I get the error:
startIndex cannot be larger than length of string.
Parameter name: startIndex
Sorry guys !...that worked perfect ! The current record I was on had that field as blank. Doh ! Thanks!
Hi ZeonFlash;

If you wanted to use Regex I think this is a better Regex solution.

        mystring = System.Text.RegularExpressions.Regex.Replace(mystring, _
            "^\s*?(\d{4})(\d\d)(\d\d)\s*?$", "$2$3$1")

Fernando
Hi kasowitz;

Can you please post a comment in the https://www.experts-exchange.com/Community_Support/ and have the question reopened and then award the points to bruintje who had the solution you used. you will have to reference this question, https://www.experts-exchange.com/questions/21833535/Change-Text-Arrangement-LEN.html.

Thank you;

Fernando