Saturday, March 31, 2012

Separate out a string, and ...

What can I do this in C# or VB.NET?

Let say I have a string stored in dbase, say stringfield, and the string is:

fire, water, electricity, milk, ...

I want to separate them out, and do a hyperlink for them, like the folowing:

fire: hyperlink is http://localhost/findwords.apsx?word=fire
water: hyperlink is http://localhost/findwords.apsx?word=water
electricity: hyperlink is http://localhost/findwords.apsx?word=electricity
milk: hyperlink is http://localhost/findwords.apsx?word=milk

Or is this better ...

fire, 654, water, 897, electricity, 512, milk, 770, ...

and when I click the word, hyperlink is as follows:

fire: hyperlink is http://localhost/findwords.apsx?wordid=654
water: hyperlink is http://localhost/findwords.apsx?wordid=897
...

How? Thanks.
Any good reference for this? Is this relates to regular expression?
Thanks.A String object has a handy Split function. So, you can split your String at each comma.

Like this:

<%@. Page Language="VB" %>
<script runat="server"
Sub Page_Load( ByVal sender As Object, ByVal eArgs As EventArgs )

' get string from database
Dim ValuesAsString As String = "fire, water, electricity, milk"

' remove the spaces
ValuesAsString = ValuesAsString.Replace( " ","" )

' split the result at the commas
Dim ValuesAsArray As Array
ValuesAsArray = ValuesAsString.Split( "," )

' bind the result to whatever you like
' being lazy, i'm just using a simple repeater
ValuesRepeater.DataSource = ValuesAsArray
ValuesRepeater.DataBind()

End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:Repeater id="ValuesRepeater" runat="server">
<ItemTemplate>
<a href='findwords.apsx?word=<%# Container.DataItem() %>'><%# Container.DataItem() %></a>
<br />
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</html>

I hope this helps answer your question.

However, I am sure it would be a much better idea to "split" the string before storing it, and therefore save the words to the database individually.

As to whether you should use ID numbers or the words themselves is a matter of "design". There's no right or wrong. Having said that, I would use IDs. Words might have accents or hypens or apostrophes ... each of which would be awkward or illegal in a query string.

I hope this helps.
Thanks for replying, and I am thinking of using the second method because some words might have more than one meaning, and I can use different ID to separate them out, for example fire, 654, fire, 655, water, 897, electricity, 512, milk, 770, ...

Hence
the url http://localhost/findwords.apsx?wordid=654 and 655 refer to same word, fire, but different meaning ...

So, the question is HOW?

I found source codes in PHP, as follows:

$LOV = split(";", ";All;0;No;1;Yes");

if(sizeof($LOV)%2 != 0)
$array_length = sizeof($LOV) - 1;
else
$array_length = sizeof($LOV);

for($i = 0; $i < $array_length; $i = $i + 2)
{
if($LOV[$i] == $fldis_recommended)
$option="<option SELECTED value=\"" . $LOV[$i] . "\">" . $LOV[$i + 1];
else
$option="<option value=\"" . $LOV[$i] . "\">" . $LOV[$i + 1];

echo $option;
}

How to translate this into VB.NET?

Thanks.
I'm a C# coder, but am learning VB and so am using this as a learning opportunity. I'm not 100% sure that the translation is correct syntactically (I'm sure someone will correct it if it isn't), however the structure is correct.

Dim LOV As String [] = ";All;0;No;1;Yes".Split(";")
Dim option As String
Dim array_length As Integer

If Not ( ( LOV.Length Mod 0 ) = 0 ) Then

array_length = LOV.Length - 1

Else

array_length = LOV.Length

End If

Dim i As Integer

For i = 0 To i < array_length Step 2

If LOV(i) = fldis_recommended Then ' Don't know where fldis_recommended comes from

option = "<option SELECTED value=" & vbQuote & LOV(i) & vbQuote & ">" & LOV(i+1)

Else

option = "<option value=" & vbQuote & LOV(i) & vbQuote & ">" & LOV(i+1)

End If

Response.Write(option) ' or however you are sending it to the Response object

Next


> I'm not 100% sure that the translation is correct syntactically (I'm sure someone will correct it if it isn't), however the structure is correct

The syntax is a little wrong, which I've amended with comments below.

(May I ask why you're learning VB?)

Dim LOV As String() = "0;All;1;No;2;Yes".Split(";")'<-- String[] to String(), also changed the string itself
Dim opt As String'<-- option is a reserved word, so changed to opt

Dim array_length As Integer
If Not ( ( LOV.Length Mod 2 ) = 0 ) Then'<-- Changed to Mod 2
array_length = LOV.Length - 1
Else
array_length = LOV.Length
End If

Dim i As Integer
If (array_length > 0) Then
For i = 0 To array_length-1 Step 2'<-- removed "i <", changed array_length-1
If LOV(i) = fldis_recommended Then' Don't know where fldis_recommended comes from
opt = "<opt SELECTED value=""" & LOV(i) & """>" & LOV(i+1)'<-- changed vbQuote to "" (escaped quotation)
Else
opt = "<opt value=""" & LOV(i) & """>" & LOV(i+1)
End If
Response.Write(opt)' or however you are sending it to the Response object
Next
End If


> (May I ask why you're learning VB?)

I just think that it's a good idea to know VB as well as C# and there may be a situation when I need to code in VB.

0 comments:

Post a Comment