1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Nim言語で文字列を中央寄せで表示する

1
Last updated at Posted at 2026-04-10

Nimのstd/strutilsに含まれるcenterを使うと良いです。

使い方

var s = "hello"
echo s.center(3) # -> "hello"
echo s.center(7) # -> " hello "
echo s.center(8) # -> " hello  "

結果の文字数を指定することができます。元の文字列よりも小さな文字数を指定した場合は元の文字列がそのまま返されます。

var s = "hello"
echo s.center(3,'.') # -> "hello"
echo s.center(7,'?') # -> "?hello?"
echo s.center(8,'_') # -> "_hello__"

埋める文字を指定することもできます。

使用例

奇数長の文字列が$N$個与えられるので、そのうち最も長いものに合わせて中央寄せで文字列を表示してください。ただし、埋める文字は.としてください。

実装例

import strutils,sequtils

var N = stdin.readLine().parseInt()
var S = newseqwith(N,stdin.readLine())
var m = S.mapit(len(it)).max()
echo S.mapit(it.center(m,'.')).join("\n")

その他

右揃えはalign、左揃えはalignLeftを使うと同じように実装できます。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?