从字符串中删除第一个和最后一个空行

时间:2021-07-29 17:59:37

标签: string go

我有以下文字:

str := `
 

Maybe we should all just listen to
records and quit our jobs

— gach White —

AZ QUOTES

 

 

 `

并想删除所有空行。 我能够删除段落中的空行:

str = strings.Replace(str, "\n\n", "\n", -1)
fmt.Println(str)

最后是:

 
Maybe we should all just listen to
records and quit our jobs
— gach White —
AZ QUOTES




所以,开头还有几行空行,结尾还有几行空行,我怎么才能把它们弄红?

在我的应用程序中,我试图从同一目录中的所有“png”文件中提取文本,并以漂亮的格式获取它,到目前为止我的完整代码是:

package main

import (
    "fmt"
    "io/ioutil"
    "os"
    "os/exec"
    "path/filepath"
    "strings"

    _ "image/png"
)

func main() {
    var files []string

    root := "."
    err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
        if filepath.Ext(path) == ".png" {
            path = strings.TrimSuffix(path, filepath.Ext(path))
            files = append(files, path)
        }
        return nil
    })
    if err != nil {
        panic(err)
    }
    for _, file := range files {
        fmt.Println(file)

        err = exec.Command(`tesseract`, file+".png", file).Run()
        if err != nil {
            fmt.Printf("Error: %s\n", err)
        } else {
            b, err := ioutil.ReadFile(file + ".txt") // just pass the file name
            if err != nil {
                fmt.Print(err)
            } else {
                str := string(b) // convert content to a 'string'
                str = strings.Replace(str, "\n\n", "\n", -1)
                fmt.Println(str) // print the content as a 'string'
            }
        }
    }

}

5 个答案:

答案 0 :(得分:2)

您可以使用 strings.TrimSpace 删除所有前导和尾随空格:

str = strings.TrimSpace(str)

答案 1 :(得分:2)

\n 分割字符串并删除分割元素中的空格,然后用 \n 连接它们

func trimEmptyNewLines(str string) string{
    strs := strings.Split(str, "\n")
    str = ""
    for _, s := range strs {
        if len(strings.TrimSpace(s)) == 0 {
            continue
        }
        str += s+"\n"
    }
    str = strings.TrimSuffix(str, "\n")

    return str
}

运行完整代码here

答案 2 :(得分:1)

我复制了您的字符串并将其转换为 JSON:

package main

import (
    "encoding/json"
    "log"
)

func main() {

    // The string from the original post.
    myString := `
 

Maybe we should all just listen to
records and quit our jobs

— gach White —

AZ QUOTES

 

 

 `

    // Marshal to json.
    data, err := json.Marshal(myString)
    if err != nil {
        log.Fatalf("Failed to marshal string to JSON.\nError: %s", err.Error())
    }

    // Print the string to stdout.
    println(string(data))
}

在 JSON 中查看空格可能会更容易。

"\n \n\nMaybe we should all just listen to\nrecords and quit our jobs\n\n— gach White —\n\nAZ QUOTES\n\n \n\n \n\n "

你看到这里的问题了吗?换行符之间有几个空格,此外,换行符的数量是奇数。因此,将 \n\n 替换为 \n 不会像您希望的那样运行。

我认为您的目标之一是:

<块引用>

并想删除所有空行。

(我不是在解决从 PNG 文件中提取文本的问题,因为这是一个单独的问题。)

package main

import (
    "encoding/json"
    "log"
    "strings"
)

func main() {

    // The string from the original post.
    myString := `
 

Maybe we should all just listen to
records and quit our jobs

— gach White —

AZ QUOTES

 

 

 `

    // Create a resulting string.
    result := ""

    // Iterate through the lines in this string.
    for _, line := range strings.Split(myString, "\n") {
        if line = strings.TrimSpace(line); line != "" {
            result += line + "\n"
        }
    }

    // Print the result to stdout.
    println(result)

    // Marshal the result to JSON.
    resultJSON, err := json.Marshal(result)
    if err != nil {
        log.Fatalf("Failed to marshal result to JSON.\nError: %s", err.Error())
    }

    println(string(resultJSON))
}

标准输出:

Maybe we should all just listen to
records and quit our jobs
— gach White —
AZ QUOTES

"Maybe we should all just listen to\nrecords and quit our jobs\n— gach White —\nAZ QUOTES\n"

答案 3 :(得分:1)

有点不同的答案。

package main

import (
    "fmt"
)

func main() {
    str := `
 

Maybe we should all just listen to
records and quit our jobs

— gach White —

AZ QUOTES

 

 

 `    
    first := 0
    last := 0

    for i, j := range []byte(str) {
        if j != 10 && j != 32 {
            if first == 0 {
                first = i
            }
            last = i
        }

    }
    str = str[first : last+1]
    fmt.Print(str)
}

答案 4 :(得分:0)

看起来中间有空格,例如

\n \n

因此,使用正则表达式 \n[ \t]*\n 执行 regexp replace 可能更明智。

虽然这不会删除开头的单个空行,为此您可以使用 ^\n* 并替换为空字符串。


再细化一下,你可以添加更多的空白,比如 \f 并一次考虑多个空行

\n([ \t\f]*\n)+
  • \n 换行
  • (...)+ 后跟一个或多个
  • [ \t\f]*\n 空行

这会清除中间的所有空行,但可能会在字符串的开头或结尾保留空白。正如其他答案中所建议的,添加 strings.TrimSpace() 可以解决这个问题。


把所有东西放在一起给出 https://play.golang.org/p/E07ZkE2nlcp

package main

import (
    "fmt"
    "regexp"
    "strings"
)

func main() {
    str := `
 

Maybe we should all just listen to
records and quit our jobs

— gach White —

AZ QUOTES

 

 

 `
    re := regexp.MustCompile(`\n([ \t\f]*\n)+`)
    str = string(re.ReplaceAll([]byte(str), []byte("\n")))
    str = strings.TrimSpace(str)
    fmt.Println("---")
    fmt.Println(str)
    fmt.Println("---")
}

最终显示

---
Maybe we should all just listen to
records and quit our jobs
— gach White —
AZ QUOTES
---
相关问题