JavaScript 文字列操作 新しいページはコチラ

提供: yonewiki
移動: 案内, 検索
(文字列位置区間(後方位置指定可能)による抽出(返却値は抽出文字列) slice)
(文字列中の文字による分割(返却値は文字列配列) split)
 
826行: 826行:
  
 
:*第一引数:取り出したい文字の開始位置番号
 
:*第一引数:取り出したい文字の開始位置番号
:*第二引数:取り出したい文字の終了位置番号
+
:*第二引数:取り出したい文字の終了位置番号、指定した文字位置は含まれないので、実際には抜き出し終了文字の次の文字位置を指定します。不数を指定すると末尾からの文字位置番号を指定できます。
  
  
834行: 834行:
  
 
document.write(".slice() : ",strData23, " ,検索結果 = ", strData23.slice(8,10), "</BR>");
 
document.write(".slice() : ",strData23, " ,検索結果 = ", strData23.slice(8,10), "</BR>");
 +
document.write(".slice() : ",strData23, " ,検索結果 = ", strData23.slice(8,-1), "</BR>");
 
</syntaxhighlight>
 
</syntaxhighlight>
  
843行: 844行:
  
 
document.write(".slice() : ",strData23, " ,検索結果 = ", strData23.slice(8,10), "</BR>");
 
document.write(".slice() : ",strData23, " ,検索結果 = ", strData23.slice(8,10), "</BR>");
 +
document.write(".slice() : ",strData23, " ,検索結果 = ", strData23.slice(8,-1), "</BR>");
 
</yjavascript>
 
</yjavascript>
  
 
== '''文字列位置区間による抽出(返却値は抽出文字列) substring ''' ==
 
== '''文字列位置区間による抽出(返却値は抽出文字列) substring ''' ==
 +
文字列オブジェクトに対して、substringメソッドの引数に取り出したい文字の開始終了ポジションを指定すると、その抽出文字列が取得できます。
  
 +
*strData = strData23.substring(8,10)
 +
 +
 +
:*第一引数:取り出したい文字の開始位置番号
 +
:*第二引数:取り出したい文字の終了位置番号、指定した文字位置は含まれないので、実際には抜き出し終了文字の次の文字位置を指定します。
 +
 +
 +
サンプルスクリプト
 +
<syntaxhighlight lang="javascript" line start="1">
 +
strData24 = new String("文字列位置による抽出(substring)");
 +
 +
document.write(".substring() : ",strData24, " ,検索結果 = ", strData24.substring(8,10), "</BR>");
 +
</syntaxhighlight>
 +
 +
 +
表示結果:
 +
 +
<yjavascript>
 +
strData24 = new String("文字列位置による抽出(substring)");
 +
 +
document.write(".substring() : ",strData24, " ,検索結果 = ", strData24.substring(8,10), "</BR>");
 +
</yjavascript>
  
 
== '''文字列位置と文字数による抽出(返却値は抽出文字列) substr ''' ==
 
== '''文字列位置と文字数による抽出(返却値は抽出文字列) substr ''' ==
 +
文字列オブジェクトに対して、subメソッドの引数に取り出したい文字の開始ポジションとその文字数を指定すると、その抽出文字列が取得できます。
  
 +
*strData = strData25.substr(3,7)
 +
 +
 +
:*第一引数:取り出したい文字の開始位置番号
 +
:*第二引数:取り出したい文字数。
 +
 +
 +
サンプルスクリプト
 +
<syntaxhighlight lang="javascript" line start="1">
 +
strData25 = new String("文字列位置による抽出(substr)");
 +
 +
document.write(".substring() : ",strData25, " ,検索結果 = ", strData25.substr(3,7), "</BR>");
 +
</syntaxhighlight>
 +
 +
 +
表示結果:
 +
 +
<yjavascript>
 +
strData25 = new String("文字列位置による抽出(substr)");
 +
 +
document.write(".substring() : ",strData25, " ,検索結果 = ", strData25.substr(3,7), "</BR>");
 +
</yjavascript>
  
 
== '''文字列中の文字による分割(返却値は文字列配列) split ''' ==
 
== '''文字列中の文字による分割(返却値は文字列配列) split ''' ==
 +
文字列オブジェクトに対して、splitメソッドの引数に分割したい文字Token(トークン:つまりは分割の記号とか文字列)を指定すると、その分割文字で分割した文字列が配列の要素となって取得できます。
 +
 +
*strData = strData26.split("-_-",3)
 +
 +
 +
:*第一引数:分割のトークンにしたい文字列
 +
:*第二引数:取り出したい配列数(上限)。
 +
 +
 +
サンプルスクリプト
 +
<syntaxhighlight lang="javascript" line start="1">
 +
strData26 = new String("文字列位置による抽出(split)-_-文字列1-_-文字列2-_-文字列3");
 +
 +
document.write(".split() : ",strData26, " ,検索結果 = ", strData26.split("-_-",3), "</BR>");
 +
</syntaxhighlight>
 +
 +
 +
表示結果:
 +
 +
<yjavascript>
 +
strData26 = new String("文字列位置による抽出(split)-_-文字列1-_-文字列2-_-文字列3");
 +
 +
document.write(".split() : ",strData26, " ,検索結果 = ", strData26.split("-_-",3), "</BR>");
 +
</yjavascript>
 +
 
[[JAVA Script#リファレンス]]に戻る。
 
[[JAVA Script#リファレンス]]に戻る。

2016年7月28日 (木) 00:00時点における最新版



個人用ツール
名前空間

変種
操作
案内
ツールボックス