javascript - Break the string into two lines - Stack Overflow

I want to break a string into two lines, but the line should not break into two, at the half of a word.

I want to break a string into two lines, but the line should not break into two, at the half of a word. How can I do this?

The string format is like this:

var words="value.eight.seven.six.five.four.three"

Expected output is:

"value.eight.seven.

six.five.four.three"

I want to break a string into two lines, but the line should not break into two, at the half of a word. How can I do this?

The string format is like this:

var words="value.eight.seven.six.five.four.three"

Expected output is:

"value.eight.seven.

six.five.four.three"
Share Improve this question edited Jun 26, 2014 at 5:19 Chankey Pathak 21.7k12 gold badges88 silver badges136 bronze badges asked Jun 26, 2014 at 5:11 KrishnaKrishna 1,3625 gold badges20 silver badges36 bronze badges 10
  • 1 what is your expected O/P..? What do you mean by two lines..? want to store the split values into two variables.. – Rajaprabhu Aravindasamy Commented Jun 26, 2014 at 5:12
  • you want to break string but still don't want to break in two. It is little confusing. Is it that you want to break it in two halfs but words should not break? – Bhushan Kawadkar Commented Jun 26, 2014 at 5:13
  • you have to be more specific. what is the point at which you want to break the string?? – Aradhna Commented Jun 26, 2014 at 5:15
  • Check this out: stackoverflow./questions/14484787/wrap-text-in-javascript It is similar to your issue, just need to change the identifier to break the line accordingly. – ak0053792 Commented Jun 26, 2014 at 5:15
  • @ Bhushan Kawadkar:i mean the line should not break at middle of "seven", or "six", means it should break after the third word or the fourth word – Krishna Commented Jun 26, 2014 at 5:16
 |  Show 5 more ments

4 Answers 4

Reset to default 3

Try,

var words = "value.eight.seven.six.five.four.three";
var splitted = words.split('.');
var index = splitted.length / 2;  
var val1 = splitted.slice(0, index).join('.') + ".";
var val2 = splitted.slice(index, splitted.length).join('.');

DEMO

Try this:

    var words="value.eight.seven.six.five.four.three"
    var wordsArr = words.split(".");
    var line1 = wordsArr.slice(0,Math.floor(wordsArr.length/2)).join("."); 
    var line2 = wordsArr.slice(Math.floor(wordsArr.length/2)).join(".");

Here is the working fiddle:

http://jsfiddle/6xgS2/

you do the following-->

var words = "value.eight.seven.six.five.four.three";
var all = words.split(".");
var new="";  //empty string

for(var i=0;i<all.length;i++){
new=new+all[i]+".";  //add each word and the .
if(i==3)
new=new+"\n"; //add a \n after the third word
}

the new var will have your new string. hope this helps.

try this

var words = "value.eight.seven.six.five.four.three";

var all = words.split(".");
var half=all.length/2;
var first = all.slice(0, half).join();
var second = all.slice(half, all.length).join();

DEMO

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745107639a4611655.html

相关推荐

  • javascript - Break the string into two lines - Stack Overflow

    I want to break a string into two lines, but the line should not break into two, at the half of a word.

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信