# findall


在指定域内查找目标内容并返回所有匹配的索引

# 语法

findall(A)
findall(f, A)
findall(pattern, string; overlap)

# 说明

findall(A) 返回Bool数组A中元素为true的索引 示例


findall(f,A)示例


findall(pattern,string;overlap)示例

# 示例

逻辑搜索

直接对对象使用findall(),不使用任何其他参数,将对进行逻辑判断返回真值索引

findall([false,true,true])
ans = 2-element Vector{Int64}:
2
3

对于 Matrix{Bool} 使用 findall() 将返回笛卡尔索引(CartesianIndex)

A = [true false]
1×2 Matrix{Bool}:
1  0
findall(A)
1-element Vector{CartesianIndex{2}}:
CartesianIndex(1, 1)
指定条件索引搜索

通过使用f函数参数,可以对目标进行函数操作后对输出值进行逻辑判断返回索引

x = [1, 3, 4]
findall(isodd, x)
2-element Vector{Int64}:
1
2

字符搜索

对字符串对象使用findall(),设定匹配的模式进行字符搜索

findall("a", "apple")
1-element Vector{UnitRange{Int64}}:
1:1
findall("ana", "banana")
1-element Vector{UnitRange{Int64}}:
2:4
findall("ana", "banana", overlap = true)
2-element Vector{UnitRange{Int64}}:
2:4
4:6

# 输入参数

f-函数

将搜索域内元素通过函数后再进行逻辑真值判断

  • 可选
  • 在不使用该参数时仅能直接对逻辑数组进行运算(Array{Bool})
A-搜索域

findall()的作用域,通常为数组,在不使用f函数参数时必须为Bool值或Bool元素构成的Array

pattern-匹配模式
String

字符串匹配的模式

  • 仅支持精准匹配,不支持模糊匹配和正则表达式
  • 不支持char和char数组

数据类型: String

string-字符搜索域
String

findall()使用字符搜索时作用的搜索范围

  • 仅支持String,不支持char数组

数据类型: String

overlap-允许重叠
布尔(Boolean)

开关型参数,用于控制搜索时是否重叠搜索

  • 缺省默认为 false,即不开启重叠搜索

数据类型: Bool

# 另请参阅

findfirst | findlast | findnext | findprev