Trim whitespace from middle of string

2019-04-20 02:29发布

I'm using the following regex to capture a fixed width "description" field that is always 50 characters long:

(?.{50})

My problem is that the descriptions sometimes contain a lot of whitespace, e.g.

"FLUID        COMPRESSOR                          "

Can somebody provide a regex that:

  1. Trims all whitespace off the end
  2. Collapses any whitespace in between words to a single space

标签: regex parsing
7条回答
狗以群分
2楼-- · 2019-04-20 03:20
str = Regex.Replace(str, " +( |$)", "$1");
查看更多
登录 后发表回答