-ffs-AbodyRulez Posted February 13, 2014 Share Posted February 13, 2014 I'm scripting a GUI memo, but i want when someone inserts a string that contains maybe more than 10 words to separate them in a new line. For example the user entered this input: "Hello my name is Abdullah and i'm a member in ffs community." a for loop that will count the spaces if they == 11 and separate with a new line \n, so the output should be: "Hello my name is Abdullah and i'm a member in ffs community." thank you. Link to comment
TrapLord Studios™ Posted February 13, 2014 Share Posted February 13, 2014 Use if string ( >10 ) then Link to comment
-ffs-AbodyRulez Posted February 13, 2014 Author Share Posted February 13, 2014 lmao, thanks for this easy trick Link to comment
-ffs-AbodyRulez Posted February 13, 2014 Author Share Posted February 13, 2014 Wait i'm a bit lost here.. if (string(text) > 10) then text = text.."\n" ? Link to comment
Bonsai Posted February 13, 2014 Share Posted February 13, 2014 If the string is longer than 10 then you just use string.sup, grab the symbols from 1 to 10, insert a \n and append the rest of the string by doing string.sup from 11 to end. You might have to repeat this if the string need more than 2 lines. Link to comment
-ffs-AbodyRulez Posted February 13, 2014 Author Share Posted February 13, 2014 For a start I've created a function but i'm not sure how to finish it. function separatingLine(text) for(i = 0; i < string(text); i++){ if( string(text) > 10) then } end Link to comment
xXMADEXx Posted February 13, 2014 Share Posted February 13, 2014 -ffs-AbodyRulez said: For a start I've created a function but i'm not sure how to finish it. function separatingLine(text) for(i = 0; i < string(text); i++){ if( string(text) > 10) then } end Lua isn't like programming languages.. I think I figured out what you were trying to do, but i'm not sure... function separatingLine(text) local i = 0 while ( i < string.len ( text ) ) do -- your code i = i + 1 end end Link to comment
-ffs-AbodyRulez Posted February 13, 2014 Author Share Posted February 13, 2014 xXMADEXx the for loop is totally not my problem.. the problem is -- your code part Link to comment
Tete omar Posted February 13, 2014 Share Posted February 13, 2014 Here you go: function separateString ( str ) local formattedStr = "" while #str ~= 0 do local newline = str:sub ( 1, 11 ) str = str:gsub ( newline, "" ) formattedStr = formattedStr .. newline .. "\n" if ( #str == 0 ) then formattedStr = formattedStr:gsub ( "%\n$", "" ) end end return formattedStr end Link to comment
-ffs-AbodyRulez Posted February 13, 2014 Author Share Posted February 13, 2014 Thank you Tete, i didn't understand your code actually, but i've tried something and it worked for me i guess. text = 1234567890 text = string.sub(text,1, 4).."\n"..string.sub(text, 5, string.len(text)) outputChatBox(text) --[[ output : 1234 567890 --]] Link to comment
.:HyPeX:. Posted February 13, 2014 Share Posted February 13, 2014 i had a post about something similar before, it might help you in the future, the code was in the end this: local array = { x = 1, y = 2, z = 3, lx = 1, ly = 2, lz = 3 } function splitArrayIntoLines( _array ) if ( type( _array ) ~= "table" ) then return "" end local splitted = "" local formed = { } for i,v in pairs( _array ) do formed[ #formed + 1 ] = i .. ": " .. v end Link to comment
Tete omar Posted February 13, 2014 Share Posted February 13, 2014 -ffs-AbodyRulez said: Thank you Tete, i didn't understand your code actually, but i've tried something and it worked for me i guess. text = 1234567890 text = string.sub(text,1, 4).."\n"..string.sub(text, 5, string.len(text)) outputChatBox(text) --[[ output : 1234 567890 --]] No problem, this function will separate each 11 characters with a new line, this is all you need i guess? Link to comment
-ffs-AbodyRulez Posted February 13, 2014 Author Share Posted February 13, 2014 Thanks HyPeX Tete, your code works fine, but how do i adjust it to make a new line when reaching a space after 11 character ? (So it won't divide a word in the 9th character for example) Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now