Split text by a number of characters

Hi Everybody,

I have a string variable with a text with hundred of letters. I would like to split the text in lines of 60 letters + EndOfLine. I want to show the splitted text in a textarea and then export it into a *.TXT file. I don’t get to split it, I have tried to use the split function. How could I do it?

Thanks,
Sergio

Split doesn’t let you say “split it into chunks this size”
It finds a specific substring and breaks you string apart at those substring boundaries

A loop using Mid would work

Something like

dim chunks() as string
for i as integer = 1 to myString.Length step 60
        chunks.append myString.mid(i, 60)
next

Thank you very much Norman, it works perfectly now.

Sergio

In processing a string or text area like that, how do you account to spaces or new lines in the string? as well as words that split on the 60 character barrier?

Are you trying to count or separate words ?

Sequences?

words so that I would be able to properly print to a graphic line without splitting the word at the end of the line printed.
e.g. using char width and height I would be able to determine that I could print, say, 80 chars on a line. I would want to
do something akin to a word wrap function in python or C

In python or C would simply look for the first white space prior to 80 char length in the string and split on that character.

In Xojo you can do just the same with Mid().
http://documentation.xojo.com/index.php/mid

Got that part down. Guess I need a few details on programming within Xojo, e.g. to look at a specific character in a string.

dim chunks() as string
dim tt as integer
dim s as text = F_instructions.text.ToText

dim kl as integer = s.length mod 60
dim lt as integer = floor(s.length/60)

for i as integer = 1 to (lt * 60 ) step 60
chunks.append s.mid(i, 60)
msgbox(s.mid(i,60))
tt=i

next
msgbox(s.mid(tt,60))
tt=tt+kl-1
msgbox(s.mid(tt,60))

I can process the string using mid to break it up into words in the the array chunks() but am not quite up to speed on processing arrays in Xojo. E.g. What returns the number of elements in the array? From that I could easily reassemble strings suitable for the print job.

Explaining the snippet above. Assuming the text field is 200 or more characters in length, and in this case I simply am going to look at chunks of 60 chars at a time, I use both the modulo() and floor() function to avoid outofbounds limits. Then I simply parse the text into 60 char chunks. One option is to find the first white space from the right end of each chunk and using a bit of magic rebuild n-chunks to be printed, one for each line.

The other option would be to split the text field into words and reassemble lines based on the number of words that would not exceed the line length I need.

Just looking for ideas to see how others would resolve this issue.

Word wrap is built into the Graphics.DrawString method. Simple supply the ‘width’ parameter.

g.DrawString(allOfTheText, x, y, wrapWidth)

Many ways to skin a … Here’s one idea I came up with that works for my needs. But I’m always open to new ideas.

In Action block for a button.

Dim TheWords(-1) As string
theWords = TextArea1.text.Split(" “) // text area field burst into an array of words.
dim t1, t2 as integer
dim i, tmpstring as string
t1=0
t2=0
for each i in theWords
t1=t1+1
t2=i.totext.length + t2
if t2 < 80 then
tmpstring=tmpstring + " " + i
else
msgbox(tmpstring)
tmpstring=”"
t2=0
end if

next

Assuming you have no fractional numbers in your text, you can do that to take punctuation into account :

// Normalize punctuation // // Make sure there is no space following punctuation TextArea1.Text = ReplaceAll(TextArea1.Text, ", ", ",") TextArea1.Text = ReplaceAll(TextArea1.Text, "; ", ";") TextArea1.Text = ReplaceAll(TextArea1.Text, ". ", ".") TextArea1.Text = ReplaceAll(TextArea1.Text, ": ", ":") TextArea1.Text = ReplaceAll(TextArea1.Text, "! ", "!") TextArea1.Text = ReplaceAll(TextArea1.Text, "? ", "?") TextArea1.Text = ReplaceAll(TextArea1.Text, ") ", ")") TextArea1.Text = ReplaceAll(TextArea1.Text, "> ", ">") TextArea1.Text = ReplaceAll(TextArea1.Text, "] ", "]") TextArea1.Text = ReplaceAll(TextArea1.Text, "” ", "”") // Curly quote right // Make sure every punctuation is followed by a space TextArea1.Text = ReplaceAll(TextArea1.Text, ",", ", ") TextArea1.Text = ReplaceAll(TextArea1.Text, ";", "; ") TextArea1.Text = ReplaceAll(TextArea1.Text, ".", ". ") TextArea1.Text = ReplaceAll(TextArea1.Text, ":", ": ") TextArea1.Text = ReplaceAll(TextArea1.Text, "!", "! ") TextArea1.Text = ReplaceAll(TextArea1.Text, "?", "? ") TextArea1.Text = ReplaceAll(TextArea1.Text, ")", ") ") TextArea1.Text = ReplaceAll(TextArea1.Text, ">", "> ") TextArea1.Text = ReplaceAll(TextArea1.Text, "]", "] ") TextArea1.Text = ReplaceAll(TextArea1.Text, "”", "” ") // Curly quote right theWords = TextArea1.text.Split(" ") // text area field burst into an array of words.

If the text contains such figures as “1,234.56” then 1 234 and 56 will be treated as separate words. In order to avoid that you can check for all combinations such as 1,1 1,2 1,3 and do replacements of comma and dot by special characters (for instance ?and ?) in a couple loops before the split and the reverse operation before reassembling line.

Straight quotes are difficult to deal with as they can be both at beginning and end of words. They would require replacing all space-" by some special character such as space ? then proceed as other punctuation and reinstate them after the split.

If the end goal is to draw text onto a Canvas, it is much easier to leave that to the wrap parameter in drawString, as advised by Will Shank.

nice! thanks

Got to find notes on “wrap parameter in drawString,” which will save a lot of pain and despair on the canvas print.

[quote=223481:@Ben Joyner]nice! thanks

Got to find notes on “wrap parameter in drawString,” which will save a lot of pain and despair on the canvas print.[/quote]

The LR is your friend : http://documentation.xojo.com/index.php/Graphics.DrawString

Got that… even added the note in my code… odd for me to do that… What would be the syntax to include an
extra line feed character after a period?

[quote=223484:@Ben Joyner]Got that… even added the note in my code… odd for me to do that… What would be the syntax to include an
extra line feed character after a period?[/quote]

ReplaceAll(sometext, EndOfLIne, endOfLine+EndOfLine)

In case it did not work (happens with text coming from a socket or the disk), use ReplaceLineEndings before the ReplaceAll. http://documentation.xojo.com/index.php/ReplaceLineEndings

Thanks Michel. It is very gratifying to find you are posting references. As I get more up to speed, I’ll know better how to look up stuff on the docs.

Actually, using DrawString with word wrap did exactly what I was attempting… the hard way.

[quote=223487:@Ben Joyner]Thanks Michel. It is very gratifying to find you are posting references. As I get more up to speed, I’ll know better how to look up stuff on the docs.

Actually, using DrawString with word wrap did exactly what I was attempting… the hard way.[/quote]

You’re welcome. If I may, I found your posts interesting, especially your last methods, with a typical C flavor. This to say that Xojo is flexible enough to adapt to different coding styles, but yet, its real strength purely as a language IMHO resides in the high level of abstraction available. Here, one single line of DrawString and you’re done.

Indeed. The graphics functions of Xojo are almost identical to the ones I used ages ago to hand code PS and PDF. As with any high level abstraction, having a grasp of the API doc is essential. But hey, I only picked this up last week and already have a beta package for a friend which includes drag-n-drop images, printing to pdf, as well as a rather robust sqlite interface. Definitely worth the investment. My day job is as an IT Engineer. This, it play stuff.