VB School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB Expert!

Join 300,404 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,623 people online right now. Registration is fast and FREE... Join Now!




Trimming the first three Letters from a string

 

Trimming the first three Letters from a string, Before I print a strinf I need the first 3 letters removed.

oe8pck

2 Jul, 2009 - 05:50 AM
Post #1

New D.I.C Head
*

Joined: 27 Jun, 2009
Posts: 17


My Contributions
Hi, I still very much a beginner!
I have a string and would like to remove the first three letters before it is sent to the Textbox. I have written the following code but do not get the desired effect. "Printout = LTrim(Newdata, 3)"
Can someone please put me on the right track. I have also tried mit Len but then the result is only numbers!

Paul OE8PCK



CODE


Option Explicit

Dim Newdata As String   'each imcoming packet is assembled here

Private Sub Form_Load()
         Form1.Caption = "OE8PCK Digital Wattmeter"
         With MSComm1
            .CommPort = 4
            .Handshaking = 2 - comRTS
            .RThreshold = 1
            .RTSEnable = True
            .Settings = "9600,n,8,1"
            .SThreshold = 1
            .PortOpen = True
            ' Leave all other settings as default values.
         End With
        
         OutputDisplay.Text = "Infobox"
         InformationDisplay.Text = "Databox"
        
         Newdata = ""   'initialize to empty
        
      End Sub

      Private Sub Form_Unload(Cancel As Integer)
         MSComm1.PortOpen = False
      End Sub

      Private Sub MSComm1_OnComm()
      
         Dim InBuff As String
            
         Dim I As Integer       'used to inspect each incoming character
         Dim theChar As String  'each received character
         Dim theInfo As String
         Dim Printout As String
        
         Select Case MSComm1.CommEvent
         ' Handle each event or error by placing
         ' code below each case statement.
         ' Errors
            Case comEventBreak   ' A Break was received.
            Case comEventCDTO    ' CD (RLSD) Timeout.
            Case comEventCTSTO   ' CTS Timeout.
            Case comEventDSRTO   ' DSR Timeout.
            Case comEventFrame   ' Framing Error.
            Case comEventOverrun ' Data Lost.
            Case comEventRxOver  ' Receive buffer overflow.
            Case comEventRxParity   ' Parity Error.
            Case comEventTxFull  ' Transmit buffer full.
            Case comEventDCB     ' Unexpected error retrieving DCB]

         ' Events
            Case comEvCD   ' Change in the CD line.
            Case comEvCTS  ' Change in the CTS line.
            Case comEvDSR  ' Change in the DSR line.
            Case comEvRing ' Change in the Ring Indicator.
            Case comEvReceive ' Received RThreshold # of chars.
            
                InBuff = MSComm1.Input  'received 1 or more characters
              
                For I = 1 To Len(InBuff) 'examine each received character in sequence
            
                    theChar = Mid$(InBuff, I, 1) 'extract the next character
                  
                    If Asc(theChar) = 13 Then 'Look for CR
                        theInfo = Mid$(Newdata, 2, 1) 'Loads the second letter in the String "Newdata" in "theInfo" is a (I) or (P)
                        
                        
                        If theInfo = "I" Then
                            Printout = LTrim(Newdata, 3)
                            InformationDisplay.SelLength = 0
                            InformationDisplay.SelStart = Len(InformationDisplay.Text)
                            InformationDisplay.SelText = Printout + vbCr + vbLf   'include a CR and LF to separate from next line placed in OutputDisplay
                            InformationDisplay.SelLength = 0
                            
                        ElseIf theInfo = "P" Then
                            Printout = LTrim(Newdata, 3)
                            OutputDisplay.SelLength = 0
                            OutputDisplay.SelStart = Len(OutputDisplay.Text)
                            OutputDisplay.SelText = Printout + vbCr + vbLf   'include a CR and LF to separate from next line placed in OutputDisplay
                            OutputDisplay.SelLength = 0
                            
                        End If
                    
                        Newdata = "" 'clear NewData so it can assemble the next packet
                
                        
                    ElseIf Asc(theChar) <> 10 Then  'ignore linefeeds
                        
                        Newdata = Newdata + theChar 'received a character -- append it to NewData
                                    
                    End If

                Next I
            
            Case comEvSend ' There are SThreshold number of
                           ' characters in the transmit buffer.
            Case comEvEOF  ' An EOF character was found in the
                           ' input stream.
         End Select

      End Sub





