将字符串拆分为偶数,形成正方形

时间:2018-05-04 20:21:35

标签: javascript

我的目标是均匀地分割字符串(少于80个字符)以创建字符串的正方形或矩形。

public class EntityName {
    @Id
    private String id;
    private Map<LocalDateTime, Integer> statistic;
}

应输出:

var squareStr = function(str) {

}

console.log(squareStr('whatwonderfulday'));
console.log(squareStr('if life was easy god then god would not have given us brain to think'));
这可能吗?我被告知我可以使用Math.sqrt,但我不太清楚如何。

感谢。

4 个答案:

答案 0 :(得分:3)

您可以使用for循环将字符串切成碎片,并在每个块的末尾添加一个新行(function squareCode(string){ let squareString = ""; string = string.replace(/\s/g, ''); const splitNum = Math.floor(Math.sqrt(string.length)); for(i=0; i<= string.length; i+=splitNum){ squareString = `${squareString}${string.slice(i, i+splitNum)}\n`; } return squareString; } console.log(squareCode('whatwonderfulday')); console.log(squareCode('if life was easy god then god would not have given us brain to think')); console.log(squareCode('asdfasdf asdfasdfasd fasdfwe wer df gf dgdfgertqewdfsf fgdgewfwdsgewerfsd fdgdfgqefasdf'));)。

如果你想自动使用字符串长度的平方根,你可以这样做:

function squareCode(string, splitNum){
  let squareString = "";
  string = string.replace(/\s/g, '');
  for(i=0; i<= string.length; i+=splitNum){
    squareString = `${squareString}${string.slice(i, i+splitNum)}\n`;
  }
  return squareString;
}

console.log(squareCode('whatwonderfulday', 4));
console.log(squareCode('if life was easy god then god would not have given us brain to think', 8));

在以下函数中,您将传入要剪切的字符串以及要剪切的数字:

EMPLOYEE_ID

答案 1 :(得分:2)

您可以使用此功能。它替换所有空的空格,然后将字符串转换为数组并将其分块。最后,如果合并每个块并将\n应用于每个块。

var squareStr = function(str, chunk) {
  str = str.replace(/ /g, '')
  str = str.split('');
  temp = []
  for (i=0; i<str.length; i+=chunk)
    temp.push(str.slice(i,i+chunk));
  return temp.map(function(a){return a.join('')+"\n"}).join('')
}

console.log(squareStr('whatwonderfulday', 4));
console.log(squareStr('if life was easy god then god would not have given us brain to think', 8));

答案 2 :(得分:1)

这么多方法......

这里的所有其他答案也是正确的,这是我的方法,一个更“可读”的答案,使用非常基本的recurses ......

你应该至少试过......

我还检查了字符串长度是否低于80。

var squareStr = function(str, charsPerLine) {
  if (str.length > 80){
    return;
  }
  str = str.replace(/ /g,'')
  var stringSplited = str.split('');  
  var newString = '';
  
  stringSplited.forEach(function(letter,index){
    if (index % charsPerLine == 0 && newString.length > 0){
      newString += '\n'; //IF YOU WANT TO USE IT IN THE HTML, USE '<br>' HERE
    }
    newString += letter;
  }); 
  console.log(newString);
  return newString;
}


squareStr('whatwonderfulday', 4);
squareStr('if life was easy god then god would not have given us brain to think', 8);

答案 3 :(得分:0)

除非您正在处理非常长的字符串,否则我没有理由不使用replace为每个 n 字符插入换行符:

function squareText(input) {
  const inputNoSpaces = input.replace(/\s/g, '');
  const partLen = Math.ceil(Math.sqrt(inputNoSpaces.length));
  const replaceExpr = new RegExp(`.{1,${partLen}}`, 'g');
  return inputNoSpaces.replace(replaceExpr, '$&\n');
}

const input = 'if life was easy then god would not have given us brain to think';
console.log(squareText(input));

这只是计算行长度,然后创建一个匹配那么多字符的新RegExp,并使用它来替换每个匹配项本身加上换行符。