By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,646 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,646 IT Pros & Developers. It's quick & easy.

substring

P: 32
how do i make use of substring method in order to extract the specified data from a a long string?
I also need to do some checking b4 extracting the data, for instance:
it only will extract the data when the first 8bits are 10101010,then it ll extract the following...for example:
10101010111000111



here is my code:
Expand|Select|Wrap|Line Numbers
  1. Dim dataE As String
  2.         Dim lot101, lot102, lot103, lot104 As String
  3.         Dim lot201, lot202, lot203, lot204 As String
  4.         Dim lot301, lot302, lot303, lot304 As String
  5.         Dim data As String = "10101010"
  6.         dataE = TextBox1.Text
  7.  
  8.         If dataE <> data Then
  9.             MsgBox("There is not have related data being sense!!")
  10.         ElseIf dataE = "" Then
  11.             MessageBox.Show("Please insert lot values!")
  12.         Else
  13.             Microsoft.VisualBasic.Left(TextBox1.Text, 8)
  14.             'If (TextBox1.Text(1, 8)) = data Then
  15.  
  16.             lot101 = dataE.Substring(9, 1)
  17.             lot102 = dataE.Substring(10, 1)
  18.             lot103 = dataE.Substring(11, 1)
  19.             lot104 = dataE.Substring(12, 1)
  20.  
  21.             lot201 = dataE.Substring(13, 1)
  22.             lot202 = dataE.Substring(14, 1)
  23.             lot203 = dataE.Substring(15, 1)
  24.             lot204 = dataE.Substring(16, 1)
  25.  
  26.             lot301 = dataE.Substring(17, 1)
  27.             lot302 = dataE.Substring(18, 1)
  28.             lot303 = dataE.Substring(19, 1)
  29.             lot304 = dataE.Substring(20, 1)
  30.  
  31.             TextBox2.Text = "The Lot 101 value is " & _
  32.             "'" & lot101 & "'" & vbCrLf _
  33.            & "The Lot 102 value is " & _
  34.              "'" & lot102 & "'" & vbCrLf _
  35.             & "The Lot 103 value is " & _
  36.              "'" & lot103 & "'" & vbCrLf _
  37.             & "The Lot 104 value is " & _
  38.              "'" & lot104 & "'" & vbCrLf
  39.  
  40.             TextBox3.Text = "The Lot 201 value is " & _
  41.              "'" & lot201 & "'" & vbCrLf _
  42.              & "The Lot 202 value is " & _
  43.               "'" & lot202 & "'" & vbCrLf _
  44.             & "The Lot 203 value is " & _
  45.              "'" & lot203 & "'" & vbCrLf _
  46.              & "The Lot 204 value is " & _
  47.             "'" & lot204 & "'" & vbCrLf
  48.  
  49.             TextBox4.Text = "The Lot 301 value is " & _
  50.              "'" & lot301 & "'" & vbCrLf _
  51.             & "The Lot 302 value is " & _
  52.             "'" & lot302 & "'" & vbCrLf _
  53.              & "The Lot 303 value is " & _
  54.             "'" & lot303 & "'" & vbCrLf _
  55.             & "The Lot 304 value is " & _
  56.             "'" & lot304 & "'" & vbCrLf
  57.  
  58.         End If

but there is error occur...somebody can help??
thanks
May 28 '07 #1
Share this Question
Share on Google+
11 Replies


Expert 5K+
P: 8,434
What error are you getting, and at which line?
May 28 '07 #2

P: 32
dyc
What error are you getting, and at which line?

Dim data As String = "10101010"
dataE = TextBox1.Text

If dataE <> data Then
MsgBox("There is don't have any related data being sense!!")
ElseIf dataE = "" Then
MessageBox.Show("Please insert lot values!")
Else
Microsoft.VisualBasic.Left(TextBox1.Text, 8)
'If (TextBox1.Text(1, 8)) = data Then

lot101 = dataE.Substring(9, 1)
lot102 = dataE.Substring(10, 1)
lot103 = dataE.Substring(11, 1)
lot104 = dataE.Substring(12, 1)

lot201 = dataE.Substring(13, 1)
lot202 = dataE.Substring(14, 1)
lot203 = dataE.Substring(15, 1)
lot204 = dataE.Substring(16, 1)

lot301 = dataE.Substring(17, 1)
lot302 = dataE.Substring(18, 1)
lot303 = dataE.Substring(19, 1)
lot304 = dataE.Substring(20, 1)


there is a textbox which being use to insert the value either 1 or 0 only.
It will only display the data to another one more textbox only as long as the first 8bits are = 10101010 (0xAA) else it wont do anything.But it can't display the following data and check the first 8 bits....Is there anything wrong?
Thank you very much!!
May 29 '07 #3

