简体   繁体   English

根据条件替换第一个和最后一个字符(哪种方法更快)?

[英]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("\\"",""); 使用line.replace("\\"","");
  2. Validating that first character is '"', then making a substring from character 1 to length - 1. 验证第一个字符为“”,然后创建一个从字符1到长度-1的子字符串。

Or even, is there a better way to do this? 甚至,还有更好的方法吗?

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. 它将最多执行一个字符比较和一个数组副本,而不是将字符串中的所有字符与“字符”进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM