Would Xojo benefit from a char datatype?

I find myself often thinking how nice it’d be to be able to say:

dim s as string = "abcdefghijklmnopqrstuv12345667"
dim magicChar as char = 'p'

for each c as char in s
  if c = magicChar then
    msgbox "magic char found!"
  end if
next

I mean, it certainly makes a lot more sense to a beginner than having to mess around with mid. To a beginner it’d probably be a lot easier to grasp the concept (and truth) of a string being a series of characters. Using mid all the time isn’t really very elegant either IMO.

Thoughts?

If you just want to search for a char in a string you can use InStrB instead of Mid but they are very similar :slight_smile:

If InStrB(s, “p”) > 0 Then
msgbox “magic char found”
End if

I would say thats easier than your suggestion above :wink:

…or what about…

  dim s as MemoryBlock = "abcdefghijklmnopqrstuv12345667"
  dim magicChar as byte = Asc("p")
  dim i as integer
  
  for i = 0 to s.Size
    if s.Byte(i) = magicChar then
      msgbox "magic char found!"
    end if
  next i 

I use MemoryBlocks almost exclusively when working with strings. Much easier and faster than using Mid.

MemoryBlocks might be tricky though when you start working with UTF character strings…

Good alternatives, thanks guys. I didn’t know about those two. It’s a shame I can’t give an answered status to both your posts.

for each c as string in s.split("") // ... next

It’s not as lightweight as a ‘char’ datatype might be, but it’s faster than repeatedly calling Mid (at the expense of more memory usage).

if you just want to know if a string is in another string, then this example in the Xojo Documentation

http://documentation.xojo.com/index.php/Extends
would be good.

then you can do

if s.contains(magicChar) then

end if

i am looking at Extends. where should i put the extends code??? eg like for the right and bottom

Function Right(Extends Ctl as RectControl) as integer
  return Ctl.Left + Ctl.Width
End Function

Function Bottom(Extends Ctl as RectControl) as integer
  return Ctl.Top + Ctl.Height
End Function

In a module.

thanks… it work now… i was putting it in the method for app which doesn’t work.