Skip to content

Pro Tip: Inserting Text Quickly in a Xojo iOS App

Manipulating text can be a time-consuming operation. I recently found myself with the need to insert text in various places in a large text document. Normally I would use a regular expression to solve this problem, but this is an iOS app and Xojo doesn’t have regular expressions available for iOS just yet. So I wrote some code to loop through the document and find and replace. Then I wrote some better code to do it a lot faster.

In my app, the text document was an HTML page. I needed to find each reference to an image and change it to point to a different location as the images were no longer in their original location. This code loops through the document, finds each image and replaces the image’s filename with an updated one:

 dim searchPos as integer = -1
 const quote = """"
   searchPos = searchPos + 1
   searchPos = PageContent.IndexOf(searchPos, "src=" + quote)
   if searchPos > -1 then
     'we found the next image file name reference
     dim firstQuotePos as integer = searchPos + 5
     dim secondQuotePos as integer = PageContent.IndexOf(firstQuotePos + 1, quote)
     dim oldFileName as text = PageContent.Mid(firstQuotePos, (secondQuotePos - 1) - (firstQuotePos - 1))
     oldFileName = oldFileName.Replace(".jpg", ".png").Replace(".jpeg", ".png")
     dim filename as text = "LRDB_Images/" + oldFileName
     dim url as text =
     PageContent = PageContent.Replace(quote + oldFileName + quote, quote + filename + quote)
     searchPos = searchPos + (filename.Length - oldFileName.Length)
     searchPos = PageContent.IndexOf(searchPos, "src=" + quote)
   end if
 loop until searchPos = -1
 Return PageContent

While this worked, it turned out to be quite slow. Before adding this code, the pages (which can be quite large) displayed instantly. After adding this code to remove old filenames and insert new ones, there was a noticeable delay before the page appeared. I needed a better solution. While reviewing the code with our engineer Greg, he came up with the idea of using Xojo’s ReplaceAll function to insert the new filenames. As it turned out, the filenames themselves had not changed. It was simply that the new ones needed to include the path to the new location where the file would be found. All that really had to be done was to replace “src=” with “src=” followed by a the path to the new location. Since this path is identical for every file, we realized that we could use the ReplaceAll function to insert text. The 18 lines of code above that were not fast enough were replaced by the four lines below that are lightning fast:

 const quote = """"
 Dim imgDir as Folderitem = SpecialFolder.GetResource("LRDB_Images")
 pageContent = pageContent.ReplaceAll("src=" + quote, "src=" + quote + imgDir.URLPath)
 pageContent = pageContent.ReplaceAll(".jpg", ".png").ReplaceAll(".jpeg", ".png")

It was not at all obvious to me that I could use a Replace function to insert text. Next time you need to insert text, take a look at the Replace and ReplaceAll functions to see if you can do it faster and with a lot less code.