Jump to content

Separating a string with a new line "Help Required"


Recommended Posts

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
  -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

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

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
  -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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...