/    Sign up×
Community /Pin to ProfileBookmark

Removing Specified character from a string

I’m trying to make function to remove a specific character from a string then
returning a new string without that character. for example I wanted to remove all comma in 1,000,000.00. But the returned array is 1,0,0,0,0,0,0,.,0,0. tried using join method but still have no effect.
any help thanks…

function myremovechar(chr,word){
var array1 = word;
var array2 = new Array();
var x,ctr = 0;
var len = array1.length;

while(ctr <= len-1){
if(array1.charAt(ctr)!=’,’){
x = array1.charAt(ctr);
array2.push(x);
}
ctr++;
}

return(array2);
}

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 07.2005 — Given the following:

str = "1,000,000.00";

One way to remove commas:

str = str.replace(/,/g, "");

another way:

str = str.split(",").join();
Copy linkTweet thisAlerts:
@setiealauthorJun 07.2005 — thanks
Copy linkTweet thisAlerts:
@phpnoviceJun 07.2005 — Cheers.
Copy linkTweet thisAlerts:
@ayooyaMar 09.2006 — Hi,

I need to know How can I excluding any characters of a string except a specified set of characters defined by Char[ ].



Example:

if I have

Char[ ] x = {..,..,..,.........};

string y;


and y is a mixture of characters in x and other characters not in x

so how can I return a string z that contains only the characters belong to x and excluding all others?

Thanks in advance for any help.

Aya.
Copy linkTweet thisAlerts:
@konithomimoMar 09.2006 — Hi,

I need to know How can I excluding any characters of a string except a specified set of characters defined by Char[ ].



Example:

if I have

Char[ ] x = {..,..,..,.........};

string y;


and y is a mixture of characters in x and other characters not in x

so how can I return a string z that contains only the characters belong to x and excluding all others?

Thanks in advance for any help.

Aya.[/QUOTE]

Just send the string to this function, and change the array of acceptable characters to fit your needs:
function removeChar(y)
{
var x = '';
var i, j;
var allowed = new Array('a','b','c');
for(i=0;i&lt;y.length;i++){
for(j=0;j&lt;allowed.length;j++){
if(y.charAt(i)== allowed[j]){
x+=y.charAt(i);
break;}}}
alert(x);
}
×

Success!

Help @setieal spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...