We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Truncating strings
Page Index Toggle Pages: 1
Truncating strings (Read 1027 times)
Truncating strings
May 11th, 2010, 3:57pm
 
I have a bunch of long strings I need to capture just the first 20 characters of.

I know I can use loop with char and append a new string (or stringbuffer), and Ill probably end up doing this, I'm just wondering if there's a quick and easy function I don't know about.

It would be something akin to LEFT(reference,#chars) in Excel.
Re: Truncating strings
Reply #1 - May 11th, 2010, 6:17pm
 
jbb wrote on May 11th, 2010, 3:57pm:
I have a bunch of long strings I need to capture just the first 20 characters of.

I know I can use loop with char and append a new string (or stringbuffer), and Ill probably end up doing this, I'm just wondering if there's a quick and easy function I don't know about.

It would be something akin to LEFT(reference,#chars) in Excel.


You want the substring() method:
Code:

String big;

// big gets set to something

String little = big.substring(0, 20);

I don't know if this will barf if big has less than 20 characters; if you want to be safe, check that (big.length()>=20) before taking the substring.
Re: Truncating strings
Reply #2 - May 17th, 2010, 8:30pm
 
Perfect!  Thanks, Smitty.
Page Index Toggle Pages: 1