RegEx vs字符串操作函数:什么更好 [英] RegEx vs string manipulation functions: What is better

查看:88
本文介绍了RegEx vs字符串操作函数:什么更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我必须要找到让我们说在一个句子一个字,我能想到的两种方法

If I have to find let's say a word in a sentence, i can think of two approaches


  1. 使用string.IndexOf

  2. 使用正则表达式

哪一个是在性能或最佳实践方面更好

Which one is better in terms of performance or best practice

推荐答案

这要看您的具体要求。如果你真的需要找到一个句子(而不是子)一个的的,那么我相信可以更简洁地表达,更明确地使用公命名的正则表达式比使用的IndexOf加上所有的额外逻辑,以确保你实际上得到一个完整的单词。

It depends on your exact requirements. If you truly need to find a word in a sentence (not a substring), then I believe that could be expressed more concisely and more explicitly using a well-named regex pattern than using IndexOf plus all the extra logic to make sure you're actually getting a complete single word.

在另一方面,如果你只是寻找一个字符串,那么的IndexOf是远远优于的性能和可读性方面

On the other hand, if you're simply looking for a substring, then IndexOf is far superior in terms of performance and readability.

这篇关于RegEx vs字符串操作函数:什么更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