String Functions

  • Thread starter Thread starter cmdolcet69
  • Start date Start date
C

cmdolcet69

What string function will cut off the last 3 characters from a string?

For example: if I had the string,

dim strtext as string = testcalls


I want to only return the strtext as test and take off the calls.
 
What string function will cut off the last 3 characters from a string?

For example: if I had the string,

dim strtext as string = testcalls

I want to only return the strtext as test and take off the calls.

Substring function.

Your example is not well-formed but i guess it's:
Dim strtext As String = "testcalls"

To display only "test" chars from whole "testcalls" word;

Substring(0,4)

which defines 0 is startIndex point and 4 is the length that belongs
the length of "test".
 
Substring function.

Your example is not well-formed but i guess it's:
Dim strtext As String = "testcalls"

To display only "test" chars from whole "testcalls" word;

Substring(0,4)

which defines 0 is startIndex point and 4 is the length that belongs
the length of "test".


What happends if i dont know the lenght. Because the lenght may change.
 
What happends if i dont know the lenght. Because the lenght may change.

If you only pass startIndex parameter without length parameter, the
substring function will return the string which is starting from
startIndex point till the rest of the whole string.

You can try and see:

For example If you do:
substring(4) only "calls" will be displayed because you're telling the
function returning rest of the whole string starting from startIndex
parameter.
 
Private Sub Test()
Dim StrText As String = "TestCalls"
MsgBox(Strings.Left(StrText, Len(StrText) - 5))
End Sub

'This is the more BASIC approach... ;-)

In order to cut off the 'Calls' part, you need to know the length of the
string: Len does that for you.
Since 'Calls' is 5 characters long, you substract this value from the
string length: And voilà, the MessageBox shows 'Test'.

In order to manipulate strings, there are 3 commands available:
- Strings.Left
- Strings.Mid
- Strings.Right

Of course, you could also use the DotNet version Substring. However, the
BASIC commands do some additional checks for you (e.g. if a string is
Nothing).

Try this:
Dim StrText2 As String = Nothing
MsgBox(Strings.Left(StrText2, 2)) 'MsgBox shows an empty text.

MsgBox(StrText2.Substring(0, 2)) 'Run-time error:
'Object reference not set to an instance of an object.

Best regards,

Martin
 
If you only pass startIndex parameter without length parameter, the
substring function will return the string which is starting from
startIndex point till the rest of the whole string.

You can try and see:

For example If you do:
substring(4) only "calls" will be displayed because you're telling the
function returning rest of the whole string starting from startIndex
parameter.- Hide quoted text -

- Show quoted text -

ok now what if i want the "test" to be returned. Remember im using
"test" as an example becuse i dont know the lenght of the string it
could be "test2"
 
ok now what if i want the "test" to be returned. Remember im using
"test" as an example becuse i dont know the lenght of the string it
could be "test2"

Well, you want to get only "test" or "test2" or whatever without
specifying length parameter? If so, here is my approach:

You can reverse the string 2 times that saves you from knowing the
length of "test" or "test2" etc:

Dim a As String = "test2calls"


a = StrReverse(a)

' Take of "calls" which consist of 5 chars
' So only pass StartIndex position parameter
a = a.Substring(5)

' Reverse again to see straight string
a = StrReverse(a)

MsgBox(a)

Tested and should work for you.

Regards,

Onur Güzel
 
What happends if i dont know the lenght. Because the lenght may change.

Dim strtext As String = "testcalls"

strtext = strtext.Substring(0, strtext.Length - 3)

But I think you really want 5 instead of 3 since "calls" is 5
characters long, not 3.

or:
strtext = strtext.Substring(0, strtext.Length - "calls".Length)

or
strtext = strtext.Remove(strtext.Length - "calls".Length)
 
cmdolcet69 said:
ok now what if i want the "test" to be returned. Remember im using
"test" as an example becuse i dont know the lenght of the string it
could be "test2"

Is this related to your previous posts regarding a password and user's
initials?

It sounds like you are now running into the realization that both the user's
password and the initials are unknown lengths. This is why it was suggested
to keep them separate. Many people have one or two intials (some Hindus that
I know), while many have more than 3 (predominantly Catholics). Passwords of
course have minimum requirements, but are rarely a fixed length.
 
Back
Top