Skip to main content
Question

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

  • December 7, 2022
  • 7 replies
  • 1673 views

Forum|alt.badge.img+3

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

7 replies

Padmakumar
Premier Pathfinder | Tier 7
Forum|alt.badge.img+15
  • Premier Pathfinder | Tier 7
  • December 7, 2022

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?


Zaid Chougle
Navigator | Tier 3
Forum|alt.badge.img+17
  • Navigator | Tier 3
  • December 7, 2022

Hi @Vinothkumar.N ,

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


Forum|alt.badge.img+3
  • Author
  • Navigator | Tier 3
  • December 7, 2022

 Example HST#320#HISTORY OF MICHIGAN 3#3.0

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


Padmakumar
Premier Pathfinder | Tier 7
Forum|alt.badge.img+15
  • Premier Pathfinder | Tier 7
  • December 7, 2022

You mean,

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

or

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


Forum|alt.badge.img+3
  • Author
  • Navigator | Tier 3
  • December 7, 2022

Like 1st option


Padmakumar
Premier Pathfinder | Tier 7
Forum|alt.badge.img+15
  • Premier Pathfinder | Tier 7
  • December 7, 2022

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.


Forum|alt.badge.img+3
  • Author
  • Navigator | Tier 3
  • December 7, 2022

Ok, Thanks for your help I will try this