Lua language implementation: intercepts part of the length of a given string, beyond the partial ellipsis "..." substitution

Source: Internet
Author: User
Tags lua

During the project development process, especially game development, when writing client programs using the LUA scripting language, the player's role name length needs to be adapted. If the player's character name is long enough to exceed a given box, it will obscure the image in the other part of the game, and the effect is ugly, so the player's experience is not good. In order to give users a good experience, often planning will ask, for example, the maximum length of the role name is 18, but the 12-bit is already out of the box, then you can only display 9 bits, the remainder is replaced with ellipses, so that the entire role name in the given box, the experience of the player is better.

Here are some of the things I've encountered in game development.

There are no pre-modification scenarios:


Planning often requires the client to shorten the role name, but does not affect reading. Then, the character is truncated and the remainder is replaced with an ellipsis.

Because of the game, many module interface needs to display the role name, so, consider whether to write a public function getshortname (), when a module interface needs to display the role name, and the role name of the box, call this function getshortname () You can display the appropriate role name.

The following is the situation I encountered in the development of the game, I use this method, first in the public method function Globaldefine.lua definition public method Getshortname (), to other module user calls, for reference only.

The function is defined as follows:

[email protected] cuts the string and uses "..." to replace the tail [email protected]sname: String to be cut [Email protected]nmaxcount, The upper bound of the string, in multiples of 2 [email protected]nshowcount: Displays the number of English characters, a multiple of 2 in the text, and can be empty [email protected]function getshortname ( Sname,nmaxcount,nshowcount) If SName = = Nil or nMaxCount = Nil then return end local SSTR = SName Loca L Tcode = {} local tname = {} Local nleninbyte = #sStr local nwidth = 0 if Nshowcount = Nil then NSHOWC Ount = NMaxCount-3 End for i=1,nleninbyte do local curbyte = String.byte (sStr, i) Local byteCount =        0;            If Curbyte>0 and curbyte<=127 then ByteCount = 1 ElseIf curbyte>=192 and curbyte<223 Then ByteCount = 2 ElseIf curbyte>=224 and curbyte<239 then ByteCount = 3 ElseIf curbyt e>=240 and curbyte<=247 then ByteCount = 4 end Local char = nil if byteCount > 0 Then char = String.sub (SSTR, I, i+bytecount-1) i = i + byteCount-1 End If ByteCount = = 1 Then nwidth = nwidth + 1            Table.insert (Tname,char) Table.insert (tcode,1) ElseIf byteCount > 1 Then nwidth = nwidth + 2 Table.insert (Tname,char) Table.insert (tcode,2) end End If nwidth > nMaxCount then local _SN = "" Local _len = 0 for I=1, #tName do _sn = _sn.        Tname[i] _len = _len + tcode[i] If _len >= Nshowcount then break end End sName = _sn. "..." End return Snameend
The Getshortname () function is then called in the module interface Scenebattlesetment.lua file that needs to use the role name:

--note SName is the player's name    local sName = Wbattleglobal:getcurrent (): Getherowithid (Self.playerids[index]). m_splayername    Local ttfname1 = Element:getchildelement ("Teammatename")    assert (Ttfname1, "Ttfname is nil")    Local Sshortname = Getshortname (sname,12,7)   --The calling function displays the appropriate role name    Wzuilabelttf:luato (ttfname1): SetText (Sshortname)
After the call, the effect is as follows:




Similar in other places, the effect is as follows:


Lua language implementation: intercepts part of the length of a given string, beyond the partial ellipsis "..." substitution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.