将字符串中的单词首字母大写。

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

Make first letter of words uppercase in a string

问题

我有一个包含大量字符串的数组,例如:

"INTEGRATED ENGINEERING 5 Year (BSC with a Year in Industry)"

我想将每个单词的首字母大写,其余字母小写。所以INTEGRATED会变成Integrated

然而,还有一个问题 - 我想对一些单词进行例外处理,例如andinawith

所以上面的例子会变成:

"Integrated Engineering 5 Year (Bsc with a Year in Industry)"

在Go语言中,我该如何实现这个功能?我可以编写循环/数组来管理这个变化,但实际的字符串转换是我遇到困难的地方。

英文:

I have a large array of strings such as this one:

"INTEGRATED ENGINEERING 5 Year (BSC with a Year in Industry)"

I want to capitalise the first letter of the words and make the rest of the words lowercase. So INTEGRATED would become Integrated.

A second spanner in the works - I want an exception to a few words such as and, in, a, with.

So the above example would become:

"Integrated Engineering 5 Year (Bsc with a Year in Industry)"

How would I do this in Go? I can code the loop/arrays to manage the change but the actual string conversion is what I struggle with.

答案1

得分: 132

在内置的strings包中有一个名为Title的函数。

s := "INTEGRATED ENGINEERING 5 Year (BSC with a Year in Industry)"
fmt.Println(strings.Title(strings.ToLower(s)))

https://go.dev/play/p/THsIzD3ZCF9

英文:

There is a function in the built-in strings package called Title.

s := "INTEGRATED ENGINEERING 5 Year (BSC with a Year in Industry)"
fmt.Println(strings.Title(strings.ToLower(s)))

https://go.dev/play/p/THsIzD3ZCF9

答案2

得分: 16

你可以使用正则表达式来完成这个任务。\w+的正则表达式将匹配所有的单词,然后通过使用Regexp.ReplaceAllStringFunc函数,你可以将单词替换为预期的内容,跳过停用词。在你的情况下,strings.ToLowerstrings.Title也会很有帮助。

示例:

str := "INTEGRATED ENGINEERING 5 Year (BSC with a Year in Industry)"

// 替换单词的函数(假设输入为小写)
replace := func(word string) string {
	switch word {
	case "with", "in", "a":
		return word
	}
	return strings.Title(word)
}

r := regexp.MustCompile(`\w+`)
str = r.ReplaceAllStringFunc(strings.ToLower(str), replace)

fmt.Println(str)

// 输出:
// Integrated Engineering 5 Year (Bsc with a Year in Industry)

https://play.golang.org/p/uMag7buHG8

你可以很容易地将这个方法适应到你的字符串数组中。

英文:

You can use regular expressions for this task. A \w+ regexp will match all the words, then by using Regexp.ReplaceAllStringFunc you can replace the words with intended content, skipping stop words. In your case, strings.ToLower and strings.Title will be also helpful.

Example:

str := "INTEGRATED ENGINEERING 5 Year (BSC with a Year in Industry)"

// Function replacing words (assuming lower case input)
replace := func(word string) string {
	switch word {
	case "with", "in", "a":
		return word
	}
	return strings.Title(word)
}

r := regexp.MustCompile(`\w+`)
str = r.ReplaceAllStringFunc(strings.ToLower(str), replace)

fmt.Println(str)

// Output:
// Integrated Engineering 5 Year (Bsc with a Year in Industry)

https://play.golang.org/p/uMag7buHG8

You can easily adapt this to your array of strings.

答案3

得分: 15

下面是对已接受答案的替代方案的翻译,该答案现在已被弃用:

package main

import (
	"fmt"
	"golang.org/x/text/cases"
	"golang.org/x/text/language"
)

func main() {
	msg := "INTEGRATED ENGINEERING 5 Year (BSC with a Year in Industry)"
	fmt.Println(cases.Title(language.English, cases.Compact).String(msg))
}
英文:

The below is an alternate to the accepted answer, which is now deprecated:

package main

import (
	"fmt"
	"golang.org/x/text/cases"
	"golang.org/x/text/language"
)

func main() {
	msg := "INTEGRATED ENGINEERING 5 Year (BSC with a Year in Industry)"
	fmt.Println(cases.Title(language.English, cases.Compact).String(msg))
}

答案4

得分: 0

在Go 1.18中,strings.Title()已被弃用。

在这里你可以阅读以下内容,了解现在应该使用什么。

> 现在应该使用cases.Title。

英文:

In Go 1.18 strings.Title() is deprecated.

Here you can read the following to know what to use now

> you should use cases.Title instead.

答案5

得分: -28

你没有指定你使用的语言,所以我会给你一个通用的答案。你有一个包含许多字符串的数组。首先,我会将整个字符串转换为小写,然后遍历字符串中的每个字符(将第一个字符大写,其余字符保持小写)。在这一点上,你需要寻找空格,这将帮助你分割每个字符串中的单词。找到空格后的第一个字符显然是一个不同的单词,应该大写。你还可以验证下一个单词不是"and"、"in"或"with"等。

我现在没有电脑,所以无法给你一个具体的示例,但我希望这能帮助你朝着正确的方向前进。

英文:

Well you didn't specify the language you're using, so I'll give you a general answer. You have an array with a bunch of strings in it. First I'd make the entire string lower case, then just go through each character in the string (capitalize the first one, rest stay lower case). At this point you need to look for the space, this will help you divide up the words in each string. The first character after finding a space is obviously a different word and should be capitalized. You can verify the next word isn't and in with Or a as well.

I'm not at a computer so I can't give to a specific example, but I hope this gets to in the right direction at least

huangapple
  • 本文由 发表于 2015年11月13日 23:22:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/33696034.html
匿名

发表评论

匿名网友

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

确定