Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default 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!



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default 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!





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
compare strings - highlight characters which are different DJS Excel Programming 8 November 23rd 07 06:24 PM
common characters in seperate strings [email protected] Excel Discussion (Misc queries) 5 February 28th 07 06:50 PM
Lookup strings with ~ and ^ characters kmhnhsuk Excel Worksheet Functions 6 May 25th 06 08:30 PM
Replacing characters in numeric text strings and SUMming rkd Excel Discussion (Misc queries) 7 April 20th 06 12:25 PM
characters and strings elicamacho Excel Discussion (Misc queries) 4 March 20th 06 06:17 PM


All times are GMT +1. The time now is 07:58 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"