/    Sign up×
Community /Pin to ProfileBookmark

Trim string length if above certain length

Hi Guys

I have a string. If it is longer than 25 chars I want to delete the excess chars. How can I do this?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@ShaolinauthorSep 27.2008 — I have written some code but it doesnt work when I try to empty the charAt value.

[CODE]var all = "1234567890123456789012345678901234567890";
for(var k=0;k<all.length;k++) {
if(k>25) {
all.charAt(k) = ""; //This doesnt work
}
}[/CODE]
Copy linkTweet thisAlerts:
@Declan1991Sep 27.2008 — <i>
</i>str = str.substring(0,25);


EDIT: Or in your stylevar all = "1234567890123456789012345678901234567890";
all = all.substring(0,25);

As a general rule, try to keep things simple. JavaScript can work out whether the string is too long, you don't have to worry about it.
Copy linkTweet thisAlerts:
@ShaolinauthorSep 27.2008 — Thanks. That was really simple in comparsion to what I was doing! I have another problem, at the end of some of my words I have a comma. How do I check for and delete it? Note that there will be commas in other parts of the sentence but the one I want to delete is the one that comes at the end of the word.
Copy linkTweet thisAlerts:
@Declan1991Sep 27.2008 — I'm not sure exactly what you mean. "hello, hello, hello," should one or three commas be deleted. I'll presume it's three, and this is what you'd usevar all = "1234567890123456789012345678901234567890";
all = all.substring(0,25).replace(/(w),/g,"$1");
Copy linkTweet thisAlerts:
@DokSep 28.2008 — Wouldn't
[CODE]all = all.substring(0,25).replace(/(w),/g,"$1");[/CODE]
be the same as
[CODE]all = all.substring(0,25).replace(/,/g, '');[/CODE]
?

(BTW: Comma is not a special char so it shouldn't be escaped)
Copy linkTweet thisAlerts:
@Declan1991Sep 28.2008 — Comma doesn't have to be escaped, but I prefer to escape all punctuation, it still works.

replace(/(w),/g,"$1");

replaces a comma if it's at the end of a word, while the other replaces all commas regardless of where they are. I'm not a hundred percent sure what the person wants, but looking at it again, this is probably what they want.all = all.replace(/(w),/g,"$1").substring(0,25);
×

Success!

Help @Shaolin 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.18,
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,
)...