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

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

Adding a space between uppsercase letters

问题

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

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

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

name := "ClientLovesProcess"

wordLength := len(name)

for i := 0; i < wordLength; i++ {
	letter := string([]rune(name)[i])

    // 然后在这里我想检查
   // 字母是大写还是小写

   if letter == uppercase{
       // 然后断开字符串并添加一个空格
   }
}

问题是我不知道如何在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:

name := &quot;ClientLovesProcess&quot;

wordLength := len(name)

for i := 0; i &lt; wordLength; i++ {
	letter := string([]rune(name)[i])

    // then in here I would like to check
   // if the letter is upper or lowercase

   if letter == uppercase{
       // then break the string and add a space
   }
}

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,这样你就不需要进行大量的字符串拼接,从而避免额外的不必要的内存分配。

以下是一个实现示例:

func addSpace(s string) string {
    buf := &bytes.Buffer{}
    for i, rune := range s {
        if unicode.IsUpper(rune) && i > 0 {
            buf.WriteRune(' ')
        }
        buf.WriteRune(rune)
    }
    return buf.String()
}

这里是一个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:

func addSpace(s string) string {
	buf := &amp;bytes.Buffer{}
	for i, rune := range s {
		if unicode.IsUpper(rune) &amp;&amp; i &gt; 0 {
			buf.WriteRune(&#39; &#39;)
		}
		buf.WriteRune(rune)
	}
	return buf.String()
}

And a play link.

答案2

得分: 0

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

playground

package main

import (
	"fmt"
	"strings"
	"unicode"
)

func main() {
	name := "ClientLovesProcess"
	newName := ""
	for _, c := range name {
	   
		if unicode.IsUpper(c){
		    newName += " "   
		}
		newName += string(c)
	}
	newName = strings.TrimSpace(newName) // 去除两端的空格。
	fmt.Println(newName)
}

希望对你有帮助!

英文:

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

playground

package main

import (
	&quot;fmt&quot;
    &quot;strings&quot;
	&quot;unicode&quot;
)

func main() {
	name := &quot;ClientLovesProcess&quot;
    newName := &quot;&quot;
	for _, c := range name {
	   
		if unicode.IsUpper(c){
		    newName += &quot; &quot;   
		}
		newName += string(c)
	}
	newName = strings.TrimSpace(newName) // get rid of space on edges.
	fmt.Println(newName)
}

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:

确定