String.length

書式

文字列オブジェクト.length

説明

文字列の文字数を返します。SHIFT JISなどで使用している日本語など2バイト文字も1文字としてカウントされます。Unicodeで複数のバイトが使用される文字でも正しくカウントされます。

サンプルコード [実行]

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>Sample</title>
</head>
<body>
<script type="text/javascript"><!--
str = new String("Sample text");
document.write(str.length);
// --></script>
</body>
</html>