Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Mon Jul 22, 2019 4:37 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Access individual characters of a string
PostPosted: Thu Feb 24, 2011 12:38 am 
Offline

Joined: Thu Apr 15, 2010 10:41 pm
Posts: 220
Location: Seattle
Is there a way to access individual characters in a string?

Say, I'm interested in the 9th, 14th, 19th, and 24th characters in a string.

Example of imaginary command "StringChar":

If StringChar(  "469BBC25-BAE0-4549-A7E5-28428F26C18D", 9 ) = "-"  ) Then


I know there are roundabout ways of doing this. But does REALbasic have a command to extract specific characters?

I looked at all these string commands:
http://docs.realsoftware.com/index.php/ ... xt_Strings

And I tried to search these forums. But this is not an easy subject on which to search.

_________________
--Basil Bourque

Using REAL Studio 2010 r5.1 with Postgres 9.0.3 on Mac OS X 10.6.5
http://crafted-software.blogspot.com/


Top
 Profile  
Reply with quote  
 Post subject: Re: Access individual characters of a string
PostPosted: Thu Feb 24, 2011 12:46 am 
Offline

Joined: Thu Oct 26, 2006 4:49 am
Posts: 307
Hi Basil,

You didn't look that hard did you?? :-)

Use the mid function with a value of 1 for length

http://docs.realsoftware.com/index.php/Mid

regards


Dave M


Top
 Profile  
Reply with quote  
 Post subject: Re: Access individual characters of a string
PostPosted: Thu Feb 24, 2011 12:58 am 
Offline
User avatar

Joined: Thu Aug 26, 2010 6:57 am
Posts: 569
Location: Sydney Australia
I assume you are trying to get rid of the "-" characters?
I use the below function to clean up xml before importing it into an app.
All you need to work out is the ascii for the character(s) you wish to filter (if they are sequential and in a range).
You might need to modify it slightly (as indicated in the comment)
Function CleanString(str as String, Lower as Integer, Upper as Integer) as String

Dim l as Integer = str.LenB
Dim final, bit as String = ""

If l <> 0 Then
For a As Integer = 0 To l
bit = MidB(str, a, 1)
// to kill of "-" only, replace below with If Asc(bit) <> 45 Then...
If Asc(bit) > Lower AND Asc(bit) < Upper Then final = final + bit
Next
Return final
Else
Return ""
End If

End Function

_________________
Regards

Chris Musty
http://www.specialised.net.au
-----------------------------------------------
if date.shortdate >= 4/6/2013 then
msgbox "I am using Xojo - YAY!!!"
else
msgbox "I am still using RS 12r2.1 - BOO!!!"
end if

Win 8 Pro, Ubuntu 13.04, Mint and Debian(gnome and KDE)
AWS for Hosting and TKL for AMI's


Top
 Profile  
Reply with quote  
 Post subject: Re: Access individual characters of a string
PostPosted: Thu Feb 24, 2011 8:55 pm 
Offline

Joined: Thu Apr 15, 2010 10:41 pm
Posts: 220
Location: Seattle
Quote:
You didn't look that hard did you??


I really did try! But I didn't grok "Mid", so thank you.

I was thinking in terms of this handy feature in the 4D language, where a number or variable between angle brackets means "extract character number N", like this:

Quote:
myChar = someString<i>


where the "ith" character of the text in the var "someString" is returned. Very simple code for text manipulation.

--Basil Bourque

_________________
--Basil Bourque

Using REAL Studio 2010 r5.1 with Postgres 9.0.3 on Mac OS X 10.6.5
http://crafted-software.blogspot.com/


Top
 Profile  
Reply with quote  
 Post subject: Re: Access individual characters of a string
PostPosted: Thu Feb 24, 2011 9:10 pm 
Offline

Joined: Thu Apr 15, 2010 10:41 pm
Posts: 220
Location: Seattle
To Specialised,

(a) Thanks for the code snipped. But I really was asking for random-access to individual characters. Useful for tearing apart fixed-length strings with multiple values compacted together.

(b) I see in your code that you are using the binary (byte/octet oriented) versions of the commands "Mid" & "Len" ("MidB" & "LenB"). I caution you against that, especially as you said you use it for manipulating XML. All XML tools, by definition, must be UTF-8 compatible. UTF-8 and other encodings can have multiple-octets per character. Your code may break when it encounters such XML files.

The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!) by Joel Spolsky

--Basil Bourque

_________________
--Basil Bourque

Using REAL Studio 2010 r5.1 with Postgres 9.0.3 on Mac OS X 10.6.5
http://crafted-software.blogspot.com/


Top
 Profile  
Reply with quote  
 Post subject: Re: Access individual characters of a string
PostPosted: Thu Feb 24, 2011 11:21 pm 
Offline
User avatar

Joined: Thu Aug 26, 2010 6:57 am
Posts: 569
Location: Sydney Australia
Quote:
Your code may break when it encounters such XML files.


Ironically this is why I have to use such code to "clean" the string as not everyone follows XML standards.

It hasn't broken on me yet but thanks for the article.

Actually I do use Mid in my code not MidB, that was copied from an old Post... might need to dig through an old app now!

_________________
Regards

Chris Musty
http://www.specialised.net.au
-----------------------------------------------
if date.shortdate >= 4/6/2013 then
msgbox "I am using Xojo - YAY!!!"
else
msgbox "I am still using RS 12r2.1 - BOO!!!"
end if

Win 8 Pro, Ubuntu 13.04, Mint and Debian(gnome and KDE)
AWS for Hosting and TKL for AMI's


Top
 Profile  
Reply with quote  
 Post subject: Re: Access individual characters of a string
PostPosted: Fri Feb 25, 2011 4:09 am 
Offline

Joined: Thu Feb 07, 2008 2:21 pm
Posts: 303
Location: Italy
Try this code:
Dim myString As String = "469BBC25-BAE0-4549-A7E5-28428F26C18D"
Dim myArray(-1) As String = Split(myString, "")

For i As Integer = 0 To Len(myString) - 1
MsgBox myArray(i)
Next i

Now, you can to access to an array composed by the characters of your string.
Bye :D

_________________
REAL Studio 2011 Release 4 Standard Edition on Windows 7 64bits
Database Engine : ADODB (Microsoft Access MDB) - Report Engine : RPDF


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group