User is offlineProfile CardPM
+Quote Post


vb5prgrmr

RE: Trimming The First Three Letters From A String

2 Jul, 2009 - 06:28 AM
Post #2

D.I.C Regular
***

Joined: 21 Mar, 2009
Posts: 459



Thanked: 30 times
My Contributions
use Mid...
CODE

Text1.Text = Mid("strText", 3)



Good Luck


User is offlineProfile CardPM
+Quote Post

birdflu9999

RE: Trimming The First Three Letters From A String

2 Jul, 2009 - 06:37 AM
Post #3

New D.I.C Head
Group Icon

Joined: 26 Jun, 2009
Posts: 22



Thanked: 2 times
Dream Kudos: 25
My Contributions
Hi i think you used the wrong function, "LTrim" is used to remove the spaces from the left side of a string....

but here is an example for you if you want to remove the first 3 letters of a string...

CODE


    Dim y As Integer
    Dim z As Integer
    Dim x As String
    Dim ans As String
    
    x = "Hello World!"
    y = Len(x)
    z = y - 3
    If z >= 3 Then
        For i = 1 To y
            ans = Right(x, z)
        Next i
    End If
    
    MsgBox ans




the ans should be "lo World!"


User is offlineProfile CardPM
+Quote Post

oe8pck

RE: Trimming The First Three Letters From A String

2 Jul, 2009 - 06:42 AM
Post #4

New D.I.C Head
*

Joined: 27 Jun, 2009
Posts: 17


My Contributions
Is this correct when the string Printout has to be printed.
If so it does not work. Sorry


If theInfo = "I" Then
Printout.Text = Mid(Newdata, 3)
InformationDisplay.SelLength = 0
InformationDisplay.SelStart = Len(InformationDisplay.Text)
InformationDisplay.SelText = Printout + vbCr + vbLf 'include a CR and LF to separate from next line placed in OutputDisplay
InformationDisplay.SelLength = 0

ElseIf theInfo = "P" Then
Printout.Text = Mid(Newdata, 3)
OutputDisplay.SelLength = 0
OutputDisplay.SelStart = Len(OutputDisplay.Text)
OutputDisplay.SelText = Printout + vbCr + vbLf 'include a CR and LF to separate from next line placed in OutputDisplay
OutputDisplay.SelLength = 0

User is offlineProfile CardPM
+Quote Post

oe8pck

RE: Trimming The First Three Letters From A String

2 Jul, 2009 - 07:47 AM
Post #5

New D.I.C Head
*

Joined: 27 Jun, 2009
Posts: 17


My Contributions
The solution from Birdflu9999 works. Thanks very much to all.
User is offlineProfile CardPM
+Quote Post

NoBrain

RE: Trimming The First Three Letters From A String

2 Jul, 2009 - 09:34 AM
Post #6

D.I.C Lover
Group Icon

Joined: 25 Mar, 2009
Posts: 1,152



Thanked: 33 times
Dream Kudos: 125
My Contributions
you can do it with mid$() function

Function Mid$(String As String, Start As Long, [Length]) As String
Member of VBA.Strings
Returns specified number of characters from a string


This post has been edited by NoBrain: 2 Jul, 2009 - 09:35 AM
User is offlineProfile CardPM
+Quote Post

NoBrain

RE: Trimming The First Three Letters From A String

2 Jul, 2009 - 09:39 AM
Post #7

D.I.C Lover
Group Icon

Joined: 25 Mar, 2009
Posts: 1,152



Thanked: 33 times
Dream Kudos: 125
My Contributions
yes psychocoder is correct exuse me sad.gif :shame:

This post has been edited by NoBrain: 2 Jul, 2009 - 09:51 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder

RE: Trimming The First Three Letters From A String

2 Jul, 2009 - 09:44 AM
Post #8

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,714



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
@NoBrain:

That is incorrect, this code

CODE

dim x as Long
dim str1 as string = "killah boy"
x=mid(str1,1)


Will generate an error because you're trying to assign a string value to an integer variable. Mid will return the text within a certain range (IndexOf returns an integer value of where the specified character or string was found)
User is offlineProfile CardPM
+Quote Post

vb5prgrmr

RE: Trimming The First Three Letters From A String

2 Jul, 2009 - 09:52 PM
Post #9

D.I.C Regular
***

Joined: 21 Mar, 2009
Posts: 459



