הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Monday, April 9, 2012 4:56 AM
Ello.
Dim str as string
str = Hi How Are You.
Is there any trick that could give me the first word of the str (Hi)
Label1.text = FirstWordOfStr.
For example:
str = blah blow bling
Label1.text = blah
Thanks
Thanks
All replies (21)
Monday, April 9, 2012 5:20 AM ✅Answered | 2 votes
use the Substring method with the start index of 0 and the length the Index of the first space
Label1.Text = str.Substring(0, str.IndexOf(" "))
“This forum post is my own opinion and does not necessarily reflect the opinion or view of Microsoft, its employees, or other MVPs.”
Monday, April 9, 2012 8:25 AM ✅Answered | 1 vote
Even something like this can work:
String.Join("", str.TakeWhile(Function(x) x <> " "c))
Cheers :)
If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
Visit the Forum: TechLifeForum
Monday, April 9, 2012 11:38 AM ✅Answered
I also prefer the split approach.. So in your case, the code would be something like..
'Define your string
Dim str As String
'Set your string value
str = "Hi How Are You."
'Define new string array containing elements of str divided by space..
Dim strWords As String() = str.Split(" ")
'Set your label text to the first element in our string array.
Label1.text = strWords(0)
Happy coding
Tuesday, April 10, 2012 2:14 AM ✅Answered
Hi Y0MANNN,
As I am thinking of other characters after the first word, not just the space " " character,
this code will work whether you have a space a comma or whatever else after the first word as in.>>
- Hi, how are you?
- Hi!! How are you?
- Hi how are you?
Add one Button and one Label to a Form to try this please.>>
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String
str = "Hi how are you?"
Label1.Text = FirstWord(str)
End Sub
Public Function FirstWord(ByVal aString As String) As String
Dim returnString As String = ""
Dim letters() As Char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray
If String.IsNullOrEmpty(aString) = False Then
For index As Integer = 0 To aString.Length - 1
If letters.Contains(Convert.ToChar(aString.Substring(index, 1))) = True Then
returnString &= aString.Substring(index, 1)
Else
Exit For
End If
Next
End If
Return returnString
End Function
End Class
Regards, ![]()
Click this link to see the NEW way of how to insert a picture into a forum post.
App Hub for Windows Phone & XBOX 360 developers.
Tuesday, April 10, 2012 2:26 AM ✅Answered | 1 vote
Hi Y0MANNN,
This code will work whether you have a space a comma or whatever else after the first word as in.>>
- Hi, how are you?
- Hi!! How are you?
- Hi how are you?
Add one Button and one Label to a Form to try this please.>>
Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim str As String str = "Hi how are you?" Label1.Text = FirstWord(str) End Sub Public Function FirstWord(ByVal aString As String) As String Dim returnString As String = "" Dim letters() As Char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray If String.IsNullOrEmpty(aString) = False Then For index As Integer = 0 To aString.Length - 1 If letters.Contains(Convert.ToChar(aString.Substring(index, 1))) = True Then returnString &= aString.Substring(index, 1) Else Exit For End If Next End If Return returnString End Function End Class
Regards,
Click this link to see the NEW way of how to insert a picture into a forum post.
In that case, you wouldn't even need all this then, check it out :)
Dim str As String = "Hi!! how are you?"
MsgBox(String.Join("", str.TakeWhile(Function(x) Char.IsLetterOrDigit(x))))
Or if you didn't want AlphaNumeric then the Char.IsLetter() method would work too. Nonetheless that entire function you have could be boiled down to just this one line.
Cheers
~Ace
If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
Visit the Forum: TechLifeForum
Monday, April 9, 2012 5:16 AM | 1 vote
What is a word? If it is any sequence of characters (including a space) preceeding a space, then you can use the string Split method, and take the first array element:
Dim S As String() = TextBox1.Text.Split(" ")
Label1.Text = S(0)
Tuesday, April 10, 2012 1:21 AM
I also prefer the substring approach.. So in your case, the code would be something like..
'Define your string Dim str As String 'Set your string value str = "Hi How Are You." 'Define new string array containing elements of str divided by space.. Dim strWords As String() = str.Split(" ") 'Set your label text to the first element in our string array. Label1.text = strWords(0)
Happy coding
That's an example of split however, not substring, but yes splitting works fine too :) Acamar had posted the same
If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
Visit the Forum: TechLifeForum
Tuesday, April 10, 2012 2:35 AM
Hi AceInfinity,
Okay, I forgot about .IsLetter
thanks for reminding me. :-)
Most of the above replies only thought about the space character though. :)
Regards, ![]()
Click this link to see the NEW way of how to insert a picture into a forum post.
App Hub for Windows Phone & XBOX 360 developers.
Tuesday, April 10, 2012 3:46 AM | 1 vote
This is true, and this is where I was became aware of this issue as well thanks to you! :)
I definitely wasn't thinking the way you were, and seemingly others weren't either lol, but what you pointed out was very valid in the context of the OP, when he termed "word", which in that case as you've said, would not include those special characters.
Cheers buddy
~Ace
If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
Visit the Forum: TechLifeForum
Tuesday, April 10, 2012 4:16 AM | 1 vote
Hi John,
I thought I had sent this already, I don't see it.
You are right of course from your answer I made also this answer (a little bit different but with the same challenge about the blank (but to added commas and things like that).
Module Module1
Sub Main()
Dim x = " John, you gave me this idea "
Console.WriteLine(x.Trim.Split({" "c, ","c})(0)) 'include all splitting charaters
Console.ReadKey()
End Sub
End Module
Success
Cor
Tuesday, April 10, 2012 6:21 AM
You are quite correct! Split is what I meant, not substring. But as you say, both approaches work fine.
Tuesday, April 10, 2012 11:59 AM
To Cor Ligthert,
It looks like you are the only one who has thought of TRIMming the string first too. :)
To AceInfinity,
Sorry I have just tried that in VB.Net 2008 and it did not work.
I tried it like this.>>
Public Function FirstWord(ByVal aString As String) As String
Dim str As String = aString
Return (String.Join("", str.TakeWhile(Function(x) Char.IsLetter(x))))
End Function
'
I could not even get this to work.>>
String.Join("", str.TakeWhile(Function(x) x <> " "c))
Please note: I was testing in VB.Net 2008 but they are probably okay in VB.Net 2010 as I have tried one of them earlier.
Regards, ![]()
Click this link to see the NEW way of how to insert a picture into a forum post.
App Hub for Windows Phone & XBOX 360 developers.
Tuesday, April 10, 2012 9:55 PM
Hi AceInfinity,
I have not tried the code as you had it posted in VB.Net 2010 but if VB.Net 2008
is anything to go by then the syntax should be as below. I had errors with your code.
To Cor Ligthert,
I have added the TRIM function into this as well. :)
To Y0MANNN,
Try this version with one Button and one Label on a Form please. :-D
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String
str = " Hi AceInfinity and Cor Ligthert, how are you?"
Label1.Text = FirstWord(str)
End Sub
Public Function FirstWord(ByVal aString As String) As String
Return (From ch As Char In aString.Trim).TakeWhile(Function(ch) Char.IsLetter(ch)).ToArray
End Function
End Class
Regards, ![]()
Click this link to see the NEW way of how to insert a picture into a forum post.
App Hub for Windows Phone & XBOX 360 developers.
Tuesday, April 10, 2012 10:53 PM
Tested the one from John, Although I prefer a console program to test this.'
Somehow John reminds me to that nice British television sequel Hi di Hi
http://en.wikipedia.org/wiki/Hi-de-Hi!
Don't become angry John, I just have fun and did like that sequel, it was so English, just like I think about John.
:-)
Success
Cor
Tuesday, April 10, 2012 11:11 PM
Hi ALL,
Please see this thread which relates to some of the code in this thread which you may find interesting.>>
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/c279cce6-cc5e-466e-97e4-43b06816954f
Hi Cor,
I'm not offended in anyway by that, however I think my sensibilities are somewhere between
the posh character Jeffrey FairBrother and the entertainment manager Ted Bovis, in other words
I am serious when I want to be and I can be occasionally funny when I want to be, however I think I
tend to be more of a thinker and take things with a serious attitude ( most of the time ).
Has there ever been many comedy series made in your country
that have been dubbed in English or with English subtitles?
Regards, ![]()
Click this link to see the NEW way of how to insert a picture into a forum post.
App Hub for Windows Phone & XBOX 360 developers.
Tuesday, April 10, 2012 11:27 PM
Hi ALL,
It should be noted that some of the above answers will only work in VB.Net 2010
while some of the code or snippets will work in both VB.Net 2008 and VB.Net 2010
I know as I have tested the code in both, with OPTION STRICT ON ( and OFF ). :)
Regards, ![]()
Click this link to see the NEW way of how to insert a picture into a forum post.
App Hub for Windows Phone & XBOX 360 developers.
Thursday, April 12, 2012 1:01 AM | 1 vote
Hi ALL,
It should be noted that some of the above answers will only work in VB.Net 2010
while some of the code or snippets will work in both VB.Net 2008 and VB.Net 2010
I know as I have tested the code in both, with OPTION STRICT ON ( and OFF ). :)
Regards,
Click this link to see the NEW way of how to insert a picture into a forum post.
PLEASE READ:
No no, It's the difference between the .NET 3.5 and .NET 4.0 Framework. Don't get the misconception that this is a Visual Studio version problem at hand. I'll try to clear things up. Here, this will work for all versions now:
Dim str As String = "Hi!! how are you?"
MsgBox(New String(str.TakeWhile(Function(x) Char.IsLetterOrDigit(x)).ToArray))
It's because the returned value is not Char() it's IEnumerable(Of Char)
Sorry for that, I guess it's better to use the .NET Framework 3.5 for my example codes :)
Ahh well... this is much cleaner code that i've provided in this post anyways... And it will also work for .NET 4.0 in addition to the 3.5 Framework.
Cheers
If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
Visit the Forum: TechLifeForum
Thursday, April 12, 2012 3:30 AM
Y0mannn,
Try this:
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim str As String str = "Hi How Are You" Dim FirstWordOfStr As String = str.Substring(0, str.IndexOf(" ")) Label1.Text = FirstWordOfStr End SubEnd Class
Regards,
Thursday, April 12, 2012 4:27 AM
That was already a solution which had been posted, however if you read along John Anthony Oliver has a good point about non-word characteristic values which exist after the word and before the first instance of a space.
This one should work just fine now though:
Dim str As String = "Hi!! how are you?"
MsgBox(New String(str.TakeWhile(Function(x) Char.IsLetter(x)).ToArray))
~Ace
If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
Visit the Forum: TechLifeForum
Thursday, April 12, 2012 5:11 AM
Ace,
Does not go with the by John showed problem of the leading blanks.
Although with this little addition it goes. So I've proposed your reply as answer again because it leads to this code.
Module Module1
Sub Main()
Dim str = " John, you gave me this idea "
Console.WriteLine(New String(str.Trim.TakeWhile(Function(x) Char.IsLetter(x)).ToArray))
Console.ReadKey()
End Sub
End Module
Success
Cor
Thursday, April 12, 2012 11:23 PM
Hi AceInfinity,
Yes the problem of the difference between Framework 3.5 and Framework 4.0 was the reply mentioned in this thread.>>
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/c279cce6-cc5e-466e-97e4-43b06816954f
which I posted a link to earlier. :)
Regards, ![]()
Click this link to see the NEW way of how to insert a picture into a forum post.