Case insensitive HasSuffix in Go

huangapple go评论79阅读模式
英文:

Case insensitive HasSuffix in Go

问题

我正在寻找目录列表中的特定文件类型,并使用HasSuffix进行比较以查找几种特定的文件类型。我希望进行这种比较时不区分大小写。

有没有办法在HasSuffix函数中添加EqualFold或其他不区分大小写的比较方式?

英文:

I am looking for specific file types from a directory listing and use HasSuffix to do the comparision looking for a few specific file types. I would like to make this comparison case insensitive.

Is there a way to add EqualFold or another case insensitive comparison to the HasSuffix function?

答案1

得分: 12

你可以直接使用以下代码:

if strings.HasSuffix(strings.ToLower(s), "suffix") {
    // 做一些操作
}

你也可以编写自己的包装函数:

func hasSuffix(s, suffix string, caseSensitive bool) bool {
    if caseSensitive {
        return strings.HasSuffix(s, suffix)
    }
    return strings.HasSuffix(strings.ToLower(s), suffix)
}

对于文件名或路径,你可以使用以下代码(参考PeterSO的回答):

if strings.ToLower(filepath.Ext(s)) == ".fileending" {
    // 做一些操作
}
英文:

You could just use

if strings.HasSuffix(strings.ToLower(s), "suffix") {
	// do something	
}

You can also write your own wrapper function:

func hasSuffix(s, suffix string, caseSensitive bool) bool {
	if caseSensitive {
		return strings.HasSuffix(s, suffix)
	}
	return strings.HasSuffix(strings.ToLower(s), suffix)
}

For file names or paths you can use (see answer of PeterSO):

if strings.ToLower(filepath.Ext(s)) == ".fileending" {
    // do something
}

答案2

得分: 2

我正在寻找目录列表中特定文件类型的文件。我希望进行的比较是不区分大小写的。

例如,

package main

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

func hasExts(path string, exts []string) bool {
	pathExt := strings.ToLower(filepath.Ext(path))
	for _, ext := range exts {
		if pathExt == strings.ToLower(ext) {
			return true
		}
	}
	return false
}

func main() {
	dir := `./`
	files, err := ioutil.ReadDir(dir)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	sourceExts := []string{".go", ".Py", ".c", ".cpp"}
	for _, fi := range files {
		if hasExts(fi.Name(), sourceExts) {
			fmt.Println(fi.Name())
		}
	}
}

输出结果:

t.c
t.go
t.py
t1.go
t1.py
t2.go

对于strings.HasSuffix的特殊形式,请从strings中复制代码。

func HasSuffix(s, suffix string) bool {
	return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
}

然后根据你的需求进行修改。例如,

func hasSuffix(s, suffix string) bool {
	return len(s) >= len(suffix) &&
		strings.ToLower(s[len(s)-len(suffix):]) == strings.ToLower(suffix)
}
英文:

> "I am looking for specific file types from a directory listing. I would like to make this comparison case insensitive."

For example,

package main

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

func hasExts(path string, exts []string) bool {
	pathExt := strings.ToLower(filepath.Ext(path))
	for _, ext := range exts {
		if pathExt == strings.ToLower(ext) {
			return true
		}
	}
	return false
}

func main() {
	dir := `./`
	files, err := ioutil.ReadDir(dir)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	sourceExts := []string{".go", ".Py", ".c", ".cpp"}
	for _, fi := range files {
		if hasExts(fi.Name(), sourceExts) {
			fmt.Println(fi.Name())
		}
	}
}

Output:

t.c
t.go
t.py
t1.go
t1.py
t2.go

For a special form of strings.HasSuffix start by copying the code from the strings package.

func HasSuffix(s, suffix string) bool {
  	return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
}

Then modify it to suit your purpose. For example,

func hasSuffix(s, suffix string) bool {
	return len(s) >= len(suffix) &&
		strings.ToLower(s[len(s)-len(suffix):]) == strings.ToLower(suffix)
}

huangapple
  • 本文由 发表于 2017年8月9日 17:37:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/45586944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定