File tree Expand file tree Collapse file tree
main/java/com/baeldung/string
test/java/org/baeldung/java Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33import java .util .Optional ;
44
5- public class StringHelper {
6- public static String removeLastChar (String s ) {
5+ class StringHelper {
6+ static String removeLastChar (String s ) {
77 return (s == null || s .length () == 0 ) ? s : (s .substring (0 , s .length () - 1 ));
88 }
99
10- public static String removeLastCharRegex (String s ) {
10+ static String removeLastCharRegex (String s ) {
1111 return (s == null ) ? s : s .replaceAll (".$" , "" );
1212 }
1313
14- public static String removeLastCharOptional (String s ) {
14+ static String removeLastCharOptional (String s ) {
1515 return Optional .ofNullable (s )
1616 .filter (str -> str .length () != 0 )
1717 .map (str -> str .substring (0 , str .length () - 1 ))
1818 .orElse (s );
1919 }
2020
21- public static String removeLastCharRegexOptional (String s ) {
21+ static String removeLastCharRegexOptional (String s ) {
2222 return Optional .ofNullable (s )
2323 .map (str -> str .replaceAll (".$" , "" ))
2424 .orElse (s );
Original file line number Diff line number Diff line change @@ -167,9 +167,11 @@ public void givenUsingPlainJava_whenGeneratingRandomStringBounded_thenCorrect()
167167 final int leftLimit = 97 ; // letter 'a'
168168 final int rightLimit = 122 ; // letter 'z'
169169 final int targetStringLength = 10 ;
170+ final Random random = new Random ();
170171 final StringBuilder buffer = new StringBuilder (targetStringLength );
172+
171173 for (int i = 0 ; i < targetStringLength ; i ++) {
172- final int randomLimitedInt = leftLimit + (int ) (new Random () .nextFloat () * (rightLimit - leftLimit + 1 ));
174+ final int randomLimitedInt = leftLimit + (int ) (random .nextFloat () * (rightLimit - leftLimit + 1 ));
173175 buffer .append ((char ) randomLimitedInt );
174176 }
175177 final String generatedString = buffer .toString ();
You can’t perform that action at this time.
0 commit comments