Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

d programming
searching
communitycreator

What is the canFind() method in D language?

NDUKWE CHIDERA K.

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The canFind() method is an inbuilt function that is a part of the standard library in D language. This function will carry out a search and find operation on a set of values.

With this function, a variable will be searched to find another variable inside it.
It is called a Boolean operation because it will return either “true” or “false”. This depends on the value being searched for and if it can be found where ever it is being searched.

Syntax

canFind(predicate,where_to_check,what_to_check)
  • predicate: This is the predicate or comparison format we use to make the comparison among the elements. It is an optional parameter.
  • where_to_check: This is a string or array which we’ll check in order to find the specified value.
  • what_to_check: This is the value that we’ll check for in the where_to_check parameter in order to find a match.

Return value

The canFind function returns true if and only if any value of what_to_check is found in the search input range where_to_check, which satisfies the predicate comparison condition. It will return false if not.

import std.stdio;
import std.algorithm;
void main(){
int [] search_nums = [1,2,3,4,5,6];
string search_text = "This is a string to search";
if(canFind(search_text,"is")){
writeln("It found it, hurray");
}
else if(canFind(search_text,","))
{
writeln("It could not find it");
}
}

Explanation

  • Line 1–2: We import the necessary modules.
  • Line 4: We start the main function.
  • Lines 5–6: Then we’ll declare new variables.
  • Lines 8–14: This contains an if loop whose condition is set using the canFind() method.

RELATED TAGS

d programming
searching
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring