Skip to main content
Solved

Remove the last character from a string if it's a comma?

  • June 26, 2015
  • 5 replies
  • 1928 views

Forum|alt.badge.img

Hi

 

 

I'm sure I should get this, but I seem to have a mental block, maybe Friday afternoon related. I have a string attribute where some of the features contain a comma as the last character which I want to get rid of. What's the most effective way of doing this?

Thanks,

 

Riley

Best answer by takashi

 

Hi Riley,

 

 

There are several ways. I would use the StringReplacer with this parameter setting.

 

-----

 

Text to Match: ,$

 

Replacement Text: <leave this paramter blank. i.e. empty stirng>

 

Use Regular Expressions: yes

 

-----

 

The regex ",$" matches to the last comma in a string. Replacing matched part with the empty string, that's equivalent to removing the matched part.

 

By default, the StringReplacer does nothing if there were no matched parts in the source string.

 

 

Takashi
This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

5 replies

jdh
Contributor
Forum|alt.badge.img+40
  • Contributor
  • June 26, 2015

Well first you need to idenify which features contain the comma, the two ways I can think of is:

 

1: SubstringExtractor (-1,-1), tester (_substring = , )

 

2. StringSearcher (RegEx ,$)

 

 

Then a SubstringExtractor of (0,-2) on the features containing the comma.

Forum|alt.badge.img
  • June 26, 2015
I tried once the Attribute Trimmer. It worked for me to make a list or delet the features over the comma. 

 

Try it maybe! 

Forum|alt.badge.img
  • June 26, 2015
I need to correct my last comments. I meant AttributeSplitter. 

 

And for Delimiter and Format String just put a "," 

takashi
Celebrity
  • Best Answer
  • June 27, 2015

 

Hi Riley,

 

 

There are several ways. I would use the StringReplacer with this parameter setting.

 

-----

 

Text to Match: ,$

 

Replacement Text: <leave this paramter blank. i.e. empty stirng>

 

Use Regular Expressions: yes

 

-----

 

The regex ",$" matches to the last comma in a string. Replacing matched part with the empty string, that's equivalent to removing the matched part.

 

By default, the StringReplacer does nothing if there were no matched parts in the source string.

 

 

Takashi

Forum|alt.badge.img
  • Author
  • June 28, 2015
Thanks for all the feedback. I got there in the end, after finally realising I had a character of whitespace after the comma breaking everything I'd tried!

 

 

For information, AttributeTrimmer wasn't suitable because there were multiple commas in the string and I only wanted to lose the end one - but I didn't explain that in my question.