Javascript - 如何编写递归函数来构建像wordwrap这样的字符串数组?

时间:2017-09-11 11:00:44

标签: javascript string recursion

我需要逐行构建一个字符串数组。它就像wordwrap。

我会输入这样的文字:

Imports System.Diagnostics
Imports System.Windows.Forms

Public Class Proc_Form

Private excel_Manage = New Excel_Management
Public Shared ok As Boolean = New Boolean

 If My.Settings.check_Directory = False Then
            If FolderBrowserDialog1.ShowDialog = DialogResult.OK Then
                My.Settings.path = FolderBrowserDialog1.SelectedPath
                My.Settings.check_Directory = True
                'My.Settings.Save()
                MsgBox(My.Settings.path)
                excel_Manage.check_Excel()
        End If

我需要数组中的每一行最多38个字符。我不想在中间分割任何单词,所以如果38个字符位于单词的中间,则返回最近的空格字符。

期望的输出:

Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Excel
Imports System.Windows.Forms
Imports System.Text.RegularExpressions
Imports System.String

Public Class Excel_Management
    Public Shared appExcel As Excel.Application = New Excel.Application
    Public Shared wsExcel As Excel.Worksheet = New Excel.Worksheet
    Public Shared wbExcel As Excel.Workbook = New Excel.Workbook
    Public Shared file_name As String = "Name" & DateTime.Now.ToString("yyyy") & ".xls"
    Public Shared sheet_name As String = "S " & DatePart(DateInterval.WeekOfYear, Now())
    Public Shared check_test As Boolean = False
    Public Shared excel_check As New Proc_Form
    Public Shared misValue As Object = System.Reflection.Missing.Value
    Public Shared message, title, defaultValue As String
    Public Shared myValue As Object = New Object
    Public Shared i As Integer = 1
    Public Shared match As Match = Regex.Match(myValue, "[^a-z0-9]")

Shared Sub check_Excel()
        If appExcel Is Nothing Then
            MsgBox("No Excel Detected !")
            Return
        Else
            create_Excel()
        End If
    End Sub

输出错误:

    var inputString = 'There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration';

我试图用空格字符拆分输入文本,最后得到:

    var output = [
        'There are many variations of passages',
        'of Lorem Ipsum available, but the',
        'majority have suffered alteration'
    ];

我不确定如何检查参数是否总计38或更多,然后再回溯它们。

3 个答案:

答案 0 :(得分:5)

你可以使用string.prototype.match()来做那个



    var inputString = 'There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration';


let result = inputString.match(/\b.{1,38}\b/g);

console.log(result);




由于正则表达式不重叠,您将获得所需的结果

答案 1 :(得分:0)

您可以在javascript中使用regex长度38并拆分字符串。



 var inputString = 'There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration';
var result = inputString.replace(/.{38}\S*\s+/g, "$&@").split(/\s+@/);

console.log(result);




答案 2 :(得分:0)

可以在不使用递归的情况下完成:

var inputString = 'There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration';
var splitted = inputString.split(" ");
var output = [""];
var i = 0;
splitted.forEach(word => {
   var line = output[i] + " " +  word; 
   if (line.length <= 38) {
       output[i] = line;
   } else {
       i++;
       output[i] = word;
   }
});
console.log("Output:", output);