[SOLVED] How to get the string length?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
YounYokel
Prole
Posts: 39
Joined: Thu Oct 03, 2019 5:57 pm

[SOLVED] How to get the string length?

Post by YounYokel »

Hey, is there a way to get a string length?
Like this:

Code: Select all

local textStr = "Hello World"
local textLen = textStr:getLength

function love.draw( dt )
    love.graphics.print( tostring( textLen ) ) -- Returns 11
end
Last edited by YounYokel on Sun Oct 06, 2019 12:12 pm, edited 1 time in total.
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: How to get the string length?

Post by MrFariator »

Any of these methods work.

Code: Select all

-- all print 4
local str = "asdf"
print(#str)
print(string.len(str))
print(str:len())
This page lists the available string methods.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to get the string length?

Post by zorg »

That said, the # operator is the preferred method, since iirc the others might be deprecated now.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
YounYokel
Prole
Posts: 39
Joined: Thu Oct 03, 2019 5:57 pm

Re: How to get the string length?

Post by YounYokel »

MrFariator wrote: Sun Oct 06, 2019 11:25 am Any of these methods work.

Code: Select all

-- all print 4
local str = "asdf"
print(#str)
print(string.len(str))
print(str:len())
This page lists the available string methods.
Thank you! Didn't know about the hashtag.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: [SOLVED] How to get the string length?

Post by ivan »

There is a caveat to this.
If your string contains non-English characters you need to use UTF8 to the get the "real" length:

Code: Select all

local utf8 = require('utf8')
local str = "абвгд"
print(#str)
print(utf8.len(str))
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to get the string length?

Post by zorg »

YounYokel wrote: Sun Oct 06, 2019 12:12 pm Thank you! Didn't know about the hashtag.
Hash. Not hashtag. Let's not mix twitter functionality with what the character is called, please. :o:
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Post Reply

Who is online

Users browsing this forum: darkfrei, Google [Bot] and 219 guests