# vb.net question



## shuggans (Oct 9, 2012)

How would I identify specific words in a string?  example "Hello my name is pat"
if i wanted to grab the 4th word from each string or only grab hi but using an identifier so I could take say the 2nd word from that string (my) or if the incoming string was "I like jam" then it would take (like)

???


----------



## eidairaman1 (Oct 9, 2012)

if youre relating this topic to the one about IRC issue, use the previous one and shut this one down, mods dont take too kindly to having multiple posts about the same thing from the same person, as its known as Topic Spamming.

http://www.techpowerup.com/forums/showthread.php?t=173368

Patience is key, give it some time, you sometimes get an immediate answer other times you dont on the forums.


----------



## FordGT90Concept (Oct 9, 2012)

shuggans said:


> How would I identify specific words in a string?  example "Hello my name is pat"
> if i wanted to grab the 4th word from each string or only grab hi but using an identifier so I could take say the 2nd word from that string (my) or if the incoming string was "I like jam" then it would take (like)
> 
> ???


Use .Split(Chr(32)) which will break it into an array of strings.  Then you'd have to use the indexor on the array to get the fourth indexed item in the array.  With it in array format, you can do whatever you want with each individual word.

32 is the ASCII character number for space.  Chr() converts that number into the character type.


----------



## shuggans (Oct 9, 2012)

This was for a separate issue... figured i'd start a new thread for search purposes...


----------



## shuggans (Oct 9, 2012)

So how do i use the indexor to grab the 3rd word?


----------



## FordGT90Concept (Oct 9, 2012)

example:

```
Dim parts() As String = "Hello my name is pat".Split(Chr(32))
Console.WriteLine(parts(2))
```
Indexors are zero-based so 2 would be the third word.


----------



## shuggans (Oct 9, 2012)

*ok...*

Ok... so heres what I want... jsut need the one weird part figured out:

```
Dim MessageParts() As String = TextBox_MsgOut.Text.Split(Chr(32))
        Dim WordCount As Integer = MessageParts.Length
        Dim ManualMessage As String = MessageParts(1, WordCount - 1)
        If MessageParts(0) = "/tell " Then
            ManualMessage = "PRIVMSG" & My.Settings.Channel & " " & ManualMessage
        End If
```
This line:

```
Dim ManualMessage As String = MessageParts(1, WordCount - 1)
```
I want to return the original text in the textbox - the "/tell "

I will use this for other commands where the first word isnt the only one needing to be looked for or excluded is why I ask in this way.

Any clue?


----------



## FordGT90Concept (Oct 9, 2012)

Would be easier just to do

```
If TextBox_MsgOut.Text.StartsWith("/tell ") Then
```


----------



## shuggans (Oct 9, 2012)

I know... but I need to make other commands... and I also need to use the same text to show me what I sent.  
So far I have:

```
Private Sub Button_send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_send.Click
        Dim InputMessage As String = TextBox_MsgOut.Text
        Dim MessageParts() As String = TextBox_MsgOut.Text.Split(Chr(32))
        Dim WordCount As Integer = MessageParts.Length
        Dim CutText As String = MessageParts(0) & " " & MessageParts(1)
        Dim CutCount As Integer = CutText.Length
        Dim ManualMessage As String = String.Join(" ", MessageParts)
        If MessageParts(0) = "/join" Then
            CurrentChannel = MessageParts(1)
            ManualMessage = "JOIN :" & MessageParts(1)
        ElseIf MessageParts(0) = "/tell" Then
            ManualMessage = "PRIVMSG " & MessageParts(1) & " " & ManualMessage.Remove(0, CutCount)
            TextBox_MsgIn.AppendText("TO " & MessageParts(1) & ": " & ManualMessage.Remove(0, CutCount) & vbCrLf)
        Else
            ManualMessage = "PRIVMSG " & CurrentChannel & " :" & ManualMessage
            TextBox_MsgIn.AppendText(My.Settings.Nick & ">>" & InputMessage & vbCrLf)
        End If

        Send(ManualMessage)
        TextBox_MsgOut.Clear()
    End Sub
```

But this is super ugly.


----------



## shuggans (Oct 9, 2012)

Basically what I want is a way to break input text down into words so I can remove MY clients commands and put it into IRC protocol form before sending.  I also want to display it cleaner than displaying the RAW IRC protocol data I'm sending.


----------



## FordGT90Concept (Oct 9, 2012)

Aren't all IRC commands a single word?  Why not use a Select...Case...End Select block?

```
Select MessageParts(0).ToLower
  Case "/join"
    'do something
  Case "/tell"
    'do something else
  Case Else
   Dim output As String = MessageParts(0)
   For i As Integer = 1 To MessageParts.Length
     output = output & " " & MessageParts(i)
   Next
End Select
```


----------



## Mindweaver (Oct 9, 2012)

+1 for *Select...Case...End Select*. Also, you can use "*RegularExpressions*" to find words/Characters in text as well.  I wouldn't re invent the wheel, and change from *String.Split Method*. I just wanted to share another way. Your in good hands with Ford he is a very knowledgeable fellow.


----------

