用值替换breakline

时间:2018-05-03 10:01:19

标签: javascript typescript

let someText = "Hi THIS IS MY TEXT AND I WANT IT CENTRED NOT ON THE TOP OF THE TEXTAREA"; //This would be replaced with your textarea value
let stringSplit = someText.split("\n");

if (stringSplit.length < 8) {
  //Add enough items to be 8 rows
  for (let x = stringSplit.length; x < 8; x++) {
    stringSplit.push("");
  }
}

//replace the 8th row value with your name
stringSplit[7] = "YourName";

//Join the array back into a string separated by newline between each element value
someText = stringSplit.join("\n");
//Set your textarea value = someText
console.log(someText);

使用此代码,我可以在第8行添加值,但问题是我的文本将是这样的:

    EXEMPLE OF TEXT






       VALUE(8throw)

而不是

            /n
            /n
            /n
        MY TEXT


       Value(8th row)

我想让我的文本居中,并且值要在第8行,而不是让我的文本放在textarea的顶部,而是我的值在底部。 我想用我的文字替换3个细分线来制作这样的东西。

\n \n \n Mytext \n \n \n Value

我想要实现的是this

1 个答案:

答案 0 :(得分:1)

交替添加到数组的开头和结尾:

let someText = "Example text";
let stringSplit = someText.split("\n");
for (let x = stringSplit.length; x < 8; x++) {
  if (x % 2 == 0) {
    stringSplit.unshift("");
  } else {
    stringSplit.push("");
  }
}
stringSplit[7] = "Last line";
console.log(stringSplit.join("\n"));

但是,是的,如果你只是为视觉效果而这样做,你应该研究适当的CSS格式。