Thanked: 30 times
My Contributions
Hmpff....

First to reply with MId and a for loop is used??? Go Figure because I can't!!! Can you oh one with No Brain?

smile.gif
User is offlineProfile CardPM
+Quote Post

birdflu9999

RE: Trimming The First Three Letters From A String

3 Jul, 2009 - 12:11 AM
Post #10

New D.I.C Head
Group Icon

Joined: 26 Jun, 2009
Posts: 22



Thanked: 2 times
Dream Kudos: 25
My Contributions
hi to vb5prgrmr no offense but i was curious about your solution so i tried it and i was having a wrong answer
i think that the error was the "3" so i changed it to "4"

CODE

  Text1.Text = Mid("strText",4)

User is offlineProfile CardPM
+Quote Post

Caranzo

RE: Trimming The First Three Letters From A String

3 Jul, 2009 - 01:59 AM
Post #11

New D.I.C Head
*

Joined: 3 Jul, 2009
Posts: 4

QUOTE(oe8pck @ 2 Jul, 2009 - 05:50 AM) *

Hi, I still very much a beginner!
I have a string and would like to remove the first three letters before it is sent to the Textbox. I have written the following code but do not get the desired effect. "Printout = LTrim(Newdata, 3)"
Can someone please put me on the right track. I have also tried mit Len but then the result is only numbers!

Paul OE8PCK



CODE


Option Explicit

Dim Newdata As String   'each imcoming packet is assembled here

Private Sub Form_Load()
         Form1.Caption = "OE8PCK Digital Wattmeter"
         With MSComm1
            .CommPort = 4
            .Handshaking = 2 - comRTS
            .RThreshold = 1
            .RTSEnable = True
            .Settings = "9600,n,8,1"
            .SThreshold = 1
            .PortOpen = True
            ' Leave all other settings as default values.
         End With
        
         OutputDisplay.Text = "Infobox"
         InformationDisplay.Text = "Databox"
        
         Newdata = ""   'initialize to empty
        
      End Sub

      Private Sub Form_Unload(Cancel As Integer)
         MSComm1.PortOpen = False
      End Sub

      Private Sub MSComm1_OnComm()
      
         Dim InBuff As String
            
         Dim I As Integer       'used to inspect each incoming character
         Dim theChar As String  'each received character
         Dim theInfo As String
         Dim Printout As String
        
         Select Case MSComm1.CommEvent
         ' Handle each event or error by placing
         ' code below each case statement.
         ' Errors
            Case comEventBreak   ' A Break was received.
            Case comEventCDTO    ' CD (RLSD) Timeout.
            Case comEventCTSTO   ' CTS Timeout.
            Case comEventDSRTO   ' DSR Timeout.
            Case comEventFrame   ' Framing Error.
            Case comEventOverrun ' Data Lost.
            Case comEventRxOver  ' Receive buffer overflow.
            Case comEventRxParity   ' Parity Error.
            Case comEventTxFull  ' Transmit buffer full.
            Case comEventDCB     ' Unexpected error retrieving DCB]

         ' Events
            Case comEvCD   ' Change in the CD line.
            Case comEvCTS  ' Change in the CTS line.
            Case comEvDSR  ' Change in the DSR line.
            Case comEvRing ' Change in the Ring Indicator.
            Case comEvReceive ' Received RThreshold # of chars.
            
                InBuff = MSComm1.Input  'received 1 or more characters
              
                For I = 1 To Len(InBuff) 'examine each received character in sequence
            
                    theChar = Mid$(InBuff, I, 1) 'extract the next character
                  
                    If Asc(theChar) = 13 Then 'Look for CR
                        theInfo = Mid$(Newdata, 2, 1) 'Loads the second letter in the String "Newdata" in "theInfo" is a (I) or (P)
                        
                        
                        If theInfo = "I" Then
                            Printout = LTrim(Newdata, 3)
                            InformationDisplay.SelLength = 0
                            InformationDisplay.SelStart = Len(InformationDisplay.Text)
                            InformationDisplay.SelText = Printout + vbCr + vbLf   'include a CR and LF to separate from next line placed in OutputDisplay
                            InformationDisplay.SelLength = 0
                            
                        ElseIf theInfo = "P" Then
                            Printout = LTrim(Newdata, 3)
                            OutputDisplay.SelLength = 0
                            OutputDisplay.SelStart = Len(OutputDisplay.Text)
                            OutputDisplay.SelText = Printout + vbCr + vbLf   'include a CR and LF to separate from next line placed in OutputDisplay
                            OutputDisplay.SelLength = 0
                            
                        End If
                    
                        Newdata = "" 'clear NewData so it can assemble the next packet
                
                        
                    ElseIf Asc(theChar) <> 10 Then  'ignore linefeeds
                        
                        Newdata = Newdata + theChar 'received a character -- append it to NewData
                                    
                    End If

                Next I
            
            Case comEvSend ' There are SThreshold number of
                           ' characters in the transmit buffer.
            Case comEvEOF  ' An EOF character was found in the
                           ' input stream.
         End Select

      End Sub







