dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: first letter of the text

  1. #1
    Join Date
    Apr 2008
    Posts
    93

    first letter of the text

    Hi!! Im totally new to VB. so if my question/s is retarded in someway, please forgive me.

    I need to check the first letter of the text whether is it start with "S", "G" or "F".

    what is the way to do it? condition, function, etc?

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: first letter of the text

    A simple command can be used to extract the first letter...
    VB has a few that allow you to parse strings...

    LEFT, MID, RIGHT ....

    Look up these three in the help files, you'll get what you need...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    Apr 2008
    Posts
    93

    Re: first letter of the text

    Quote Originally Posted by GremlinSA View Post
    A simple command can be used to extract the first letter...
    VB has a few that allow you to parse strings...

    LEFT, MID, RIGHT ....

    Look up these three in the help files, you'll get what you need...
    Thanks =)

  4. #4
    Join Date
    May 2012
    Posts
    7

    Thumbs up Re: first letter of the text

    Hi,



    By using any of the following method we could get the first letter of the text or word.

    1 Mid,
    2 Left,
    3 Right,
    4 Instr


    If Mid(Text1.Text, 1, 1) = "S" Or Mid(Text1.Text, 1, 1) = "G" Or Mid(Text1.Text, 1, 1) = "F" Then
    MsgBox "Yes The First Letter Is " & Mid(Text1.Text, 1, 1)
    End If

    If Left(Text1.Text, 1) = "S" Or Left(Text1.Text, 1) = "G" Or Left(Text1.Text, 1) = "F" Then
    MsgBox "Yes The First Letter Is " & Left(Text1.Text, 1, 1)
    End If

    If InStr(1, Trim(Text1.Text), "S", vbTextCompare) > 0 Or InStr(1, Trim(Text1.Text), "G", vbTextCompare) > 0 Or InStr(1, Trim(Text1.Text), "F", vbTextCompare) > 0 Then
    MsgBox "Yes The First Letter Is " & Mid(Text1.Text, 1, 1)
    End If

    If Right(Text1.Text, 1) = "S" Or Right(Text1.Text, 1) = "G" Or Right(Text1.Text, 1) = "F" Then
    MsgBox "Yes The First Letter Is " & Right(Text1.Text, 1)
    End If

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: first letter of the text

    Except that the usage of Right() is COMPLETELY WRONG in this case, because it would return the LAST letter of the string,
    AND the usage of InStr() is also WRONG, because it would fire true if ANY of the letters in the string is S, F or G.

    The solid choice is definitely the Left() function (also Mid() is acceptable).
    If you want to use InStr() you have to ask:
    If InStr(Text1.Text, "G") = 1 Then...

  6. #6
    Join Date
    May 2012
    Posts
    7

    Re: first letter of the text

    Quote Originally Posted by WoF View Post
    Except that the usage of Right() is COMPLETELY WRONG in this case, because it would return the LAST letter of the string,
    AND the usage of InStr() is also WRONG, because it would fire true if ANY of the letters in the string is S, F or G.

    The solid choice is definitely the Left() function (also Mid() is acceptable).
    If you want to use InStr() you have to ask:
    If InStr(Text1.Text, "G") = 1 Then...
    Thanks WoF,
    I got it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)