3

In javascript how to break my string if it exceeds 25 characters into two lines , if my string contain 75 character i want to get the string to three lines of 25 character.

thanks in advance

asked Feb 2, 2011 at 6:19

4 Answers 4

7

That's really easy to accomplish with a regular expression:

var text = '75 characters long (really!) — well... maybe not, but you get the picture.',
 broken;
broken = text.replace(/([^0円]{25})/g, '1ドル\n');

As demonstrated here: http://jsbin.com/ajiyo/3.

Edit: To explain the regular expression: it will match any string of characters (the collection of every character except NUL), that is 25 characters long.

The parentheses () mean that this portion should be captured, and the '1ドル' part of the second argument (the replacement string) refers to that first capture.

Every string of 25 characters found will be replaced by 'itself plus a newline'. If the remainder is less than 25 characters, it will not be matched but left alone.

2nd edit: Brock is right, the dot loses its special meaning when in square brackets. I've replaced that by all non-NUL characters, since I don't expect NUL characters in a text string.

answered Feb 2, 2011 at 12:32
Sign up to request clarification or add additional context in comments.

3 Comments

@BrockAdams: Oops, you’re right. I forgot to test that last change. :-\ I’ve updated the answer.
Anyone looking for a solution that doesn't break words (aka wordwrap), please see this site: james.padolsey.com/javascript/wordwrap-for-javascript
@JLewkovich THANK YOU!
3

Try to use something like this

var point=0;
var myStr="12345678901234567890ABCDE my very long string 12345678901234567890ABCDE";
var myRes="";
while(myStr.substring(point).length>25)
{
 myRes=myRes+myStr.substring(point,point+25)+"\n"
 point+=25;
}
return myRes+myStr.substring(point);
answered Feb 2, 2011 at 6:27

1 Comment

Now that it's fixed, original comment deleted. +1
1

This should get you pretty close:

var txt = "This is a really long string that should be broken up onto lines of 25 characters, or less.";
for (i=0;i<(Math.ceil(txt.length/25));i++) {
 document.write(txt.substring(25*i,25*(i+1)) + "<br />");
}

See working example:

http://jsfiddle.net/dbgDj/

answered Feb 2, 2011 at 6:58

Comments

0

use str_split (php) equivalent in javascript

http://phpjs.org/functions/str_split:530

 function str_split (string, split_length) {
 // Convert a string to an array. If split_length is specified,
 // break the string down into chunks each split_length characters long. 
 // 
 // version: 1101.3117
 // discuss at: http://phpjs.org/functions/str_split 
 // + original by: Martijn Wieringa
 // + improved by: Brett Zamir (http://brett-zamir.me)
 // + bugfixed by: Onno Marsman
 // + revised by: Theriault
 // + input by: Bjorn Roesbeke (http://www.bjornroesbeke.be/) 
 // + revised by: Rafał Kukawski (http://blog.kukawski.pl/)
 // * example 1: str_split('Hello Friend', 3);
 // * returns 1: ['Hel', 'lo ', 'Fri', 'end']
 if (split_length === null) {
 split_length = 1; }
 if (string === null || split_length < 1) {
 return false;
 }
 string += '';
 var chunks = [], pos = 0, len = string.length;
 while (pos < len) {
 chunks.push(string.slice(pos, pos += split_length));
 }
 return chunks;
}
answered Feb 2, 2011 at 6:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.