QUOTE(Caranzo @ 3 Jul, 2009 - 01:56 AM) *

QUOTE(oe8pck @ 2 Jul, 2009 - 05:50 AM) *

Hi, I still very much a beginner!
I have a string and would like to remove the first three letters before it is sent to the Textbox. I have written the following code but do not get the desired effect. "Printout = LTrim(Newdata, 3)"
Can someone please put me on the right track. I have also tried mit Len but then the result is only numbers!

Paul OE8PCK



CODE


Option Explicit

Dim Newdata As String   'each imcoming packet is assembled here

Private Sub Form_Load()
         Form1.Caption = "OE8PCK Digital Wattmeter"
         With MSComm1
            .CommPort = 4
            .Handshaking = 2 - comRTS
            .RThreshold = 1
            .RTSEnable = True
            .Settings = "9600,n,8,1"
            .SThreshold = 1
            .PortOpen = True
            ' Leave all other settings as default values.
         End With
        
         OutputDisplay.Text = "Infobox"
         InformationDisplay.Text = "Databox"
        
         Newdata = ""   'initialize to empty
        
      End Sub

      Private Sub Form_Unload(Cancel As Integer)
         MSComm1.PortOpen = False
      End Sub

      Private Sub MSComm1_OnComm()
      
         Dim InBuff As String
            
         Dim I As Integer       'used to inspect each incoming character
         Dim theChar As String  'each received character
         Dim theInfo As String
         Dim Printout As String
        
         Select Case MSComm1.CommEvent
         ' Handle each event or error by placing
         ' code below each case statement.
         ' Errors
            Case comEventBreak   ' A Break was received.
            Case comEventCDTO    ' CD (RLSD) Timeout.
            Case comEventCTSTO   ' CTS Timeout.
            Case comEventDSRTO   ' DSR Timeout.
            Case comEventFrame   ' Framing Error.
            Case comEventOverrun ' Data Lost.
            Case comEventRxOver  ' Receive buffer overflow.
            Case comEventRxParity   ' Parity Error.
            Case comEventTxFull  ' Transmit buffer full.
            Case comEventDCB     ' Unexpected error retrieving DCB]

         ' Events
            Case comEvCD   ' Change in the CD line.
            Case comEvCTS  ' Change in the CTS line.
            Case comEvDSR  ' Change in the DSR line.
            Case comEvRing ' Change in the Ring Indicator.
            Case comEvReceive ' Received RThreshold # of chars.
            
                InBuff = MSComm1.Input  'received 1 or more characters
              
                For I = 1 To Len(InBuff) 'examine each received character in sequence
            
                    theChar = Mid$(InBuff, I, 1) 'extract the next character
                  
                    If Asc(theChar) = 13 Then 'Look for CR
                        theInfo = Mid$(Newdata, 2, 1) 'Loads the second letter in the String "Newdata" in "theInfo" is a (I) or (P)
                        
                        
                        If theInfo = "I" Then
                            Printout = LTrim(Newdata, 3)
                            InformationDisplay.SelLength = 0
                            InformationDisplay.SelStart = Len(InformationDisplay.Text)
                            InformationDisplay.SelText = Printout + vbCr + vbLf   'include a CR and LF to separate from next line placed in OutputDisplay
                            InformationDisplay.SelLength = 0
                            
                        ElseIf theInfo = "P" Then
                            Printout = LTrim(Newdata, 3)
                            OutputDisplay.SelLength = 0
                            OutputDisplay.SelStart = Len(OutputDisplay.Text)
                            OutputDisplay.SelText = Printout + vbCr + vbLf   'include a CR and LF to separate from next line placed in OutputDisplay
                            OutputDisplay.SelLength = 0
                            
                        End If
                    
                        Newdata = "" 'clear NewData so it can assemble the next packet
                
                        
                    ElseIf Asc(theChar) <> 10 Then  'ignore linefeeds
                        
                        Newdata = Newdata + theChar 'received a character -- append it to NewData
                                    
                    End If

                Next I
            
            Case comEvSend ' There are SThreshold number of
                           ' characters in the transmit buffer.
            Case comEvEOF  ' An EOF character was found in the
                           ' input stream.
         End Select

      End Sub







