+ Start a Discussion

How do you get substring to end at first space?

I am trying to create a substring that will be varying lengths. So, how can I get the end index of the substring to be the first space?
I have tried the following:
String signature = signedby[1].substring(0); This returns everything after the split.
String signature = signedby[1].substring(0).trim(); This returns everything after the split.

The only way I can get the substring to terminate is to create a concrete end index for example .substring(0,15) which means I will lose any charaters that are greater than 15. I need the substring to be able to adapt to any string length and end where the first space occurs.
So - you're basically trying to pull off the first word of your string?

I'd go:

String signature = myString.split ( ' ' )[ 0 ];

Just one of many ways to skin this cat...