Thread Tools Display Modes
02-01-10, 12:51 PM   #1
nehegeb
A Murloc Raider
Join Date: Nov 2009
Posts: 9
Breaking strings in pieces...

I want to post my table input to the chat frame ingame. But as you know, there is a limit of 255 characters per message.

Here is my question:


I need to break the message into pieces with a maximum of 230 characters. After this, I want to post those pieces to the ingame chat, seperated by '...'.

In example:
string: "Hello there, I want to post a long message!"
Chatpost1: "Hello there, I want..."
Chatpost2: "...to post a long message!"

It also would be great, if this function would detect a whole word and won't split it due posting like in the following example.
Chatpost1: "Hello there, i want to po..."
Chatpost2: "...st a long message!"

I hope anybody can help me with that.
  Reply With Quote
02-01-10, 05:15 PM   #2
ArrchDK
A Fallenroot Satyr
 
ArrchDK's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 25
Code:
MAX_CHAR = 230
local myMsg = "This is my message..." -- Message you want to break up.
local myStringTbl = {} -- Table to store each line.

local function breakUpLine(msg)

	if string.len(msg) < MAX_CHAR then -- If our string is less than our per line char limit, then insert the whole line into table.
		tinsert(myStringTbl, "..."..msg)
	else
		index = MAX_CHAR -- Start at the MAX_CHAR character
		while (string.sub(msg, index, index) ~= " ") -- Go back character by character until we find a space.
			index = index - 1
		end

		tinsert( myStringTbl, "..."..string.sub(msg, 1, index - 1).."..." ) -- Insert a line into our table from the beginning of the string to character before the space.
		breakUpLine(string.sub(msg, index+1)) -- Run recursively, passing the string that starts from after our space character until the end.
	end
end

breakUpLine(myMsg) -- Run our recursive function.
myStringTbl[1] = string.sub(myStringTbl[1], 4) -- Removes the preceding ellipsis from the first line.
Untested, but it should do the trick.

When finished, you'll have a table with indeces of 1, 2, ... n where n is the number of lines:
myStringTbl[1] = "This is a string that..."
myStringTbl[2] = "...has been split up..."
myStringTbl[3] = "...based on a predetermined..."
myStringTbl[4] = "...number of max characters..."
myStringTbl[5] = "...per line stored in MAX_CHAR"

Last edited by ArrchDK : 02-01-10 at 05:28 PM. Reason: Add more info & clarification on the code.
  Reply With Quote
02-02-10, 10:45 AM   #3
nehegeb
A Murloc Raider
Join Date: Nov 2009
Posts: 9
Very nice! That's almost perfect!

I just added a ' myStringTbl = {nil} ' for clearing the table afterwards.

Just one thing:
The while statement doesn't work at all and till now I couldn't figure out why. I took it out so far. So it splits the string just where the MAX_CHAR is reached and cuts the word there. But I think I'll solve that tiny problem soon.

Thank you for your help, ArrchDK!
  Reply With Quote
02-02-10, 11:49 AM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
This should work to break between words.
lua Code:
  1. local MAX_LEN=255;
  2. function BreakMessage(msg)
  3.     if #msg<=MAX_LEN then return {msg}; end
  4.  
  5.     local tbl={""};
  6.  
  7.     for word in msg:gmatch("%S+") do
  8.         if #tbl[#tbl]+#word+1<=MAX_LEN-3 then
  9.             tbl[#tbl]=tbl[#tbl].." "..word;
  10.         else
  11.             tbl[#tbl]=tbl[#tbl].."...";
  12.             tbl[#tbl+1]="..."..word;
  13.         end
  14.     end
  15.  
  16.     return tbl;
  17. end
It would glitch if the string passed to it contains a word that has more letters than MAX_LEN.

The result of this glitch would look something like this...
Code:
msg=BreakMessage("OneWordThatExceedsMAXLEN");
-- msg[1] is "..."
-- msg[2] is "...OneWordThatExceedsMAXLEN"
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 02-02-10 at 11:53 AM.
  Reply With Quote
02-02-10, 12:58 PM   #5
nehegeb
A Murloc Raider
Join Date: Nov 2009
Posts: 9
hehe, i got the problem with that while statement:
there is missing a ' do '!

Code:
while (string.sub(msg, index, index) ~= " ") do
   index = index - 1
end
There it is! And now it works perfect!

But I also want to thank SDPhantom for your idea. Didn't thought about that way. But the while-thing is quite a bit more comfortable.

Last edited by nehegeb : 02-02-10 at 03:10 PM.
  Reply With Quote

WoWInterface » Developer Discussions » General Authoring Discussion » Breaking strings in pieces...

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off