Get character from a string at a particular position?

megaxoom

New Member
Joined
May 11, 2002
Messages
11
Hello

How do I get a character from a string at a particular position? For example: I have a string declared as follow

Dim str As String

and assume the str = "this is a string"

if I want to know a character at position 4, how do I do it besides using the Mid function.

Thanks inadvance for any help.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Is there any reasons why you don't want to use the Mid function?

anyways, here's another way...

Dim sTxt as String
sTxt = "12345"
MsgBox Replace(Left(sTxt, 4), Left(sTxt, 4 - 1), "")
 
Upvote 0
Public Sub StringDemo()
Dim MyStr As String
MyStr = "this is a string"
MsgBox ("The fourth letter is > " & Right(Left(MyStr, 4), 1))
End Sub
 
Upvote 0
I think using these functions (Mid, Left , Right) are a little bit slow. Is there a way to retrieve it very fast? In C language, to get a character from a string, just use the bracket and the position of the string that wanted to be retrieved.
For example, If I want to retrieve the fourth character from a string str (assumming str="this is a test"), all I have to do is to declare like this, str[4], and I get the character at that position.
 
Upvote 0
Hello Mega :cool:

Hmmmm .... I ran the following procedure that uses the "Mid" function exactly 1million times. It was able to perform the million Mid functions in 1 second. I'm not sure what kind of code your attempting to write but I'm doubtful that this type of time limitation is that big of a concern :oops: :rolleyes:

Public Sub TimeMidFunction()
Dim t As Date
t = Now()

For x = 1 To 1000000
MyStr = Mid("this is a string", 4, 1)
Next x

MsgBox Format(Now() - t, "hh:mm:ss")
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,615
Messages
6,120,538
Members
448,970
Latest member
kennimack

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top