How to split the words in ASP.net

sameera

Member
Joined
Jun 16, 2005
Messages
20
Programming Experience
Beginner
How to split the words in ASP.net - solved

Hi, I need a way of spliting words from a text box in ASP.net
Ex: If a user types "Good Morning" in the text box i need to get it seperatly as "good" and "Morning" So can anyone tell me how to do this (I have already post this once)

Actually i want to search some keywords with my web form

regards,

sameera:)
 
Last edited:
If you want to separate all the individual words in a string you can use myString.Split(" "c) to get them as a string array. Note that if there are any double spaces then you will get an empty string as an element of the array. If you want to find certain substrings you can use IndexOf, LastIndexOf or perhaps regular expressions through the Regex class.
 
Well, even if i don't recommend a book reading or another site solution this time i will make digression :D . Find a good atricle/tutorials that describes all methods/properties about string manipulation cuz we can solve your current issue but new one will come very soon ...

ok let me try to give some tips:

1st you can use Compare method to find if user typed exactely what you want.

PHP:
 TextBox1.Text = String.Compare("good morning", "Good Morning") 
 
If TextBox1.Text = -1 Then
 
MessageBox.Show("String 1 is less than string 2")
 
End If
 
If TextBox1.Text = 0 Then
 
MessageBox.Show("String 2 is equal to string 1")
 
End If
 
If TextBox1.Text = 1 Then
 
MessageBox.Show("String 1 is greater than string 2")
 
End If
 
If TextBox1.Text = "" Then
 
MessageBox.Show("String 1 and / or string two is null")
 
End If


You can use IndexOf method to find if typed string exist at all:

PHP:
 Dim n As Integer 
 
TextBox1.Text = "Good Morning Vietnam"
 
TextBox2.Text = "Morning"
 
n = TextBox1.Text.IndexOf(TextBox2.Text)
 
If n > 0 Then
 
MessageBox.Show("Word was found, it starts as " & n & "th character in the string.")
 
Else
 
MessageBox.Show("Sorry, word wasn't found")
 
End If



and finally as jmcilhinney suggested you can use Split function that is most reasonable in this case:

PHP:
Dim s As String = TextBox1.Text 'i assume text will be "Good Morning Vietnam" as well
 
Dim n() As String = s.Split(" ") 
 
MessageBox.Show(n(0)) 'will return "Good"
 
MessageBox.Show(n(1)) 'will return "Morning"
 
'and so on

Cheers ;)
 
Thanx Again - Problem solved

:) Thanks for you all, My problem was solved and this is the second time (Actually i found this site really helpful as previously also i have solved my vb.net problem):cool:

Guys, levyuk,jmcilhinney,kulrom Thanks again for replying my query. Finally i have decided to follow the split method as it is more comfertable with me.


i have done the following modifucations so that it fits all the variable length text values ;)
'------------------------------------------------

Dim
s As String = Trim(txtWord.Text)
Dim a() As String = s.Split(" ")

Dim x As Integer = a.GetLength(0)

For x = 0 To a.GetLength(0) - 1

MsgBox(a(x))

Next

'--------------------------------------------------------------------

So if you think a better way than this please let me know,

regards,

sameera
 
This is just a small thing but important to realise. Note that the Split method cannot split on a substring, but only on a single character. You have passed it a string containing one character that is a space. That string is being implicitly converted to the space character. Like I said, not a big deal but I used to do the same thing and it lulled me into making the mistake of thinking that I could split a string on a substring. It is more correct to call Split(" "c), which is actually passing the space character. If you set Option Strict to On I think you'll find that you have to do this.

Also, for a 1-dimensional array, as most people use most of the time and the result of Split will always be, you can use the Length property of the array rather than calling GetLength.

Edit:
Also, the version of Trim that you are using is the old VB6 version. The preferred .NET method would be txtWord.Text.Trim(), which is a member of the String class.
 
What you point out is quite valid, Neal. Split can separate on an array of single characters, but what I meant to point out is that if you had the string "aaaxxxbbbxxxccc", you could not call Split("xxx") and expect to get the array {"aaa", "bbb", "ccc"} returned.
 
Thankx guys, I'll get it for granted. And as i'm a vb 6 programmer i used that trim function. Actually I wanted to do my degree project in vb.net and asp.net so that i thought i would find better ways with this new language. I think I'm improving day by day :)

And this has became my place for solving problems and I would like to thak al you out there for giving help when i needed ,

regards,
sameera
 
If you want to force yourself to use "pure .NET" methods, you can remove the reference to Microsoft.VisualBasic from your project. I don't necessarily recommend or discourage this, but I know that there are plenty of developers who do it. You will lose access to many useful functions, but you will also be forced to abandon a lot of old VB6 functions that are included for backward compatibility but which have preferred .NET alternatives, e.g. MessageBox.Show() instead of MsgBox().
 
Back
Top