Please Help How Do I Search A String for Specific Charecters

By zukeru ·
I am using from visual studio 2008
I need to take a string like .2503AR56Sl10000.0132 and only take specific charecters after Sl, this is a constantly changing string changes every 50ms. The length of the string is not static. It can change from being 19 charecters long to 5 charecters long. The only constant is that Sl will be in the string. I need to take Sl and the 5 charecters after it out of the string and display them as another string. Also I need it to check against if Sl has 5 charecters after it and if it doesn to do nothing. There current code i have is

if instr(returndata, "Sl") <> 0 then
label1.text = returndata
end if

however that only returns the entire string and since the string is not static nor the size is not static, i need to know how to say to vb display the 5 charecters after Sl. I dont even want it to display Sl I want it to display the 5 charecters after.

I can't say what this project is for do to security reasons, however i would be great if someone could help.

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

Some String methods might come in handy

by Tony Hopkinson In reply to Please Help How Do I Sear ...

this is c# but the methods on the string type are .Net.

int posSI = SIString.IndexOf("SI");
if (posSI >=0)
string SIPart = SIString.SubString(posSI,7);
if (SIPart.Length == 7)
// all is golden..

Less VB, more .Net

Collapse -

Error I get when i use that code

by zukeru In reply to Some String methods might ...

So I used your idea for the string I had tried that already however I get an error,

I get "ArgumentOutOfRangeException was unhandled"
Startindex cannot be less than zero. Parameter name: Startindex

My code

dim networkstream as networkstream = tcpclient.getstream()
if networkstream.canwrite and networkstream.canread then
dim bytes(tcpclient.recievebuffersize) as byte, 0, CInt(tcpclient.recievebuffersize))
dim returndata as string = encoding.ascii.getstring(bytes)
dim slpart as string
dim possl as integer
possl = returndata.indexof("Sl")
slpart = returndata.substring(possl, 7)
if slpart = 7 then
label1.text = slpart
end if

Collapse -

You missed some lines out

by Tony Hopkinson In reply to Error I get when i use th ...

the error you are getting is that possl < 0 which means "Sl" wasn't in your data.

possl = returndata.indexof("Sl")
if possl >= 0 then
slpart = returndata.substring(possl, 7)
if slpart.LENGTH = 7 then
label1.text = slpart
end if
end if

C# to VB.Net and vice versa is fairly simple

int i
instead of
dim i as int

if (condition)
//do dosomething)
instead of
if condition then
// do something
end if

is about it in this snippet, the rest of it revolves around the .net type string.

Suggest you have a wee go at C# (Visual studio express is free), the best way to learn one language is to learn another one.

Related Discussions

Related Forums