ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Characters in Strings (https://www.excelbanter.com/excel-programming/381188-re-characters-strings.html)

Chip Pearson

Characters in Strings
 
John,

To get a single character, use Mid. E.g,.

Dim C As String
C = Mid("abcdefg", 5, 1)

will put "e" in C (5th character, length 1)/

I'm used to strings
being arrays of characters so this seems mad!


Strings in VB/VBA are actually what are called BSTRs, and part of
COM/OLE/Automation. See
http://msdn2.microsoft.com/en-us/library/ms221069.aspx

You can treat them as if they were arrays of characters. You can't use
pointers as you do in C/C++. Instead of a point to a character, you use an
1-based index into the string.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)



"John Pritchard" wrote in message
...
I wanted to read along a string character by character in VB and ended up
with a home made function :-

Public Function GetChr(ByVal InPutStr As String, ByVal position As
Integer)
As String

If position <= Len(InPutStr) Then
GetChr = Right(Left(InPutStr, position), 1)
Else
GetChr = Chr(0)
End If

End Function

Is there an easier (and more efficient) way than this. I'm used to strings
being arrays of characters so this seems mad!




Chip Pearson

Characters in Strings
 
John,

I should have added that you should become familiar with the InStr and
InStrRev functions, which are VB/VBA's equivalent of C's strstr function.
InStr searches left-to-right and InStrRev searches right-to-left. Both
return the 1-based offset (always from the left) of the first character of
the search-for string within the search-in string. Both return 0 if the
seach-for string is not found.

VB/VBA also has a StrComp function which is essentially the same as C's
strcmp function, except that you can specify whether the comparison it
case-sensitive or case-insensitive.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"Chip Pearson" wrote in message
...
John,

To get a single character, use Mid. E.g,.

Dim C As String
C = Mid("abcdefg", 5, 1)

will put "e" in C (5th character, length 1)/

I'm used to strings
being arrays of characters so this seems mad!


Strings in VB/VBA are actually what are called BSTRs, and part of
COM/OLE/Automation. See
http://msdn2.microsoft.com/en-us/library/ms221069.aspx

You can treat them as if they were arrays of characters. You can't use
pointers as you do in C/C++. Instead of a point to a character, you use an
1-based index into the string.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)



"John Pritchard" wrote in
message ...
I wanted to read along a string character by character in VB and ended up
with a home made function :-

Public Function GetChr(ByVal InPutStr As String, ByVal position As
Integer)
As String

If position <= Len(InPutStr) Then
GetChr = Right(Left(InPutStr, position), 1)
Else
GetChr = Chr(0)
End If

End Function

Is there an easier (and more efficient) way than this. I'm used to
strings
being arrays of characters so this seems mad!







All times are GMT +1. The time now is 09:18 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com