gpt4 book ai didi

javascript - Angular js - 使用正确的空格在模态中格式化字符串

转载 作者:数据小太阳 更新时间:2023-10-29 05:21:28 24 4
gpt4 key购买 nike

我有一个具有以下输出的弹出窗口!输出只是一个包含空格和换行符的完整字符串。但是每一行都连接到上一行。因此,每条线都可以单独调整。

Test1                        :  Success :    200
Test2 : Success : 200
Test3 : Success : 200
Test4 : Success : 200
Test5 : Success : 404
Test6 : Success : 401

因为我有多个这样的弹出窗口和每个弹出窗口的多个测试。有没有一种方法可以格式化字符串以具有适当的缩进?那就是我希望我的输出是:

Test1               :  Success :    200
Test2 : Success : 200
Test3 : Success : 200
Test4 : Success : 200
Test5 : Success : 404
Test6 : Success : 401

最佳答案

这是我会做的:

首先,用 \n 拆分字符串以获取数组中的每一行。接下来,使用 :trim 再次拆分以删除变量空间。

最后,再次加入它们,但第一个元素附加了额外的空间,这对它们中的每一个都是相同的。

let str = "Test1                        :  Success :    200\nTest2              :  Success :    200\nTest3                  :  Success :    200\nTest4                :  Success :    200\nTest5                  :  Success  :    404\nTest6           :  Success  :    401"


let arr = str.split("\n")

let res = arr.map(function(st) {
let temp = st.split(":")
return temp.map(s => s.trim())
})

let final = res.map(function(a) {
a[0] = a[0] + " "
return a.join(" : ")
})

console.log(final)

关于javascript - Angular js - 使用正确的空格在模态中格式化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43993530/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com