Skip to content

Commit efbef1e

Browse files
Max Bileschiadriaanm
authored andcommitted
Update stripPrefix/StringLike docs to talk about no op case
Incorporate review comments by Som Snytt.
1 parent d028d89 commit efbef1e

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/library/scala/collection/immutable/StringLike.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,23 +135,29 @@ self =>
135135
def linesIterator: Iterator[String] =
136136
linesWithSeparators map (line => new WrappedString(line).stripLineEnd)
137137

138-
/** Returns this string with first character converted to upper case */
138+
/** Returns this string with first character converted to upper case.
139+
* If the first character of the string is capitalized, it is returned unchanged.
140+
*/
139141
def capitalize: String =
140142
if (toString == null) null
141143
else if (toString.length == 0) ""
144+
else if (toString.charAt(0).isUpper) toString
142145
else {
143146
val chars = toString.toCharArray
144147
chars(0) = chars(0).toUpper
145148
new String(chars)
146149
}
147150

148-
/** Returns this string with the given `prefix` stripped. */
151+
/** Returns this string with the given `prefix` stripped. If this string does not
152+
* start with `prefix`, it is returned unchanged.
153+
*/
149154
def stripPrefix(prefix: String) =
150155
if (toString.startsWith(prefix)) toString.substring(prefix.length)
151156
else toString
152157

153158
/** Returns this string with the given `suffix` stripped. If this string does not
154-
* end with `suffix`, it is returned unchanged. */
159+
* end with `suffix`, it is returned unchanged.
160+
*/
155161
def stripSuffix(suffix: String) =
156162
if (toString.endsWith(suffix)) toString.substring(0, toString.length() - suffix.length)
157163
else toString

0 commit comments

Comments
 (0)