#Is there a shorter way to do this?

1 messages · Page 1 of 1 (latest)

regal torrent
#

Is it possible to substring and captilize at the same time?
String name=uniEmail.substring(0,4);
name=name.substring(0,1).toUpperCase()+name.substring(1,4);
System.out.print(name);

zenith frigateBOT
#

<@&987246399047479336> please have a look, thanks.

zenith frigateBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

paper basalt
#

Nope

#

That's basically it, tbh.

#

Guava or Apache Commons might have some utility to do this for you, but no need to import an entire library for a single function.

golden saddle
#

Theres nothing defined in java's API for that. And a function like that typically would violate some programming 'rule' that says a function should only do one task. Hence you would have 1 function to uppercase, and one to trim it down. You can write your own method like

public static String ucFirst(String s) {
    if (s == null) return null;
    if (s.length() <= 1) return s.toUpperCase();
    return s.substring(0,1).toUpperCase() + s.substring(1).toLowerCase();
}```
then 
```java
ucFirst(name).substring(0, 4) // missing error checking on substring upper bound and non null
regal torrent
#

Thank you all!