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?

More than 1 year has passed since last update.

【UiPath】2文字以上の空白を含む文字列から、空白以外の文字列に分割したい

Last updated at Posted at 2022-06-11

2文字以上の空白を含む文字列

UiPathでの文字列操作は専用のアクティビティを使うケースもあれば、VBのコードを使うこともある。私が特定の文字列を分割しようとしたがうまくいかなかった。例えば以下のような例

"名前    身長 体重   備考   "

これを各文字列ごとに分割したい

"名前" "身長" "体重" "備考"

よくあるアプローチ

trim
str.trim

str.trimを使うと前後の空白は除くことができるが中間の空白は省くことができない。

"名前    身長 体重   備考"
split
str.split(" ")

str.split(" ")を使うと途中の空白も分割されてしまう。

"名前" " " " " "身長" "体重" " " "備考"

実装例

いろいろ調べたがよい方法が見つからなかったのでロジックを組んだ

contain.png

まずは空白が2文字つづくケースが存在するか判定する

str.Contains("  ")

空白が2文字を続く文字列を空白1文字に変換する

str.Replace("  ", " ")
"名前 身長 体重 備考 "

これをこれを行えば連続する空白を1文字の空白に変えることができた。
終わってみれば簡単だったが、2時間くらい調べてしまった。

image.png

この後によく使うSplitを入れ込むことで文字列を分割できる。

item.Split(CChar(" "))
"名前" "身長" "体重" "備考"

もし、コードで一発で出来るような方法があれば是非おしえてください。

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?