Skip to content

Instantly share code, notes, and snippets.

@Pie001
Last active December 21, 2016 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pie001/6e1f89f3e9bd3bc3ef32 to your computer and use it in GitHub Desktop.
Save Pie001/6e1f89f3e9bd3bc3ef32 to your computer and use it in GitHub Desktop.
指定した長さで文字列を切る
//指定した長さで文字列を切る
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