I have a 1 string with first name and last name that I want on separate line

I will have random names returned to me in a string “friend_name”.  However the name will be random:

Joe Smith

Abigail Aberachy

I  present data like so:  display.newText(friend_name, 0, 0, config.thinFont, 34)

However I dont want the names to print on the same line.  I want first name on one line and last name directly underneath.  Can someone please assist me on a way to do this so no matter the length of 1st, last name, it will display on two lines.  Thank you!

something like this:

namelist = friend_name:split(’ ')

for i = 1, #namelist do

    name = display.newText(namelist[i], …)

    name.y = 100 + i * 40

end

I am getting error, any other thoughts please?

/Users/russm305/Desktop/rdi-client-coronav2/corona/view/friend.lua:431: cannot use ‘…’ outside a vararg function near ‘…’

stack traceback:

[C]: in function ‘error’

the … is just a placement. Use the real parameters there, like font, positions, size etc.

Okay thank you,  ran again getting error with split…

2015-06-26 19:52:18.408 Corona Simulator[19889:48867] Runtime error

/Users/russm305/Desktop/rdi-client-coronav2/corona/view/friend.lua:429: attempt to call method ‘split’ (a nil value)

stack traceback:

/Users/russm305/Desktop/rdi-client-coronav2/corona/view/friend.lua:429: in function ‘item’

 

 

 

namelist = friend_name:split(’ ')

for i = 1, #namelist do

    name = display.newText(namelist[i], 0, 0, config.thinFont, 34)

    name.y = 100 + i * 40

 

end

split is not a standard function for Lua. You have to write your own or find one on the Internet. We provided one in a tutorial:

https://coronalabs.com/blog/2013/04/16/lua-string-magic/

Rob

local s = {"Joe Smith", "Abigail Aberachy"} for i=1, #s do for k, v in string.gmatch( s[i], "(%w+) (%w+)" ) do local a=k local b=v print (a.."\n"..b) end end

from here should be easy to get what you want.

Just in case you haven’t noticed this already, but using the code above you will get multiple lines if the user’s first name or surname has a space in it.

E.g. “Jon Bon Jovi” would become 

Jon  

Bon  

Jovi

rather than

Jon  

Bon Jovi

So you’ll need to take that into account when planning out your UI. Is there no way that the API you are getting the names from can supply you with “firstName” and “surname” as separate fields?

the example i gave is just that…so he can take if from there…there a lot of approaches he can go depending for the origin of the data.

he can check letter by letter till he gets a “space”. that will be the first name, and the rest of the string will be the last name.

other approach is trying to get the first name and last name seperated with a “,” for example. from there is pretty easy to separate both.

Yes, it’s a difficult one to balance if you have no control over the source data.  

While you’d want to split at the first space for “Jon Bon Jovi”, you’d might want to split on the last space for “Billy Ray Cyrus”.

And who knows where it would be best to split “Billy Ray Bon Jovi”…

in my country first name is just that…FIRST name. a first name can’t have 2 names… Guess in english that’s possible. if  you can’t control the source data (which is the n.1 best option and should be the way to go), the solution is a little more complex then. you need a table of execptions, word counting and thats all. if you have more than 2 words in a line you need to compare the middle names with the execption table from there you can decide where to cut ;), still i believe this is not the way to go…the Alan solution or mine trying to change the source would be the correct solution.

something like this:

namelist = friend_name:split(’ ')

for i = 1, #namelist do

    name = display.newText(namelist[i], …)

    name.y = 100 + i * 40

end

I am getting error, any other thoughts please?

/Users/russm305/Desktop/rdi-client-coronav2/corona/view/friend.lua:431: cannot use ‘…’ outside a vararg function near ‘…’

stack traceback:

[C]: in function ‘error’

the … is just a placement. Use the real parameters there, like font, positions, size etc.

Okay thank you,  ran again getting error with split…

2015-06-26 19:52:18.408 Corona Simulator[19889:48867] Runtime error

/Users/russm305/Desktop/rdi-client-coronav2/corona/view/friend.lua:429: attempt to call method ‘split’ (a nil value)

stack traceback:

/Users/russm305/Desktop/rdi-client-coronav2/corona/view/friend.lua:429: in function ‘item’

 

 

 

namelist = friend_name:split(’ ')

for i = 1, #namelist do

    name = display.newText(namelist[i], 0, 0, config.thinFont, 34)

    name.y = 100 + i * 40

 

end

split is not a standard function for Lua. You have to write your own or find one on the Internet. We provided one in a tutorial:

https://coronalabs.com/blog/2013/04/16/lua-string-magic/

Rob

local s = {"Joe Smith", "Abigail Aberachy"} for i=1, #s do for k, v in string.gmatch( s[i], "(%w+) (%w+)" ) do local a=k local b=v print (a.."\n"..b) end end

from here should be easy to get what you want.

Just in case you haven’t noticed this already, but using the code above you will get multiple lines if the user’s first name or surname has a space in it.

E.g. “Jon Bon Jovi” would become 

Jon  

Bon  

Jovi

rather than

Jon  

Bon Jovi

So you’ll need to take that into account when planning out your UI. Is there no way that the API you are getting the names from can supply you with “firstName” and “surname” as separate fields?

the example i gave is just that…so he can take if from there…there a lot of approaches he can go depending for the origin of the data.

he can check letter by letter till he gets a “space”. that will be the first name, and the rest of the string will be the last name.

other approach is trying to get the first name and last name seperated with a “,” for example. from there is pretty easy to separate both.

Yes, it’s a difficult one to balance if you have no control over the source data.  

While you’d want to split at the first space for “Jon Bon Jovi”, you’d might want to split on the last space for “Billy Ray Cyrus”.

And who knows where it would be best to split “Billy Ray Bon Jovi”…