How to find the middle string of a value

My sbyte return a text string, however it seems like on every instance when i read the com port it will add the last value. so the value is 000000000001 then on the second pass its 000000000001 0000FFFE0001 how can i only get the 0000FFFE0001 and of that get the FFFE value. I tried MidStr ( Mid(sBuffer, 5, 12)) but that didn;t seem to work.
If comm1.InBufferCount > 0 Then
            '  Arr = comm1.Input
            Dim sByte As String = String.Empty
            sByte = comm1.Input
            sBuffer = sByte
            comm1.InBufferCount = 0
        End If

Open in new window

cmdolcetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lee555J5Commented:
This is very basic and will return the 5th-8th characters after the first space found.
sBuffer = Mid(sByte, InStr(1, sByte, " ") + 5, 4)
Will your string change? Do we need to make this more robust?
Lee
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cmdolcetAuthor Commented:
my string will change every itteration so yes
0
lee555J5Commented:
I know your string will change. I am asking about the structure. What are other considerations? Are they all 12-digit [space] 12-digit? I am looking for some consistency to use to parse the string that will work every time for you.
Lee
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

cmdolcetAuthor Commented:
no its 12 digits no spaces no dashes
0
SawinerCommented:
i'm not too much familiar with the input(buffer?) you are using, but you should find a way to avoid duplicate inputs.

EIther way, MidStr and Mid are old vb.net functions left from vb6.
You should use String.Substring() (instance method) instead.

String inputText;
String inputTextFromMiddleLength = inputText.Substring(inputText.Length / 2);

VB.Net:
//InputText is a string exists of the text
Dim InputTextFromMidLen = InputText.Substring(InputText.Length / 2)

in your case though, you might want to first find the correct space index (0 for first message, 1 for second, etc) using split, and then get the data from it:

//String inputText = "000000000001 0000FFFE0001"
String[] spl = inputText .Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

String firstMsg = spl[0];
String secondMsg = spl[1];

note: Unfortunately my vb.net compiler is not installed so you'll have to try and convert those bits of codes to vb.net

If you want a function to give you back the text from the middle point, X characters to the right and left (e.g. X is 2 in your example):

public String GetFromMiddle(String input, Int32 length)
{
     Int32 start = input.Length / 2 - length / 2;
     return input.Substring(start,length);
}

vb.net:
Public Function GetFromMiddle(input as String, length as Int32) as String
     Dim StartOffset as Int32 = input.Length / 2 - length / 2
     return input.Substring(start, length)
End Function

in order to use it on our (second) message:
//String secondMsg = spl[1];
String finalResult = GetFromMiddle(secondMsg, 2);

Good luck.
0
Mohamed AbowardaSoftware EngineerCommented:
Try to use split() function
0
CodeCruiserCommented:
First, set your comm1.Input to String.Empty(unless its readonly) to get rid of duplicate values.

Do you want to get alpha values? Try this

sByte=sByte.Trim("0").Trim("1").Trim("0")
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.