473,386 Members | 1,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Quick string question

Hi,

I have a feeling there is a easy answer to this. I need to return the first
9 characters in a string if it is longer than 9 characters, otherwise the
whole string.

If was hoping this code would do this:

Dim myString as string="string"
msgbox (mystring.substring(0,9))

But this gives me a "Index and length must refer to a location within the
string." error.

I understand that i could do this with if statements, but i was wondering if
there is a easier ".Net" way to do this

Any ideas ?

Regards

Niclas
Nov 20 '05 #1
10 1226
e
How about something like this? Nothing ".Net" about it, really, just a way
of accomplishing the task without using an If:

Function First9Method(byval SomeString as string) as String

SomeString &= " "
return SomeString.Substring(0, 9).Trim

End Function

But whatcha got against If statements?
"MS Newsgroups" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a feeling there is a easy answer to this. I need to return the first 9 characters in a string if it is longer than 9 characters, otherwise the
whole string.

If was hoping this code would do this:

Dim myString as string="string"
msgbox (mystring.substring(0,9))

But this gives me a "Index and length must refer to a location within the
string." error.

I understand that i could do this with if statements, but i was wondering if there is a easier ".Net" way to do this

Any ideas ?

Regards

Niclas

Nov 20 '05 #2

I would suggest using IF statements anyway, there are, for example,
"interesting" ways of doing these things in C, but they tend to obscure the
meaning of the code. Same applies in any language really.

For example, which is more meaningful to the reader? Neither is quicker
than the other.

int c;

c = ( d > 0 ? 1 : -1 )

or

if d > 0
{
c = 1
}
else
{
c = -1
}

"MS Newsgroups" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a feeling there is a easy answer to this. I need to return the first 9 characters in a string if it is longer than 9 characters, otherwise the
whole string.

If was hoping this code would do this:

Dim myString as string="string"
msgbox (mystring.substring(0,9))

But this gives me a "Index and length must refer to a location within the
string." error.

I understand that i could do this with if statements, but i was wondering if there is a easier ".Net" way to do this

Any ideas ?

Regards

Niclas

Nov 20 '05 #3
e
In hindsight though, I guess you'd be screwed with that method if your
string legitimately ended in spaces you wanted to preserve. Then you'd have
to pad it with spaces after the trim, until the length is 9. Wouldn't it be
way easier for you and the processor to just use an If?

"e" <e@e.com> wrote in message news:qJ********************@speakeasy.net...
How about something like this? Nothing ".Net" about it, really, just a way of accomplishing the task without using an If:

Function First9Method(byval SomeString as string) as String

SomeString &= " "
return SomeString.Substring(0, 9).Trim

End Function

But whatcha got against If statements?
"MS Newsgroups" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a feeling there is a easy answer to this. I need to return the first
9 characters in a string if it is longer than 9 characters, otherwise the whole string.

If was hoping this code would do this:

Dim myString as string="string"
msgbox (mystring.substring(0,9))

But this gives me a "Index and length must refer to a location within the string." error.

I understand that i could do this with if statements, but i was

wondering if
there is a easier ".Net" way to do this

Any ideas ?

Regards

Niclas


Nov 20 '05 #4
I have nothing against using if statements, just wanted to check if there
was a simpler way that i missed

Thanks for your help guys

Niclas

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:bn*******************@news.demon.co.uk...

I would suggest using IF statements anyway, there are, for example,
"interesting" ways of doing these things in C, but they tend to obscure the meaning of the code. Same applies in any language really.

For example, which is more meaningful to the reader? Neither is quicker
than the other.

int c;

c = ( d > 0 ? 1 : -1 )

or

if d > 0
{
c = 1
}
else
{
c = -1
}

"MS Newsgroups" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a feeling there is a easy answer to this. I need to return the first
9 characters in a string if it is longer than 9 characters, otherwise the whole string.

If was hoping this code would do this:

Dim myString as string="string"
msgbox (mystring.substring(0,9))

But this gives me a "Index and length must refer to a location within the string." error.

I understand that i could do this with if statements, but i was

wondering if
there is a easier ".Net" way to do this

Any ideas ?

