Equivalent to Mid function but read from right of the string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All,

I have a string variable, whose length is not constant. I need to extract
the whole string, except the last character in the string. Is there any
string function that can do this?

Something like the Mid function, but extracts the string from the right; so
that I could extract the string from 2nd character till the end of the
string, but start reading from the right.

Thanks.
kd
 
¤ Hi All,
¤
¤ I have a string variable, whose length is not constant. I need to extract
¤ the whole string, except the last character in the string. Is there any
¤ string function that can do this?
¤
¤ Something like the Mid function, but extracts the string from the right; so
¤ that I could extract the string from 2nd character till the end of the
¤ string, but start reading from the right.

Probably more than one way to do this, but the Substring method of the String class is similar to
the Mid function.


Paul
~~~~
Microsoft MVP (Visual Basic)
 
Kd
Roughly typed.
\\\
dim s as string = "kd"
s = s.substring(0,s.length-1)
///
Or
 
I have a string variable, whose length is not constant. I need to
extract the whole string, except the last character in the string. Is
there any string function that can do this?

s2=Left(s1, Len(s1)-1)
Something like the Mid function, but extracts the string from the
right; so that I could extract the string from 2nd character till the
end of the string, but start reading from the right.

s2=StrReverse(Mid(s1, 2))

Andrew
 
kd said:
I have a string variable, whose length is not constant. I need to extract
the whole string, except the last character in the string. Is there any
string function that can do this?

Something like the Mid function, but extracts the string from the right;
so
that I could extract the string from 2nd character till the end of the
string, but start reading from the right.

\\\
Const Text As String = "axxxxxxxxxxxxxxxxz"

' Everything except last character.
MsgBox(Strings.Left(Text, Len(Text) - 1))

'Everything except first character.
MsgBox(Strings.Right(Text, Len(Text) - 1))
///
 
Dim MyString as string = "Some useful data!"
Dim MyPartialString as string

then use

MyPartialString = MyString.Substring(0,MyString.Length - 1)

or

MyPartialString = Microsoft.VisualBasic.Left(MyString,MyString.Length - 1)

Bobbo
 
If you really just want a FUNCTION to do this, use

Private Function SubstringRev(ByVal InputString as String, ByVal Start as
Integer) as String
Return InputString.Substring(0, InputString.Length - Start)
End Function

Bobbo
 
You may want to try something like this:

Dim strlen as Integer
Dim MyString as String
strlen=MyString.Length
MyString=Mystring.Substring(1,strlen-1)

This will give you everything but the last character of the string,
regardless of the length of the string. However, if the string length is 0,
you'll get an exception, so you'll need to test the string length first.

HTH
Lee
 

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

Back
Top