问题
I have 2 very long strings which span multiple lines each and want to print to console both strings in their own "column" to the terminal
i.e.
let veryLongStringWithNewLines = "a very long string with \n
and blah blah \n
and more text ....."
let anotherVeryLongStringWithNewLines = "......."
print ("\(veryLongStringWithNewLines) %???Break Screen in Center???% \(anotherVeryLongStringWithNewLines)") // <<<< TO THE CONSOLE
I want to divide the console into 2 columns such that each string is printed in its own area , but side by side , i.e. EXCEL with 2 columns, each string in its own column
The string should wrap inside of that column to new lines when necessary
Not sure how to achieve this
回答1:
Here's something to get you started:
// Break a string up into an array of Strings, first on newlines
// and then by width if the lines are longer than width
func breakIntoLines(text: String, width: Int) -> [String] {
var result = [String]()
for line in text.split(separator: "\n") {
var str = String(line)
while str.count > width {
result.append(String(str.prefix(width)))
str = String(str.dropFirst(width))
}
// pad last line to width to make displaying easier
result.append(str + String(repeating: " ", count: width - str.count))
}
return result
}
// print two Strings in two columns optionally keeping the rows
// in sync
func printIn2Columns(text1: String, text2: String, columnWidth: Int, space: Int, keepRowsInSync: Bool = false) {
if keepRowsInSync {
// split the lines into rows on newline
var rows1 = text1.split(separator: "\n")
var rows2 = text2.split(separator: "\n")
// pad the shorter number of rows
if rows1.count > rows2.count {
rows2 += Array(repeating: "", count: rows1.count - rows2.count)
} else if rows2.count > rows1.count {
rows1 += Array(repeating: "", count: rows2.count - rows1.count)
}
// print each row in two columns
for (row1, row2) in zip(rows1, rows2) {
printIn2Columns(text1: String(row1), text2: String(row2), columnWidth: columnWidth, space: space)
}
} else {
var column1 = breakIntoLines(text: text1, width: columnWidth)
var column2 = breakIntoLines(text: text2, width: columnWidth)
// pad the shorter column with extra rows
let blankLine = String(repeating: " ", count: columnWidth)
if column1.count > column2.count {
column2 += Array(repeating: blankLine, count: column1.count - column2.count)
} else if column2.count > column1.count {
column1 += Array(repeating: blankLine, count: column2.count - column1.count)
}
let spacing = String(repeating: " ", count: space)
for (line1, line2) in zip(column1, column2) {
print("\(line1)\(spacing)\(line2)")
}
}
}
Tests:
let text1 = """
This is a test
of the code which
breaks this up into
columns
"""
let text2 = """
Well, here goes nothing!
Does this do what you want?
"""
printIn2Columns(text1: text1, text2: text2, columnWidth: 8, space: 5)
This is Well, he a test re goes of the c nothing! ode whic Does thi h s do wha breaks t t you wa his up i nt? nto columns
printIn2Columns(text1: text2, text2: text1, columnWidth: 10, space: 4)
Well, here This is a goes noth test ing! of the cod Does this e which do what yo breaks thi u want? s up into columns
printIn2Columns(text1: text2, text2: text1, columnWidth: 30, space: 5)
Well, here goes nothing! This is a test Does this do what you want? of the code which breaks this up into columns
Keeping the rows in sync
If you are comparing two texts, it might be desirable to keep the rows in sync. If you have to wrap a row in one column but not the other, add blank lines to the shorter to keep the rows of the original texts in sync.
let t1 = """
1:
2:
3: this is a really long line 3
4:
5: this is a really long line 5
6:
"""
let t2 = """
one - this is a really long line one
two
three - this is a really long line three
four
five
six
"""
printIn2Columns(text1: t1, text2: t2, columnWidth: 16, space: 5, keepRowsInSync: true)
1: one - this is a really long line one 2: two 3: this is a rea three - this is lly long line 3 a really long li ne three 4: four 5: this is a rea five lly long line 5 6: six
来源:https://stackoverflow.com/questions/57500721/swift-how-to-print-to-console-multi-line-strings-in-2-distinct-columns-on-the