如何找到子字符串的索引? [英] How to find index of a substring?

查看:85
本文介绍了如何找到子字符串的索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在寻找与Ruby等效的Elixir:

Looking for Elixir equivalent of Ruby's:

"john.snow@domain.com".index("@")         # => 9
"john.snow@domain.com".index("domain")    # => 10


推荐答案

TL; DR:String.index / 2是由于存在更智能的替代方案,因此故意丢失了。 String.split / 2 通常会解决潜在的问题-并且具有更好的性能。

TL;DR: String.index/2 is intentionally missing because smarter alternatives exist. Very often String.split/2 will solve the underlying problem - and with a way better performance.


  • 我假设我们在这里谈论的是UTF-8字符串,并希望能够干净地处理非ASCII字符。

  • I assume we are talking UTF-8 strings here and expect to cleanly deal with non-ASCII characters.

Elixir鼓励使用快速代码。事实证明,我们通常尝试使用String.index / 2解决的问题可以用一种更加智能的方式来解决,从而在不降低代码可读性的情况下极大地提高了性能。

Elixir encourages fast code. It turns out that problems we usually try solve with String.index/2 can be solved in a much smarter way, vastly improving performance without degrading code readability.

更聪明的解决方案是使用String.split / 2和/或其他类似的String模块功能。 String.split / 2在字节级别上运行,同时仍可以正确处理字素。不会出错,因为两个参数都是字符串! String.index / 2必须在字素级别上工作,并缓慢地遍历整个String。

The smarter solution is to use String.split/2 and/or other similar String module functions. The String.split/2 works on a byte-level while still correctly handling graphemes. It can't go wrong because both arguments are Strings! The String.index/2 would have to work on a grapheme-level, slowly seeking throughout the String.

因此,String.index / 2为除非出现非常引人注目的用例,而这些用例不能被现有功能彻底解决。

For that reason the String.index/2 is unlikely be added to the language unless very compelling use cases come up that cannot be cleanly solved by existing functions.

另请参见关于elixir-lang-core的讨论,没关系:
https://groups.google。 com / forum /#!topic / elixir-lang-core / S0yrDxlJCss

See also the elixir-lang-core discussion on that matter: https://groups.google.com/forum/#!topic/elixir-lang-core/S0yrDxlJCss

Elixir在其成熟的Unicode中非常独特支持。虽然大多数语言都在代码点级别上工作(俗称字符),但Elixir却使用了更高的字素概念。字素是用户认为的一个字符(更确切地说,它是对字符的理解)。字素可以包含多个代码点(依次可以包含多个字节)。

On a side note, Elixir is pretty unique in its mature Unicode support. While most languages work on a codepoint level (colloquially "characters"), Elixir works with a higher level concept of graphemes. Graphemes are what users perceive as one character (lets say its a more practical understanding of a "character"). Graphemes can contain more than one codepoint (which in turn can contain more than one byte).

最后,如果我们确实需要索引:

Finally, if we really need the index:

case String.split("john.snow@domain.com", "domain", parts: 2) do
  [left, _] -> String.length(left)
  [_] -> nil
end

这篇关于如何找到子字符串的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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