Question

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

  • 7 December 2022
  • 7 replies
  • 76 views

Badge +1

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


7 replies

Userlevel 7
Badge +12

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?

Badge +4

Hi @Vinothkumar.N ,

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

Badge +1

 Example HST#320#HISTORY OF MICHIGAN 3#3.0

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

Userlevel 7
Badge +12

You mean,

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

or

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

Badge +1

Like 1st option

Userlevel 7
Badge +12

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 +1

Ok, Thanks for your help I will try this

Reply