Иван обнови решението на 25.10.2015 03:56 (преди над 2 години)
+package main
+
+import (
+ "strings"
+)
+
+func ExtractColumn(logContents string, column uint8) string {
+ logContents = strings.TrimLeft(logContents, "\n")
+ logContents = strings.TrimRight(logContents, "\n")
+ logContents = strings.TrimLeft(logContents, " ")
+ logContents = strings.TrimRight(logContents, " ")
+ logContents = strings.TrimLeft(logContents, " ")
+ logContents = strings.TrimRight(logContents, " ")
+
+ if logContents == "" {
+ return ""
+ } else {
+ var rows []string = strings.Split(logContents, "\n")
+ return GetLogColumn(rows, column)
+ }
+}
+
+func GetLogColumn(rows []string, column uint8) string {
+ var result string
+
+ switch column {
+ case 0:
+ for i := 0; i < len(rows); i++ {
+ var currentRow string = rows[i]
+ var currentRowChunks []string = strings.Split(currentRow, " ")
+ var zeroColumn string = strings.Join(currentRowChunks[0:2], " ")
+
+ result = strings.TrimLeft(result+"\n"+zeroColumn, "\n")
+ }
+ case 1:
+ for i := 0; i < len(rows); i++ {
+ var currentRow string = rows[i]
+ var currentRowChunks []string = strings.Split(currentRow, " ")
+ var firstColumn string = strings.Join(currentRowChunks[2:3], " ")
+
+ result = strings.TrimLeft(result+"\n"+firstColumn, "\n")
+ }
+ case 2:
+ for i := 0; i < len(rows); i++ {
+ var currentRow string = rows[i]
+ var currentRowChunks []string = strings.Split(currentRow, " ")
+ var currentRowChunksLength = len(currentRowChunks)
+ var secondColumn string = strings.Join(currentRowChunks[3:currentRowChunksLength], " ")
+
+ result = strings.TrimLeft(result+"\n"+secondColumn, "\n")
+ }
+ }
+
+ return result + "\n"
+}
Добро утро , две неща :
- първите няколко реда могат да са един
strings.Trim
или дажеstrings.TripSpace
- много се повтаряш в GetLogColumn