Curl Global Community

Full Version: 文字列の中身が数字かどうか
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
String の中の値が数字かどうかを確かめる良い方法はありますか?

Code:
{let s:Strign = "123"}
{if {String {s.to-int}} == s then
    {popup-message "int"}
}

この方法だと、int型は判断できるのですが、小数点付きの値などもいちいちやっていくのが面倒で・・・
数字かどうかというのはどこまで厳しく確認するつもりでしょうか。

正規表現使えますね。
trim->正規表現 ("\d*\.?\d*"的に)->MatchStateを使ったり...
こんなんでどうですか?

Code:
{define-proc public {dou? str:String}:bool
    {return {regexp-match? "^(-?)([1-9][0-9]*)?[0-9](\\.[0-9]*[1-9])?$", {str.trim-clone}}}
}