pyspark.sql.functions.instr

pyspark.sql.functions.instr(str: ColumnOrName, substr: str) → pyspark.sql.column.Column[source]

Locate the position of the first occurrence of substr column in the given string. Returns null if either of the arguments are null.

New in version 1.5.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
strColumn or str

target column to work on.

substrstr

substring to look for.

Returns
Column

location of the first occurence of the substring as integer.

Notes

The position is not zero based, but 1 based index. Returns 0 if substr could not be found in str.

Examples

>>> df = spark.createDataFrame([('abcd',)], ['s',])
>>> df.select(instr(df.s, 'b').alias('s')).collect()
[Row(s=2)]