Skip to content

Commit 5417b62

Browse files
CapitalizeOnlyFirstLetter
1 parent e391ab2 commit 5417b62

File tree

7 files changed

+65
-8
lines changed

7 files changed

+65
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ System.out.println( StringUtils.implode(words, " ") );
6060
| Property |Type|Description|Default|
6161
|----------------|-------------------------------|-----------------------------|--------|
6262
|implode |`(list: List<T>, delimiter: String): String`|Converts elements of a list into a string by separating each element with a delimiter| - |
63-
|capitalize |`(text: String): String`|Converts elements of a list into a string by separating each element with a delimiter| - |
63+
|capitalize |`(text: String): String`|Converts the first character of a string to an uppercase letter and all other alphabets to lowercase| - |
64+
|capitalizeOnlyFirstLetter |`(text: String): String`|Converts the first character of a string to an uppercase letter and concatenate it with remaining letters.| - |
6465

6566

6667
## 🚩 Changelog

dist/1.x/string-utils-1.1.1.jar

5.89 KB
Binary file not shown.

dist/1.x/string-utils-1.1.2.jar

5.76 KB
Binary file not shown.

pom-mavencentral.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.wniemiec-util-java</groupId>
88
<artifactId>string-utils</artifactId>
9-
<version>1.1.0</version>
9+
<version>1.1.1</version>
1010
<name>StringUtils</name>
1111
<description>Contains methods that perform string manipulation.</description>
1212
<url>https://github.com/wniemiec-util-java/string-utils</url>
@@ -101,7 +101,7 @@
101101
<artifactId>maven-jar-plugin</artifactId>
102102
<version>3.0.2</version>
103103
<configuration>
104-
<outputDirectory>${project.build.directory}/../dist/${version.major}.x</outputDirectory>
104+
<outputDirectory>${basedir}/dist/${version.major}.x</outputDirectory>
105105
</configuration>
106106
</plugin>
107107

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>wniemiec.util.java</groupId>
88
<artifactId>string-utils</artifactId>
9-
<version>1.1.0</version>
9+
<version>1.1.2</version>
1010
<name>StringUtils</name>
1111
<description>Contains methods that perform string manipulation.</description>
1212
<url>https://github.com/wniemiec-util-java/string-utils</url>

src/main/java/wniemiec/util/java/StringUtils.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,29 @@ private static void validateCapitalizeArg(String text) {
107107
throw new IllegalArgumentException("Text cannot be null");
108108
}
109109
}
110+
111+
/**
112+
* Converts the first character of a string to an uppercase letter and
113+
* concatenate it with remaining letters.
114+
*
115+
* @param text Text to be capitalized
116+
*
117+
* @return Capitalized text
118+
*/
119+
public static String capitalizeOnlyFirstLetter(String text) {
120+
validateCapitalizeArg(text);
121+
122+
if (text.isEmpty()) {
123+
return text;
124+
}
125+
126+
if (text.length() == 1) {
127+
return text.toUpperCase();
128+
}
129+
130+
String firstCharacter = text.substring(0, 1);
131+
String remainingText = text.substring(1);
132+
133+
return firstCharacter.toUpperCase() + remainingText;
134+
}
110135
}

src/test/java/wniemiec/util/java/StringUtilsTest.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@
88
class StringUtilsTest {
99

1010
@Test
11-
void testHelloWorld() {
11+
void testImplode() {
1212
List<String> items = List.of("hello", "world");
1313
String obtained = StringUtils.implode(items, " ");
1414

1515
Assertions.assertEquals("hello world", obtained);
1616
}
1717

1818
@Test
19-
void testNullList() {
19+
void testImplodeWithNullList() {
2020
Assertions.assertThrows(IllegalArgumentException.class, () -> {
2121
StringUtils.implode(null, " ");
2222
});
2323
}
2424

2525
@Test
26-
void testNullDelimiter() {
26+
void testImplodeWithNullDelimiter() {
2727
List<String> items = List.of("hello", "world");
2828

2929
Assertions.assertThrows(IllegalArgumentException.class, () -> {
@@ -32,7 +32,7 @@ void testNullDelimiter() {
3232
}
3333

3434
@Test
35-
void testNullDelimiterAndList() {
35+
void testImplodeWithNullDelimiterAndList() {
3636
Assertions.assertThrows(IllegalArgumentException.class, () -> {
3737
StringUtils.implode(null, null);
3838
});
@@ -68,4 +68,35 @@ void testCapitalizeNullText() {
6868
StringUtils.capitalize(null);
6969
});
7070
}
71+
72+
@Test
73+
void testCapitalizeOnlyFirstLetterWithTextWith1Character() {
74+
String text = "h";
75+
String obtained = StringUtils.capitalizeOnlyFirstLetter(text);
76+
77+
Assertions.assertEquals("H", obtained);
78+
}
79+
80+
@Test
81+
void testCapitalizeOnlyFirstLetterWithTextWithMoreThan1Characters() {
82+
String text = "hello wOrLd";
83+
String obtained = StringUtils.capitalizeOnlyFirstLetter(text);
84+
85+
Assertions.assertEquals("Hello wOrLd", obtained);
86+
}
87+
88+
@Test
89+
void testCapitalizeOnlyFirstLetterWithEmptyText() {
90+
String text = "";
91+
String obtained = StringUtils.capitalizeOnlyFirstLetter(text);
92+
93+
Assertions.assertEquals("", obtained);
94+
}
95+
96+
@Test
97+
void testCapitalizeOnlyFirstLetterWithNullText() {
98+
Assertions.assertThrows(IllegalArgumentException.class, () -> {
99+
StringUtils.capitalizeOnlyFirstLetter(null);
100+
});
101+
}
71102
}

0 commit comments

Comments
 (0)