Expert 5K+
P: 8,434
I think we need to clarify exactly what you're working with here. Are you talking about bits, or are you talking about a string, containing "0" and "1" character, which represent bits? There are big differences. Strings are easy. To check whether the start of a string matches your 8 characters, just check If Left(dataE, 8) <> data Then.

To explain a bit more, when you do this...
Dim data As String = "10101010"
...that is not 8 bits. That is a string of 8 bytes, or characters. Each of which is made up of 8 bits or more, depending on whether we're talking about ASCII or Unicode text.
May 29 '07 #4

Expert 5K+
P: 8,434
Question - what does this line do?

Microsoft.VisualBasic.Left(TextBox1.Text, 8)
May 29 '07 #5

P: 32
dyc
Question - what does this line do?

Microsoft.VisualBasic.Left(TextBox1.Text, 8)
i want to extract the string from the data that being key...
i also have tried left(dataE,8) but it return me an error which says

public property left() as integer has no parameters abd its return type cannot be indexed.

what does its means??

actually that I wan it in the byte,the thing i wan is..
there has a data packet being sense by a sensor, but here I using a simulation ...
there have been divided to three portions of data, the first data packet it has 1 byte,the second data packet it has 1byte and the last data packet it has 3byte.
firstly,I need to make sure that if the first heading detected a start byte with (0xAA),then after that it will detect the number of byte expected and lastly it ll display the last information.
the last information which is contain 3 bytes info has been divided to 2bytes for address and 1 byte for data....

izzit there is possible for me to convert the byte to binary's form?
and can u pls expalin some about byte bcoz i not really understand abt how does the byte works...

Really thanks to u..
May 29 '07 #6

Expert 5K+
P: 8,434
I think the problem with this whole Left() business is that you are trying to execute a function in the wrong context.

I mean, what if I said to you "square root", or "three quarters"? It doesn't mean anything except in the right context.

Likewise, if you have a string value, you can't just say "the first 8 characters". You have to do something with them. Like "take the first 8 characters and place them in this variable".
May 29 '07 #7

P: 32
dyc
I think the problem with this whole Left() business is that you are trying to execute a function in the wrong context.

I mean, what if I said to you "square root", or "three quarters"? It doesn't mean anything except in the right context.

Likewise, if you have a string value, you can't just say "the first 8 characters". You have to do something with them. Like "take the first 8 characters and place them in this variable".

How do I do if there is a long data that is key into a textbox and I wanna to extract the last 8 bits?because I cannot know that total got how many bits will be entered....the data being extract are 1 or 0 coz i ald converted it to binary format...
Jun 8 '07 #8

Expert 5K+
P: 8,434
How do I do if there is a long data that is key into a textbox and I wanna to extract the last 8 bits?
Use the Right() function.
Jun 8 '07 #9

P: 32
dyc
Use the Right() function.


Assume that my design has a textbox and a button ..
the textbox is use for insert the long string data which are either 1 or 0. for example like 1011100001111100001110000 or even more longer oe shorter but must more than 8 bits, else an error will occur.I want to shift the bit to right..before shifting the bit,I need to do some checking which is I wan to check izzit the last 8 bits data is "10101010" in Binary 170 in dec and HAA in Hex.If the last 8 bits are not equal to 10101010,then i need to shift it by 1 bit to check the following bit until I get the correct data which is equal to 10101010,else it will keep checking...how I can do that?Once I get the correct data,it will extract the following 8 bits.How i can do it ?

For example like when i key in a long string= 11110000111010101011
by shifting two times ,i get the correct data.I will be a little bit more easier if there was a fixed data but it's not.It is a flexible data which key in by user's leisure...
Thanks..
Jun 8 '07 #10

Expert 5K+
P: 8,434
So in the quoted example, your routine should return "11" - correct?
Jun 8 '07 #11

Expert 5K+
P: 8,434
So in the quoted example, your routine should return "11" - correct?
Assuming this is the case, here's some fairly quick and ugly code (VB6 style) which should do the trick...

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.   Dim I As Long, L As Long
  3.   Dim s As String, Found As String
  4.   Found = "Nothing"
  5.   Const Flag As String = "10101010"
  6.   s = Text1.Text
  7.   L = Len(s)
  8.   For I = L - 7 To 1 Step -1
  9.     If Mid$(s, I, 8) = Flag Then
  10.       Found = Mid$(s, I + 8)
  11.       Exit For
  12.     End If
  13.   Next
  14.   MsgBox "Found: " & Found
  15. End Sub
Jun 8 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.