Split string on upper case characters

1,820 views
Skip to first unread message

Ollie Castle

unread,
Feb 17, 2013, 11:17:52 AM2/17/13
to golan...@googlegroups.com
Hi

I am trying to split a string based on upper case characters but with no luck. I am using

controllerName := "ApiHeartRateController"
splitPath := strings.FieldsFunc(controllerName, func(c rune) bool { return unicode.IsUpper(c) })

But the output from this drops the capital letter and I cant find any other method then unicode.IsUpper which will tell me if a letter is upper case.

Any ideas? Any help greatly appreciated?

Rémy Oudompheng

unread,
Feb 17, 2013, 11:24:01 AM2/17/13
to Ollie Castle, golan...@googlegroups.com
Write it by hand:

controllerName := "ApiHeartRateController"
var words []string
l := 0
for s := controllerName; s != ""; s = s[l:] {
l = strings.IndexFunc(s[1:], unicode.IsUpper) + 1
if l <= 0 {
l = len(s)
}
words = append(words, s[:l])
}

Rémy.

minux

unread,
Feb 17, 2013, 11:23:40 AM2/17/13
to Ollie Castle, golan...@googlegroups.com
using unicode.IsUpper is suitable, but strings.FieldsFunc is not.
because it will drop the field separator.

you need to use strings.IndexFunc to implement your own function for this.

Oliver Castle

unread,
Feb 17, 2013, 11:31:50 AM2/17/13
to Rémy Oudompheng, golan...@googlegroups.com
Perfect, Thanks

Just gave me the knock to work out what I was doing wrong. 
Reply all
Reply to author
Forward
0 new messages