[topic] 末尾の空白文字を取り除く

与えられた文字列の末尾の空白文字を取り除く方法と、その操作が与えられた文字列を破壊するかどうか。取り除かれる空白文字の種類。

Posted feedbacks - OCaml

見当たらなかったので自作しました。両方非破壊です。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
let rtrim ?(ch=" \t\n\011\012\013") str =
  let rec loop i =
    if i>=0 && String.contains ch str.[i]
    then loop (i-1)
    else String.sub str 0 (i+1)
  in loop (String.length str - 1);;
    
(*正規表現版*)
#load "str.cma";;
let rtrim2 str =
  let cond = Str.regexp "[ \009-\013]+$" in
  Str.global_replace cond "" str;;

Index

Feed

Other

Link

Pathtraq

loading...