Quick string question

  • Thread starter Thread starter MS Newsgroups
  • Start date Start date
M

MS Newsgroups

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
 
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?
 
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
}
 
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?
 
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
 
Try this "If-like" statement:

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

-Barry
 
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)
 
MS Newsgroups said:
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)
 
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
 
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
 
Back
Top