User is offlineProfile CardPM
+Quote Post

Caranzo

RE: Trimming The First Three Letters From A String

3 Jul, 2009 - 02:05 AM
Post #12

New D.I.C Head
*

Joined: 3 Jul, 2009
Posts: 4

use trim then mid........
by the way vb will no longer excess....... biggrin.gif
User is offlineProfile CardPM
+Quote Post

NoBrain

RE: Trimming The First Three Letters From A String

3 Jul, 2009 - 02:39 AM
Post #13

D.I.C Lover
Group Icon

Joined: 25 Mar, 2009
Posts: 1,152



Thanked: 33 times
Dream Kudos: 125
My Contributions
vb6.0 you mean
User is offlineProfile CardPM
+Quote Post

NoBrain

RE: Trimming The First Three Letters From A String

3 Jul, 2009 - 02:59 AM
Post #14

D.I.C Lover
Group Icon

Joined: 25 Mar, 2009
Posts: 1,152



Thanked: 33 times
Dream Kudos: 125
My Contributions
why trim and then mid ? why trim at all

what is diference betwin mid and mid$? (mid returns variant ? )

This post has been edited by NoBrain: 3 Jul, 2009 - 02:58 AM
User is offlineProfile CardPM
+Quote Post

birdflu9999

RE: Trimming The First Three Letters From A String

3 Jul, 2009 - 05:18 AM
Post #15

New D.I.C Head
Group Icon

Joined: 26 Jun, 2009
Posts: 22



Thanked: 2 times
Dream Kudos: 25
My Contributions
hi i think you'll use the Trim function if the string input will have "spaces" from the left.

example:
CODE

Text1.Text = Mid("   strTEXT", 4)


the result with this code will be "strTEXT" but if you use the code with a trim

CODE

Text1.Text = Mid(Trim("   strTEXT"), 4)


the result will be "TEXT"

This post has been edited by birdflu9999: 3 Jul, 2009 - 05:23 AM
User is offlineProfile CardPM
+Quote Post

NoBrain

RE: Trimming The First Three Letters From A String

3 Jul, 2009 - 05:29 AM
Post #16

D.I.C Lover
Group Icon

Joined: 25 Mar, 2009
Posts: 1,152



Thanked: 33 times
Dream Kudos: 125
My Contributions
aham ok ty and some one what is the diference betwin mid and mid$ functions

sorry for taking over your topic dude sad.gif
User is offlineProfile CardPM
+Quote Post

vb5prgrmr

RE: Trimming The First Three Letters From A String

3 Jul, 2009 - 07:48 PM
Post #17

D.I.C Regular
***

Joined: 21 Mar, 2009
Posts: 459



Thanked: 30 times
My Contributions
Mid...
Returns a Variant (String) containing a specified number of characters from a string.

while Mid$ returns a string.

To see the difference, type in mid and hit the space bar. You should see the tooltop come up. Now backspace and add the $ and then hit space. Now when the tooltip comes up, you should see the As String.

So what does this mean, you ask?

Well when mid returns its variant, vb then needs to coerce the variant into the datatype that is recieving the results of the mid operation which takes longer to do because vb then needs to check the datatype that is recieving this result. Once it knows that, it then changes the result (if possible) to the correct datatype...
CODE

Dim I As Integer

I = 12346

I = Mid(I, 3)

Debug.Print I

while mid$ directly states that it will return a string (even though it will work with above example) and is thus faster when doing string manipulations.

and yeah, go figure, want to remove 3 characters so when typing out mid("strText",3)


Good Luck
User is offlineProfile CardPM
+Quote Post

NoBrain

RE: Trimming The First Three Letters From A String

4 Jul, 2009 - 01:54 AM
Post #18

D.I.C Lover
Group Icon

Joined: 25 Mar, 2009
Posts: 1,152



Thanked: 33 times
Dream Kudos: 125
My Contributions
ok ty dude
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 10:33PM

Live VB Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month