Question

How to remove the last space from a string using regex?

  • 7 December 2022
  • 7 replies
  • 957 views

Badge +2

How to remove the last space from a string using regex?


7 replies

Userlevel 7
Badge +13

HI @Vinothkumar.N ,

 

In general, you can do the following method to remove white spaces.

Search for ^[ \t]+  and replace with empty value → To trim leading White space

Search for [ \t]+$ and replace with empty value  → To trim trailing White space

 

Else, simply put .trim with your Variable name to automatically trim down the white space.

 

Could you please provide an example to understand your query?

Userlevel 4
Badge +10

Hi @Vinothkumar.N ,

If you want to remove last space from a string you can use trim action under string package.

Badge +2

 Example HST#320#HISTORY OF MICHIGAN 3#3.0

Before the last space I need to add an # instead of space

Userlevel 7
Badge +13

You mean,

like this HST#320#HISTORY OF MICHIGAN#3#3.0

or

like this HST#320#HISTORY OF MICHIGAN 3#3.0#

Badge +2

Like 1st option

Userlevel 7
Badge +13

If that’s the case, then you can split the text HST#320#HISTORY OF MICHIGAN and 3#3.0 assign it to separate variables. Then you can use [ \t]+$ and replace with #. Finally join them.

Badge +2

Ok, Thanks for your help I will try this

Reply