## digitalmars.D.learn - Check if give an array is equal to first elements in another array

```Is there one function in the Phobos library to check if give an
array is equal to first elements in another array? e.g, f(a, b)
the entire b array must match to first elements in a and then
return true otherwise false, ie:

a[0 .. b.length] == b

probably there's no such a function (but I wouldn't find bad if
there was) in the D's library so what's a good name (that'a
descriptive) to such a function?

btw, what it solves is:

if(a.length >= b1.length && a[0 .. b1.length] == b1) { ... }
if(a.length >= b2.length && a[0 .. b2.length] == b2) { ... }

since if bx.length < a.length is valid I can't remove all the
a.length >= bx.length checks otherwise I can have a range
violation error
```
Oct 15 2014
```On Thursday, 16 October 2014 at 04:35:13 UTC, MachineCode wrote:
Is there one function in the Phobos library to check if give an
array is equal to first elements in another array?

auto n = min(a.length, b.length);
if (a[0..n] == b[0..n]) ...
```
Oct 15 2014 =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
```On 10/15/2014 09:35 PM, MachineCode wrote:
Is there one function in the Phobos library to check if give an array is
equal to first elements in another array? e.g, f(a, b) the entire b
array must match to first elements in a and then return true otherwise
false, ie:

a[0 .. b.length] == b

probably there's no such a function (but I wouldn't find bad if there
was) in the D's library so what's a good name (that'a descriptive) to
such a function?

btw, what it solves is:

if(a.length >= b1.length && a[0 .. b1.length] == b1) { ... }
if(a.length >= b2.length && a[0 .. b2.length] == b2) { ... }

since if bx.length < a.length is valid I can't remove all the a.length
>= bx.length checks otherwise I can have a range violation error

import std.algorithm;

void main()
{
auto a = [ 1, 42, 100, 7 ];
auto b = [ 1, 42 ];
assert(a.startsWith(b));
}

Ali
```
Oct 15 2014
{"ft_lang_label":"__label__en","ft_lang_prob":0.71591705,"math_prob":0.95954424,"size":2258,"snap":"2021-43-2021-49","text_gpt3_token_len":680,"char_repetition_ratio":0.12999113,"word_repetition_ratio":0.7051282,"special_character_ratio":0.33525243,"punctuation_ratio":0.21601489,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99080014,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-21T02:44:28Z\",\"WARC-Record-ID\":\"<urn:uuid:6b6d3d0a-a0ae-4e96-8b66-7f706c491fff>\",\"Content-Length\":\"13271\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ba4962b1-5538-4b1c-a370-73346680ae0d>\",\"WARC-Concurrent-To\":\"<urn:uuid:3c581b67-d545-4a1b-8b71-74eb0d77902e>\",\"WARC-IP-Address\":\"162.217.114.56\",\"WARC-Target-URI\":\"https://www.digitalmars.com/d/archives/digitalmars/D/learn/Check_if_give_an_array_is_equal_to_first_elements_in_another_array_64731.html\",\"WARC-Payload-Digest\":\"sha1:EZETIKPIX3TB6OXHYVALUCD2OTPSWOTA\",\"WARC-Block-Digest\":\"sha1:MKUQAGNRMODM5NSVPKH5T3CXJAYSM2AZ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585380.70_warc_CC-MAIN-20211021005314-20211021035314-00329.warc.gz\"}"}