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

Look for a character in a certain position

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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
Nov 21 '05 #2

P: n/a
"KJ" <kl******@hotmail.com> schrieb:
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 "".

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3

P: n/a
Herfried,

That was it, I knew it.

:-)

Cor
Nov 21 '05 #4

P: n/a


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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #5

P: n/a
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
Nov 21 '05 #6

P: n/a
"BillGatesFan" <no****@devdex.com> schrieb:
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].

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.