文字列のセンタリング
Posted feedbacks - R
> for(s in c("1234567890", "abc", "abcdef", "abcdefg", "abcdefghijkl")){
+ print(center(s, 10))
+ }
[1] "1234567890"
[1] " abc "
[1] " abcdef "
[1] " abcdefg "
[1] "bcdefghijk"
1 2 3 | center <- function(s, n){
format(substring(s, (i<-(nchar(s)-n)/2+1), i+n-1), width=n, justify="centre")
}
|

nobsun
#4089()
Rating0/2=0.00
文字列を指定のカラム幅にセンタリング配置する関数を示してください。文字列の長さが指定した幅より長い場合には文字列の両端をできるだけ均等に切り落して指定幅に収めてください。1文字は1カラムに収まるものと仮定してかまいません。
[ reply ]