Last active
December 21, 2016 05:48
-
-
Save Pie001/6e1f89f3e9bd3bc3ef32 to your computer and use it in GitHub Desktop.
指定した長さで文字列を切る
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//指定した長さで文字列を切る | |
function GetStringByByte(str, num) { | |
len = 0; | |
estr = escape(str); | |
ostr = ""; | |
for (i = 0; i < estr.length; i++) { | |
len++; | |
ostr = ostr + estr.charAt(i); | |
if (estr.charAt(i) == "%") { | |
i++; | |
ostr = ostr + estr.charAt(i); | |
if (estr.charAt(i) == "u") { | |
ostr = ostr + estr.charAt(i + 1) + estr.charAt(i + 2) + estr.charAt(i + 3) + estr.charAt(i + 4); | |
i += 4; | |
len++; | |
} | |
} | |
if (len >= num - 3) { | |
return unescape(ostr) + "..."; | |
} | |
} | |
return unescape(ostr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment