この章では、Curl® 言語で文字列に関して実行できる作業のいくつかを説明します。具体的には、次の項目について説明します。
Curl 言語には、文字列のデータ型を変換する方法がいくつかあります。
| 読み取り専用 | 読み取り / 書き込み | 要約 |
to-double | Y | Y | 文字列から double 返すメソッド |
文字列から
double を返すには、
to-double メソッドを使用します。次の例でこのメソッドを示します。
例:
double への変換 |
 |
{value
|| Declare and initialize a string.
let s:String = "365e-10"
|| Declare and initialize a double to hold the return
|| value from a call to "to-double".
let number:double = {s.to-double}
|| Display the double.
number
}
| |
| 読み取り専用 | 読み取り / 書き込み | 要約 |
to-int | Y | Y | 文字列から int を返すメソッド |
文字列から
int を返すには、
to-int メソッドを使用します。次の例でこのメソッドを示します。
例:
to-int メソッドの使用 |
 |
{value
|| Declare and initialize a string.
let s:String = "-0X16D"
|| Declare and initialize an int to hold the return
|| value from a call to to-int.
let number:int = {s.to-int}
|| Display the int.
number
}
| |
| 読み取り専用 | 読み取り / 書き込み | 要約 |
to-String | Y | Y | String に文字列を変換するメソッド |
to-String を使用して、任意の文字列クラスで作成された文字列を String に変換します。to-String メソッドは String オブジェクトを返します。次の例でこのメソッドを示します。
例:
文字列への変換 |
 |
{value
|| Define and initialize a StringBuf.
let sb:StringBuf = {StringBuf "Hello World!"}
|| Convert the StringBuf to a String.
let s:String = {sb.to-String}
|| Display the contents of the String.
{value s}
}
| |
メソッド / プロシージャ | 読み取り専用 | 読み取り / 書き込み | 要約 |
StringInterface.to-InputStream | Y | Y | 入力ストリームに文字列を変換するメソッド |
例:
ストリームへの変換 (パート 1) |
 |
|| Declare and initialize a read-only string.
{let s:String = "Hello World"}
|| Convert the string to an input stream
{let tis:TextInputStream = {s.to-InputStream}}
|| Output the value of the input stream.
{tis.read-one-string}
|| Close the text input stream.
{tis.close}
| |
| 読み取り専用 | 読み取り / 書き込み | 要約 |
split | Y | Y | 指定した文字で文字列を分割するメソッド |
指定した文字が見つかるたびに文字列を分割するには、
split メソッドを使用します。既定では、このメソッドは空白の各位置で文字列を分割します。他の文字で文字列を分割するには、
split-chars キーワード引数を使用してその文字を含む
CharClass オブジェクトを渡します。
CharClass オブジェクトの詳細については、「
文字クラス」を参照してください。このメソッドは、分割された文字列を含む配列を返します。次の例でこのメソッドを示します。
例:
文字列の分割 (パート 1) |
 |
{value
|| Declare and initialize a string.
let s:String = "www.curl.com"
|| Split the string at the periods.
let a:StringArray = {s.split split-chars="."}
|| Declare and instantiate a VBox for displaying
|| an output message indicating the results.
let out:VBox = {VBox}
|| For each element in the results array, add it
|| to the output message.
{for i:int = 0 to (a.size - 1) do
{out.add {value a[i]}}
}
|| Display the string and the output message.
{value out}
}
| |
文字列に存在しない文字を使用して分割した場合、このメソッドは 1 つの StringInterface を含む配列を返します。この StringInterface には元の文字列が格納されています。次に例を示します。
例:
文字列の分割 (パート 2 ) |
 |
{value
|| Declare and initialize a string.
let s:String = "www.curl.com"
|| Use the colon character (:) to split the string.
let a:StringArray = {s.split split-chars=":"}
|| Declare and instantiate a VBox for displaying
|| an output message indicating the results.
let out:VBox = {VBox}
|| For each element in the results array, add it
|| to the output message.
{for i:int = 0 to (a.size - 1) do
{out.add {value a[i]}}
}
|| Display the string and the output message.
{value out}
}
| |
空の文字列を分割した場合、このメソッドは StringInterface を含まない配列を返します。次に例を示します。
例:
文字列の分割 (パート 3) |
 |
{value
|| Declare and initialize an empty string.
let s:String = ""
|| Split the string. By default, the method splits
|| at whitespace.
let a:StringArray = {s.split}
|| Display the number of StringInterfaces in the array.
{value a.size}
}
| |
Curl 言語には、文字列を比較するいくつかの方法があります。
メソッド / プロシージャ | 読み取り専用 | 読み取り / 書き込み | 要約 |
compare | Y | Y | 2 つの文字列を比較するメソッド |
StringInterface-leq? | Y | Y | StringInterface が対象文字列以下かどうかを調べるプロシージャ |
String-leq? | Y | Y | String が対象文字列以下かどうかを調べるプロシージャ |
2 つの文字列を比較するには、compare メソッドを使用します。このメソッドでは、文字列の比較に各文字の Unicode 値を使用します。つまり、文字列の辞書比較を行ないます。Unicode では、大文字の値は対応する小文字の値より小さくなる点に注意してください。たとえば、a の Unicode 値は 0x0061 ですが、A の Unicode 値は 0x0041 です。大文字と小文字を区別しない場合は、ignore-case?=true キーワード引数を指定します。
この文字列の比較対象の文字列を引数としてメソッドに与えます。引数が文字列と同じ場合、メソッドは 0 を返し、大きい場合は -1、小さい場合は 1 を返します。次の例でこのメソッドを示します。
例:
compare を使用して文字列を比較 |
 |
{value
|| Declare and initialize two strings.
let s1:String = "Hello World!"
let s2:String = "hello world!"
|| Compare the two strings and display an
|| appropriate message.
{switch {s1.compare s2}
case -1 do
{text String 1 is less than String 2.}
case 0 do
{text String 1 is equal to String 2.}
else
{text String 1 is greater than String 2.}
}
}
| |
文字列を比較するいくつかのプロシージャもあります。StringInterface-leq? は、StringInterface が対象文字列以下かどうかを調べます (Curl 言語では StringInterface はすべての文字列のスーパークラスです)。StringInterface-leq? を使用して Curl 言語の 2 つの文字列オブジェクトを比較できます。あるいは、String-leq? プロシージャで String が別の String 以下かどうかを調べます。両方のプロシージャの呼び出しでは、比較する 2 つの文字列オブジェクトを引数として与えます。最初の引数が次の引数以下の場合、これらのプロシージャは true を返します。次の例では String-leq? を示しています。
例:
string-leg を使用した文字列の比較 |
 |
{value
|| Declare and initialize two strings.
let s1:String = "Hello World!"
let s2:String = "hello world!"
|| Compare the two strings and display an
|| appropriate message.
{if {String-leq? s1, s2} then
{text String 1 is less than or equal to String 2.}
else
{text String 1 is greater than String 2.}
}
}
| |
2 つの文字列の内容が等しいかどうかを調べるには、equal? メソッドを使用します。比較対照となる文字列を引数として指定します。大文字と小文字の区別をしない場合は、ignore-case?=true キーワード引数を与えます。このメソッドは、2 つの 文字列が等しいかどうかを示す bool 値を返します。次の例でこのメソッドを示します。
例:
equal? を使用した文字列の比較 |
 |
{value
|| Declare and initialize two strings.
let s1:String = "Hello World!"
let s2:String = "Hello World!"
|| Test whether the contents of the two strings are
|| equal and display an appropriate message.
{if {s1.equal? s2} then
{text The contents of the strings are equal.}
else
{text The contents of the strings are NOT equal.}
}
}
| |
同等演算子 (== ) または不同等演算子 (!=) を使って、
各文字毎に、大文字と小文字を区別しながら二つの文字列を比較することもできます。
例:
同等演算子 == を使った文字列の比較 |
 |
{value
|| Declare and initialize two strings.
let s1:String = "Hello World!"
let s2:String = "Hello World!"
|| Test whether the contents of the two strings are
|| equal and display an appropriate message.
{if s1 == s2 then
{text The contents of the strings are equal.}
else
{text The contents of the strings are NOT equal.}
}
}
| |
Curl 言語には、文字列内の部分文字列を検索するいくつかの方法があります。
Curl 言語には、文字列の高度なパターン マッチング処理を実行するための正規表現ライブラリが含まれています。詳細は「
正規表現」を参照してください。
メソッド / プロシージャ | 読み取り専用 | 読み取り / 書き込み | 要約 |
find | Y | Y | 指定した文字との最初の一致を検索するメソッド |
find-string | Y | Y | 指定した部分文字列の最初の一致を検索するメソッド |
find-char-class | Y | Y | 指定した CharClass で一致する最初の文字を検索するメソッド |
Curl 言語の文字列では、以下のタスクも実行できます。
format マクロを使用して文字列の出力を制御できます。
format マクロは、C プログラミング言語の書式設定コードをサポートしています。詳細は、『API リファレンス マニュアル』の
format を参照してください。
| 読み取り専用 | 読み取り / 書き込み | 要約 |
empty? | Y | Y | 文字列が空かどうかを示すアクセッサ |
文字列が空かどうかを判定するには、
empty? アクセッサを使用します。このアクセッサは文字列が空かどうかを示す
bool 値を返します。次の例ではこのアクセッサを示しています。
例:
文字列が空かどうかをテスト |
 |
{value
|| Declare and initialize an empty string.
let s:String = ""
|| Check whether the string is empty and display an
|| appropriate message.
{if s.empty? then
{text The string is empty!}
else
{text The string has characters!}
}
}
| |
| 読み取り専用 | 読み取り / 書き込み | 要約 |
size | Y | Y | 文字列のサイズを示すアクセッサ |
読み取り専用の文字列の場合、size は文字列の文字数を返します。次の例は、読み取り専用の文字列でこのアクセッサを示しています。
例:
文字列のサイズの取得 |
 |
{value
|| Declare and initialize a read-only string.
let s:String = "Hello World!"
|| Output a message that includes the return value of
|| a call to "size".
{text There are {value s.size} characters in the string.}
}
| |
| 読み取り専用 | 読み取り / 書き込み | 要約 |
size | N | Y | 文字列のサイズを示すアクセッサ |
読み取り / 書き込み可能な文字列の場合、size は次を実行します。
- 文字列の文字数を返します。
- 文字列の文字数を設定できます。
現在の文字列のサイズより小さく設定すると、Curl® 実行環境 (RTE) はその文字列を切り捨てます。現在の文字列のサイズより大きく設定すると、該当する数の文字(Unicode の 16 進数の値 0000) が文字列の最後に追加されます。次の例は、読み取り / 書き込み可能な文字列でこのアクセッサを示しています。
例:
文字列のサイズの設定 |
 |
|| Declare and initialize a read-write string.
{let sb:StringBuf = {StringBuf "Hello World!"}}
|| Set the size of the string to 5 characters.
|| The string should now contain only the first five
|| characters "Hello".
{set sb.size = 5}
|| Output a message that includes the new string.
{text If size is set to 5, the string
becomes: {value sb}}
|| Set the size of the string to 10 characters.
|| The string should now have 5 characters
|| with the Unicode (hexadecimal) value 0000 at the end.
{set sb.size = 10}
|| Output a message that includes the new string.
{text Then, if size is set to 10, the string
becomes: {value sb}}
| |
メソッド / プロシージャ | 読み取り専用 | 読み取り / 書き込み | 要約 |
prefix? | Y | Y | 指定した文字で文字列が開始するかどうかを調べるメソッド |
指定した文字で文字列が開始するかどうかを調べるには、prefix? メソッドを使用します。対象となる文字を含む文字列を引数として、メソッド呼び出しに与えます。既定では、このメソッドで文字列の先頭の文字を比較する際に、大文字と小文字が区別されます。大文字と小文字の区別をしない場合は、ignore-case?=true キーワード引数を指定します。次の例ではこのメソッドを示しています。
例:
文字列の先頭を調べる |
 |
|| Declare and initialize a string.
{let s:String = "Hello World!"}
|| Check whether the string begins with "Hello" (case sensitive).
{value
{s.prefix? "Hello"}
}
|| Check whether the string begins with "hello" (case sensitive).
{value
{s.prefix? "hello"}
}
|| Check whether the string begins with "hello" (not case sensitive).
{value
{s.prefix? "hello", ignore-case?=true}
}
| |
メソッド / プロシージャ | 読み取り専用 | 読み取り / 書き込み | 要約 |
suffix? | Y | Y | 指定した文字で文字列が終了するかどうかを調べるメソッド |
指定した文字で文字列が終了するかどうかを調べるには、suffix? メソッドを使用します。対象となる文字を含む文字列を引数として、メソッド呼び出しに与えます。既定では、このメソッドで文字列の末尾の文字を比較する際に、大文字と小文字が区別されます。大文字と小文字の区別をしない場合は、ignore-case?=true キーワード引数を指定します。次の例ではこのメソッドを示しています。
例:
文字列の末尾を調べる |
 |
|| Declare and initialize a string.
{let s:String = "Hello World!"}
|| Check whether the string ends with "World!" (case sensitive).
{value
{s.suffix? "World!"}
}
|| Check whether the string ends with "world!" (case sensitive).
{value
{s.suffix? "world!"}
}
|| Check whether the string ends with "world!" (not case sensitive).
{value
{s.suffix? "world!", ignore-case?=true}
}
| |
Copyright © 1998-2019 SCSK Corporation.
All rights reserved.
Curl, the Curl logo, Surge, and the Surge logo are trademarks of SCSK Corporation.
that are registered in the United States. Surge
Lab, the Surge Lab logo, and the Surge Lab Visual Layout Editor (VLE)
logo are trademarks of SCSK Corporation.