Script/文字列の削除
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
[[書き比べスクリプト・リファレンス]]
* Python [#a817b41d]
# 文字列の先頭/末尾にある空白文字を削除:strip/lstrip/...
s1 = ' ==** sample text **== '
s2 = s1.strip() # 文字列s1の先頭/末尾にある空白文字を削除
print(s2) # ==** sample text **==
s2 = s1.strip(' =') # 引数に指定した文字を文字列s1の先頭...
print(s2) # ** sample text **
s2 = s1.lstrip(' =*') # 引数に指定した文字を文字列s1の先...
''rstrip''
s3 = s1.rstrip(' =*') # 引数に指定した文字を文字列s1の末...
print('lstrip:', s2) # lstrip: sample text **==
print('rstrip:', s3) # rstrip: ==** sample text
引数を省略した場合、行末の改行文字および空白文字を取り除く
# 特定のプレフィックス/サフィックスを削除:removeprefix...
s1 = '_start_ sample text _end_'
s2 = s1.removeprefix('_start_') # 文字列が'_start_'で始...
print(s2) # sample text _end_
s2 = s1.removesuffix('_end_') # 文字列が'_end_'で終わっ...
print(s2) # _start_ sample text
終了行:
[[書き比べスクリプト・リファレンス]]
* Python [#a817b41d]
# 文字列の先頭/末尾にある空白文字を削除:strip/lstrip/...
s1 = ' ==** sample text **== '
s2 = s1.strip() # 文字列s1の先頭/末尾にある空白文字を削除
print(s2) # ==** sample text **==
s2 = s1.strip(' =') # 引数に指定した文字を文字列s1の先頭...
print(s2) # ** sample text **
s2 = s1.lstrip(' =*') # 引数に指定した文字を文字列s1の先...
''rstrip''
s3 = s1.rstrip(' =*') # 引数に指定した文字を文字列s1の末...
print('lstrip:', s2) # lstrip: sample text **==
print('rstrip:', s3) # rstrip: ==** sample text
引数を省略した場合、行末の改行文字および空白文字を取り除く
# 特定のプレフィックス/サフィックスを削除:removeprefix...
s1 = '_start_ sample text _end_'
s2 = s1.removeprefix('_start_') # 文字列が'_start_'で始...
print(s2) # sample text _end_
s2 = s1.removesuffix('_end_') # 文字列が'_end_'で終わっ...
print(s2) # _start_ sample text
ページ名: