Forum Moderators: open

Message Too Old, No Replies

break a string apart

         

hal12b

3:57 pm on Jun 29, 2010 (gmt 0)

10+ Year Member



How can I break a database value into three separate values onto a page. For example, I have 6/23/10 and want each number separately

The 6 the 23 and the 10.

Thanks,
Hal

marcel

4:33 pm on Jun 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use String.Split [msdn.microsoft.com] for that.

Your example string looks very much like a date, are you saving dates as a varchar?

hal12b

11:50 am on Jun 30, 2010 (gmt 0)

10+ Year Member



Actually nvarchar and yes, it's a date. On the insert page it is broken up into three fields (day, month, and year), then I combine them into one field and save them, but now I have a page where an admin can edit the date, so I need to break them back into three fields on page load.

marcel

12:03 pm on Jun 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would strongy advise you to save the Date as a DateTime (or Date) object in SQL server, it will give you much more power and flexibility when sorting, filtering, selecting date ranges etc.

hal12b

2:49 pm on Jun 30, 2010 (gmt 0)

10+ Year Member



I would have, but I want to save the dates such as 06/09/10 for example. When I used the date field, it kept saving it as x/x/xx. Since they are listed, they look cleaner like

06/09/10
06/09/10
06/09/10
06/09/10
06/09/10

instead of

6/9/10
11/10/10
11/10/10
6/9/10
6/9/10
6/9/10
11/9/10

etc....

I'm just a novice, so i am sure there is something I could do to clean this up, but for every question I post here, I have 30 I don't post!

marcel

3:07 pm on Jun 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's the beauty of the .NET framework, Dates and Times are really well thought out.

You can Format strings like this:
' Create Date 6 september 2010
Dim dt As DateTime = New DateTime(2010, 9, 6)
' Convert tyo String
Dim sdt As String = dt.ToString("dd-MM-yy")
' Output should now be "06-09-10"
Response.Write(sdt)

LifeinAsia

3:46 pm on Jun 30, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



When I used the date field, it kept saving it as x/x/xx.

That's just the way the data is presented to you through whatever client you're using to view the data. How it actually saves the data is completely different from what you see.

As marcel points out, you have a lot of flexibility in the way you format the data.

Saving date data as strings is evil and should be avoided at all costs. It will ALWAYS come back to bite you at some point.

Besides the sorting issue ('06/01/10' is sorted before '07/01/09' as a character string, yet June 1, 2010 comes AFTER July 1, 2009), what happens if someone else comes in later and doesn't understand how you're storing the date. To you, '06/01/10' may be June 1, 2010 (U.S. format), but Europeans would read it as January 6, 2010, and a lot of Asians would read it as January 10, 2006.

hal12b

3:59 pm on Jun 30, 2010 (gmt 0)

10+ Year Member



OK thanks for the input and quick responses. Let me see what I can do, probably not much hahha. I'll give it a shot.

hal12b

4:45 pm on Jun 30, 2010 (gmt 0)

10+ Year Member



I am stuck here. With this example below, how could I assign each fruit a unique variable so I can do a response.write on the page to see if it works?


Dim TestString As String = "apple pear banana "
Dim TestArray() As String = Split(TestString)
' TestArray holds {"apple", "", "", "", "pear", "banana", "", ""}
Dim LastNonEmpty As Integer = -1
For i As Integer = 0 To TestArray.Length - 1
If TestArray(i) <> "" Then
LastNonEmpty += 1
TestArray(LastNonEmpty) = TestArray(i)
End If
Next
ReDim Preserve TestArray(LastNonEmpty)
' TestArray now holds {"apple", "pear", "banana"}

hal12b

4:48 pm on Jun 30, 2010 (gmt 0)

10+ Year Member



just got it -->

Response.Write(TestArray(0))
Response.Write(TestArray(1))
Response.Write(TestArray(2))

I love when I post a question and solve it 30 seconds later.

hal12b

4:57 pm on Jun 30, 2010 (gmt 0)

10+ Year Member



maybe I spoke too soon. With the code below when I do a response.write I get the whole date such as 4/7/2010. I am including the "/" below, so I am not sure what I did wrong. Anybody?


'need to break date apart for three drop downs on page
Dim TestString As String = Utils.CheckForNull_Text(dr.Item("DATE_DISPLAY"), "/")
Dim TestArray() As String = Split(TestString)
' TestArray holds {"apple", "", "", "", "pear", "banana", "", ""}
Dim LastNonEmpty As Integer = -1
For i As Integer = 0 To TestArray.Length - 1
If TestArray(i) <> "" Then
LastNonEmpty += 1
TestArray(LastNonEmpty) = TestArray(i)
End If
Next
ReDim Preserve TestArray(LastNonEmpty)
' TestArray now holds {"apple", "pear", "banana"}

Response.Write(TestArray(0))

LifeinAsia

4:59 pm on Jun 30, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Yep. Often your brain has the answer locked away subconsciously. Getting the question out verbally or through writing it down releases the trapdoor and lets it sneak its way up to the conscious level.

They you slap yourself with a big D'oh! and EVERYTHING recedes back to subconscious (or unconscious if the slap is hard enough). :)

marcel

5:03 pm on Jun 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dim TestArray() As String = Split(TestString)

should be
Dim TestArray() As String = TestString.Split("/")


What is the slash doing here?
Utils.CheckForNull_Text(dr.Item("DATE_DISPLAY"), "/") 


It would be much more readable with a Date object though, you could have something like this:
Dim myDate As DateTime = dr.Item("DATE_DISPLAY")
dim myYear as Integer = myDate.Year
dim myMonth as Integer = myDate.Month
dim myDay as Integer = myDate.Day

hal12b

6:12 pm on Jun 30, 2010 (gmt 0)

10+ Year Member



The slash represents where to break it apart. At least that's what worked on the other samples I tried. If I did this instead it will break it at each blank space and each response.write will write each word separately. So if the title was "The Dark moon", then

TestArray(0) = The
TestArray(1) = Dark
TestArray(2) = Moon


'need to break date apart for three drop downs on page
Dim TestString As String = Utils.CheckForNull_Text(dr.Item("TITLE"), "")
Dim TestArray() As String = Split(TestString)
' TestArray holds {"apple", "", "", "", "pear", "banana", "", ""}
Dim LastNonEmpty As Integer = -1
For i As Integer = 0 To TestArray.Length - 1
If TestArray(i) <> "" Then
LastNonEmpty += 1
TestArray(LastNonEmpty) = TestArray(i)
End If
Next
ReDim Preserve TestArray(LastNonEmpty)
' TestArray now holds {"apple", "pear", "banana"}

Response.Write(TestArray(0))
Response.Write(TestArray(1))
Response.Write(TestArray(2))


So the question remains how do I pull a date out of the database (it is a datetime field) and use the slash as the way to break it up?

hal12b

6:19 pm on Jun 30, 2010 (gmt 0)

10+ Year Member



Marcel -
Your code did work and YES it was much easier than what I was trying to do!

LifeinAsia

6:29 pm on Jun 30, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



use the slash as the way to break it up?

That's the beauty of date data- you don't need to do any parsing like that. There are built in functions to return the specific date (or time) part that you need- see the end of marcel's previous post.