What is the findSplitAfter method in D Language?

Overview

This function is part of the std.algorithm.searching module in D programming. The findSplitAfter() method is helpful for carrying out a search and match on strings and arrays.

Functionality

The findSplitAfter() method will search through an array, a string, or a tuple value to find a particular search term.

This method will return a tuple result that includes two ranges if the search term is found. Range 1, or say output[0], is the part of the array or string that was searched to the point of finding the match, with the match inclusive as well. Range 2: output[1] is the other part of the searched array or string starting after the match.

If no match is found, output[0] will be empty, and output[1] will be the entirety of the searched array.

Syntax

findSplitAfter(predicate,searchin,search_for)

Parameters

  • predicate: We are using either an optional parameter, or any custom or user-defined comparison format.
  • search_in: This is the string or array that we’ll check to find the search_for value.
  • search_for: The value that we’ll check for in search_in for a match.

Return value

It returns a tuple of two ranges of values.

import std.stdio;
import std.algorithm.searching;
void main(){
string search_text ="A platform for Byte-sized dev shots";
int [] search_array = [2,4,6,8,10];
//provided a wrong search word
writeln("The whole string is returned as output[1] and output[0] left empty");
writeln(findSplitAfter(search_array,[9]));
//perform a findSplitAfter operation and save return value
auto holder = findSplitAfter(search_text,"-");
//print to screen the value of holder different instances
writeln("\n",holder);
writeln("\n Output[0] gives :",holder[0]);
writeln("\n Output[1] gives :",holder[1]);
}

Explanation

  • Line 1–2: We import the required in-built modules to access the function findSplitAfter().

  • Line 4: Here, the main function starts, with the values being used to define on lines 5–6.

  • Line 9: We use an output to display some text.

  • Line 10: We perform a findSplitAfter operation and print its return value to output.

  • Line 13: Then we use the holder variable to save the outcome of the findSplitAfter operation on the variable search_text.

  • Lines 15–17: These lines contain the values of holder being printed to display in different aspects.

Free Resources