Regards

Niclas


Nov 20 '05 #5
Try this "If-like" statement:

MessageBox.Show(mystring.substring(0, IIf(mystring.length < 9,
mystring.length, 9))

-Barry

"MS Newsgroups" <no****@nospam.com> wrote in message
news:#L**************@TK2MSFTNGP09.phx.gbl...
Hi,

I have a feeling there is a easy answer to this. I need to return the first 9 characters in a string if it is longer than 9 characters, otherwise the
whole string.

If was hoping this code would do this:

Dim myString as string="string"
msgbox (mystring.substring(0,9))

But this gives me a "Index and length must refer to a location within the
string." error.

I understand that i could do this with if statements, but i was wondering if there is a easier ".Net" way to do this

Any ideas ?

Regards

Niclas

Nov 20 '05 #6
On Tue, 21 Oct 2003 17:29:57 +0100, "MS Newsgroups"
<no****@nospam.com> wrote:
Hi,

I have a feeling there is a easy answer to this. I need to return the first
9 characters in a string if it is longer than 9 characters, otherwise the
whole string.

If was hoping this code would do this:

Dim myString as string="string"
msgbox (mystring.substring(0,9))


MsgBox Strings.Left(myString,9)
Nov 20 '05 #7
Hi Niclas,

This is one of those situations where the retro VB functions score over
the .NET functions.

This works just like it always did. ;-)
myString = Left (myString, 9)

Except!! when you are in a class (eg Form) where there is a method called
Left. Then the compiler assumes that you mean Me.Left() and gets a bit cross.
[Just to be clear - it's fine in Modules and Classes without a Left().]

To get round <that> you use the fully qualified name.
myString = Microsoft.VisualBasic.Left (myString, 9)

But that's such a mouthful it's worse than all the If'ing that you're
trying to avoid.

So you add an aliased Imports:
Imports VB = Microsoft.VisualBasic

and you can then do
myString = VB.Left (myString, 9)

Regards,
Fergus
Nov 20 '05 #8
That was what i was looking for !

Many thanks

Niclas
"_Andy_" <wi******@nospamthanks.gov> wrote in message
news:tk********************************@4ax.com...
On Tue, 21 Oct 2003 17:29:57 +0100, "MS Newsgroups"
<no****@nospam.com> wrote:
Hi,

I have a feeling there is a easy answer to this. I need to return the first9 characters in a string if it is longer than 9 characters, otherwise the
whole string.

If was hoping this code would do this:

Dim myString as string="string"
msgbox (mystring.substring(0,9))


MsgBox Strings.Left(myString,9)

Nov 20 '05 #9
Niclas,
Instead of an if statement you could use Math.Min.

Something like:
Dim myString as string="string"
msgbox (mystring.substring(0,Math.Min(mystring.Length, 9)))
Or you could pad the length of the string, then do the sub string:
msgbox (mystring.PadRight(9).substring(0,9))
Which will actually ensure the length is 9, which may not be what you want.

Or as Fergus & Andy stated, use the Microsoft.VisualBasic functions.

Hope this helps
Jay

"MS Newsgroups" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... Hi,

I have a feeling there is a easy answer to this. I need to return the first 9 characters in a string if it is longer than 9 characters, otherwise the
whole string.

If was hoping this code would do this:

Dim myString as string="string"
msgbox (mystring.substring(0,9))

But this gives me a "Index and length must refer to a location within the
string." error.

I understand that i could do this with if statements, but i was wondering if there is a easier ".Net" way to do this

Any ideas ?

Regards

Niclas

Nov 20 '05 #10
"MS Newsgroups" <no****@nospam.com> schrieb
I have a feeling there is a easy answer to this. I need to return the
first 9 characters in a string if it is longer than 9 characters,
otherwise the whole string.

If was hoping this code would do this:

Dim myString as string="string"
msgbox (mystring.substring(0,9))

But this gives me a "Index and length must refer to a location within
the string." error.

I understand that i could do this with if statements, but i was
wondering if there is a easier ".Net" way to do this

Any ideas ?


msgbox Strings.Left(mystring, 9)
--
Armin

Nov 20 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.