實作練習-txt文本批次取代器
尚硅谷Golang課

實作練習-txt文本批次取代器

自己寫用來一次取代多個名詞,去除多餘空格或符號等等

package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strings"
)

var data []string = make([]string, 128)

func main() {
	readByBufio()
	for _, v := range data {
		if v == "" {
			break
		}
		fmt.Print(v)
	}

}
func readByBufio() {
	file, err := os.Open("C:/test.txt")
	if err != nil {
		fmt.Printf("os.Open err %s\n", err)
		return
	}
	defer file.Close()
	reader := bufio.NewReader(file)
	for i := 0; ; i++ {
		line, err := reader.ReadString('\n')
		if err == io.EOF {
			return
		}
		if err != nil {
			fmt.Printf("reader.ReadString err %s\n", err)
			return
		}
		line = strings.ReplaceAll(line, "++", "+") //去除多餘++
		line = strings.ReplaceAll(line, "  ", " ") //去除多餘空格
		line = strings.ReplaceAll(line, "*", "")   //去除多餘*
		line = strings.ReplaceAll(line, "AAAA", "BBBB")
		line = strings.ReplaceAll(line, "", "")
		data[i] = line
	}

}

上次修改於 2021-08-01

此篇文章的評論功能已經停用。