regular expression replaceif the thereis a non alpha numeric charters at the end of the string

Posted on 2014-09-11
Medium Priority
Last Modified: 2014-09-15
I wnat to replace with empty string if there is an apha numeric character at the end of the string
Question by:Angela4eva

Author Comment

ID: 40317496
I want to replace that chacters only

then  return test1234
LVL 54

Expert Comment

ID: 40318837

pls try

"test1234*".Replace("\W$", "");

LVL 35

Accepted Solution

Terry Woods earned 2000 total points
ID: 40320397
@Rgonzo1971's solution allows underscore characters to remain at the end of the string. It also only removes the last non-alphanumeric character when there are more than one. To fix both those issues, use:
"test1234*".Replace("[^a-zA-Z0-9]+$", "");

Open in new window


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…
Suggested Courses

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question