How to Replace Special Characters in a String

Replace special characters in a string in Python

str.replace is the wrong function for what you want to do (apart from it being used incorrectly). You want to replace any character of a set with a space, not the whole set with a single space (the latter is what replace does). You can use translate like this:

removeSpecialChars = z.translate ({ord(c): " " for c in "!@#$%^&*()[]{};:,./<>?\|`~-=_+"})

This creates a mapping which maps every character in your list of special characters to a space, then calls translate() on the string, replacing every single character in the set of special characters with a space.

Replacing special characters in Java String

Don't use square brackets, as it represents a set of single characters to match (a character class).

a=a.replaceAll("’|‵", "-");

Demo!

How to replace all special characters in string

You can create a User-Defined-Function for something like that.

Then use the UDF in the update.

CREATE FUNCTION [dbo].LowerDashString (@str varchar(255))
RETURNS varchar(255)
AS
BEGIN
DECLARE @result varchar(255);
DECLARE @chr varchar(1);
DECLARE @pos int;
SET @result = '';
SET @pos = 1;

-- lowercase the input and remove the single-quotes
SET @str = REPLACE(LOWER(@str),'''','');

-- loop through the characters
-- while replacing anything that's not a letter to a dash
WHILE @pos <= LEN(@str)
BEGIN

SET @chr = SUBSTRING(@str, @pos, 1)

IF @chr LIKE '[a-z]' SET @result += @chr;
ELSE SET @result += '-';

SET @pos += 1;
END;

-- SET @result = TRIM('-' FROM @result); -- SqlServer 2017 and beyond

-- multiple dashes to one dash
WHILE @result LIKE '%--%' SET @result = REPLACE(@result,'--','-');

RETURN @result;
END;
GO

Example snippet using the function:

-- using a table variable for demonstration purposes
declare @SomeInfo table (Id int primary key identity(1,1) not null, InfoCode varchar(100) not null);

-- sample data
insert into @SomeInfo (InfoCode) values
('Cathe Friedrich''s Low Impact'),
('coffeyfit-cardio-box-&-burn'),
('Jillian Michaels: Cardio'),
('Sleek Technique™'),
('The Dancer''s-workout®');

update @SomeInfo
set InfoCode = dbo.LowerDashString(InfoCode)
where (InfoCode LIKE '%[^A-Z-]%' OR InfoCode != LOWER(InfoCode));

select *
from @SomeInfo;

Result:

Id  InfoCode
-- -----------------------------
1 cathe-friedrichs-low-impact
2 coffeyfit-cardio-box-burn
3 jillian-michaels-cardio
4 sleek-technique-
5 the-dancers-workout-

How to replace special characters and numbers with the character and spaces on each side

You can use

input.replaceAll("(?<=[^\\s/])(?=[^\\s/])", " ")

See the regex demo. Details:

  • (?<=[^\s/]) - a positive lookbehind that matches a location that is immediately preceded with a char other than a whitespace char and a slash
  • (?=[^\s/]) - a positive lookahead that matches a location that is immediately followed with a char other than a whitespace char and a slash.

Replace the string of special characters in C#

I believe, best is to use a regular expression here as below

s/[*'",_&#^@]/ /g

You can use Regex class for this purpose

Regex reg = new Regex("[*'\",_&#^@]");
str1 = reg.Replace(str1, string.Empty);

Regex reg1 = new Regex("[ ]");
str1 = reg.Replace(str1, "-");

How to replace special character in an array with the next non-special character?

You need two indices: one for the array you're reading from and the other for the array you're writing to. If you use the same index for both, you'll end up with unset slots in your output array. Presumably these happen to be 0 in this case, prematurely terminating the output string.



Related Topics



Leave a reply



Submit