indexOf

fun ByteString.indexOf(byte: Byte, startIndex: Int = 0): Int(source)

Returns the index within this byte string of the first occurrence of the specified byte, starting from the specified startIndex. If the byte not found, -1 is returned.

Behavior of this method is compatible with CharSequence.indexOf.

Parameters

byte

the value to search for.

startIndex

the index (inclusive) starting from which the byte should be searched.

Samples

import kotlinx.io.bytestring.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val string = ByteString(1, 2, 3, 2, 1)

assertEquals(1, string.indexOf(2))
assertEquals(3, string.indexOf(2, startIndex = 2))
assertEquals(-1, string.indexOf(0)) 
   //sampleEnd
}

fun ByteString.indexOf(byteString: ByteString, startIndex: Int = 0): Int(source)

Returns the index within this byte string of the first occurrence of the specified byteString, starting from the specified startIndex. If the byteString not found, -1 is returned.

Behavior of this method is compatible with CharSequence.indexOf.

Parameters

byteString

the value to search for.

startIndex

the index (inclusive) starting from which the byteString should be searched.

Samples

import kotlinx.io.bytestring.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val string = ByteString(1, 2, 3, 4, 1, 3, 4)

assertEquals(2, string.indexOf(ByteString(3, 4)))
assertEquals(5, string.indexOf(ByteString(3, 4), startIndex = 3))
assertEquals(-1, string.indexOf(ByteString(1, 1, 1)))
assertEquals(-1, string.indexOf(ByteString(1, 3, 4, 5)))
assertEquals(0, string.indexOf(ByteString(/* empty byte string */))) 
   //sampleEnd
}

fun ByteString.indexOf(byteArray: ByteArray, startIndex: Int = 0): Int(source)

Returns the index within this byte string of the first occurrence of the specified byteArray, starting from the specified startIndex. If the byteArray not found, -1 is returned.

Behavior of this method is compatible with CharSequence.indexOf.

Parameters

byteArray

the value to search for.

startIndex

the index (inclusive) starting from which the byteArray should be searched.

Samples

import kotlinx.io.bytestring.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val string = ByteString(1, 2, 3, 4, 1, 3, 4)

assertEquals(2, string.indexOf(byteArrayOf(3, 4)))
assertEquals(5, string.indexOf(byteArrayOf(3, 4), startIndex = 3))
assertEquals(-1, string.indexOf(byteArrayOf(1, 1, 1)))
assertEquals(-1, string.indexOf(byteArrayOf(1, 3, 4, 5)))
assertEquals(0, string.indexOf(byteArrayOf(/* empty byte array */))) 
   //sampleEnd
}