在大写字母之间添加一个空格

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

Adding a space between uppsercase letters

问题

我有一个字符串,像这样ClientLovesProcess,我需要在每个大写字母之间添加一个空格,除了第一个大写字母之外,所以最终结果应该是Client Loves Process

我认为golang的字符串支持并不是最好的,但这是我考虑的方法:

首先循环遍历每个字母,像这样:

  1. name := "ClientLovesProcess"
  2. wordLength := len(name)
  3. for i := 0; i < wordLength; i++ {
  4. letter := string([]rune(name)[i])
  5. // 然后在这里我想检查
  6. // 字母是大写还是小写
  7. if letter == uppercase{
  8. // 然后断开字符串并添加一个空格
  9. }
  10. }

问题是我不知道如何在go中检查一个字母是小写还是大写。我查了一下字符串的手册,但它们似乎没有相应的函数。有没有其他方法可以用go完成这个任务?

英文:

I have string like this ClientLovesProcess I need to add a space between each uppercase letter except for the first uppercase letter so the end result would be this Client Loves Process

I don't think golang has the best string support but this is how I was thinking about going about it:

First loop through each letter so something like this:

  1. name := &quot;ClientLovesProcess&quot;
  2. wordLength := len(name)
  3. for i := 0; i &lt; wordLength; i++ {
  4. letter := string([]rune(name)[i])
  5. // then in here I would like to check
  6. // if the letter is upper or lowercase
  7. if letter == uppercase{
  8. // then break the string and add a space
  9. }
  10. }

The issue is I don't know how to check if a letter is lower or uppercase in go. I checked the strings manual but they don't some to have a function for it. What would be another approach to get this done with go?

答案1

得分: 7

你要找的函数是unicode.IsUpper(r rune) bool

我建议使用bytes.Buffer,这样你就不需要进行大量的字符串拼接,从而避免额外的不必要的内存分配。

以下是一个实现示例:

  1. func addSpace(s string) string {
  2. buf := &bytes.Buffer{}
  3. for i, rune := range s {
  4. if unicode.IsUpper(rune) && i > 0 {
  5. buf.WriteRune(' ')
  6. }
  7. buf.WriteRune(rune)
  8. }
  9. return buf.String()
  10. }

这里是一个play链接

英文:

The function you're looking for is unicode.IsUpper(r rune) bool.

I would use a bytes.Buffer so that you're not doing a bunch of string concatenations, which results in extra unnecessary allocations.

Here's an implementation:

  1. func addSpace(s string) string {
  2. buf := &amp;bytes.Buffer{}
  3. for i, rune := range s {
  4. if unicode.IsUpper(rune) &amp;&amp; i &gt; 0 {
  5. buf.WriteRune(&#39; &#39;)
  6. }
  7. buf.WriteRune(rune)
  8. }
  9. return buf.String()
  10. }

And a play link.

答案2

得分: 0

你可以使用unicode包来测试大写字母。这是我的解决方案:

playground

  1. package main
  2. import (
  3. "fmt"
  4. "strings"
  5. "unicode"
  6. )
  7. func main() {
  8. name := "ClientLovesProcess"
  9. newName := ""
  10. for _, c := range name {
  11. if unicode.IsUpper(c){
  12. newName += " "
  13. }
  14. newName += string(c)
  15. }
  16. newName = strings.TrimSpace(newName) // 去除两端的空格。
  17. fmt.Println(newName)
  18. }

希望对你有帮助!

英文:

You can test for upper case with the unicode package. This is my solution:

playground

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;strings&quot;
  5. &quot;unicode&quot;
  6. )
  7. func main() {
  8. name := &quot;ClientLovesProcess&quot;
  9. newName := &quot;&quot;
  10. for _, c := range name {
  11. if unicode.IsUpper(c){
  12. newName += &quot; &quot;
  13. }
  14. newName += string(c)
  15. }
  16. newName = strings.TrimSpace(newName) // get rid of space on edges.
  17. fmt.Println(newName)
  18. }

huangapple
  • 本文由 发表于 2015年10月22日 00:10:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/33264231.html
匿名

发表评论

匿名网友

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

确定