Look for a character in a certain position

K

KJ

How can you look for a character in a certain position with VB.Net and
if the string length is less than the position have it return and
empty string instead of an error?

Substring returnns an error if the length is less than the position of
the character you want.


Ex. Dim x As String = 'ABCD'


x.Substring(5,1) returns error

I need a way to get the character or not return an error.

I don't want to have to do this

If x.length = 5 then
x.substring(5,1)
End If


Thanks
 
C

Cor Ligthert

KJ,

I don't want to have to do this

If x.length = 5 then
x.substring(5,1)
End If

This above will fail with an error

If x.lenght > 5 then y = x.substring(5.1)

I do not know shorter solutions.

I hope it helps?

Cor
 
H

Herfried K. Wagner [MVP]

KJ said:
How can you look for a character in a certain position with VB.Net and
if the string length is less than the position have it return and
empty string instead of an error?

Substring returnns an error if the length is less than the position of
the character you want.


Ex. Dim x As String = 'ABCD'


x.Substring(5,1) returns error

I need a way to get the character or not return an error.

Use 'Mid' instead if 'Substring', it won't throw an exception in this case
and will return "".
 
B

BillGatesFan

Wow. I did not know that. Is Mid one of those old VB6 functions that
creeped over to .NET? In my company we are not allowed to use old VB 6
functions like CStr and CInt. Thanks
 
C

Cor Ligthert

Bill,
Wow. I did not know that. Is Mid one of those old VB6 functions that
creeped over to .NET? In my company we are not allowed to use old VB 6
functions like CStr and CInt. Thanks

Do you use the = from old VB6 as well not?

CStr and CInt are full dotNet methods, however not in the basic namespace
but in the namespace.
Microsoft.VisualBasic which is sometimes confused with the
Microsoft.VisualBasicCompatibility namespace, whih will be temporaly.

Although I never use the MID. But that is because of the for me confusing
indexing, which start at One while methods not in the basic Net namespace
start with the historical Zero indexer what I am used too.

Cor
 
H

Herfried K. Wagner [MVP]

BillGatesFan said:
Wow. I did not know that. Is Mid one of those old
VB6 functions that creeped over to .NET?

You are programming VB.NET, not ".NET". Yes, 'Mid' was available in VB6
too, and is still available in VB.NET, and will be available in VB 2005, and
will maybe exist even longer than the .NET Framework will exist.
In my company we are not allowed to use old VB 6
functions like CStr and CInt.

Stupid rules!!

"Use VB.NET, but do not use it!"

Forbidding the use of these functions is simply a sign for the
/incompetence/ of the person who set up these rules.

[Yes, you are granted rights to print out this mail and show it the person
who invented these stupid rules].
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top