How to find the middle string of a value

Posted on 2010-04-06
Medium Priority
Last Modified: 2012-05-09
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

Question by:cmdolcet
LVL 13

Accepted Solution

lee555J5 earned 1000 total points
ID: 29971320
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?

Author Comment

ID: 29971401
my string will change every itteration so yes
LVL 13

Expert Comment

ID: 29971624
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.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


Author Comment

ID: 29971916
no its 12 digits no spaces no dashes

Expert Comment

ID: 29972070
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);

//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);

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.
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 29998550
Try to use split() function
LVL 83

Expert Comment

ID: 30042244
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


Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…

599 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question