htafoya
htafoya

Reputation: 19273

Replace first and last character upon condition (which method is faster)?

I have a list of thousands of strings that may come in the form:

word

or

"more than one word"

I want to remove the "" whenever they are present, so that I can save the string to my DB.

I'm wondering which method is faster:

  1. Using line.replace("\"","");
  2. Validating that first character is '"', then making a substring from character 1 to length - 1.

Or even, is there a better way to do this?

Upvotes: 1

Views: 124

Answers (1)

yole
yole

Reputation: 97148

The second method is faster. It will perform at most one character comparison and one array copy, instead of comparing all characters in the string with the " character.

Upvotes: 1

Related Questions