contains

contains(haystack, needle)

Determine whether the second argument is a substring of the first.

Examples

In the Julia programming language, the function contains(haystack, needle)

Determine whether the second argument is a substring of the first.

julia> contains("hello world", "world")
true

julia> contains("julia programming language", "python")
false

Common examples of its use:

  1. Check if a string contains a specific substring:

    julia> str = "Julia is a high-level programming language";
    julia> contains(str, "high-level")
    true

    This example checks if the string str contains the substring "high-level".

  2. Find if an array contains a specific element:

    julia> arr = [1, 2, 3, 4, 5];
    julia> contains(arr, 3)
    true

    It checks if the array arr contains the element 3.

  3. Check if a string contains a character:
    julia> str = "Hello, Julia!";
    julia> contains(str, 'J')
    true

    This example checks if the string str contains the character 'J'.

Common mistake example:

julia> contains("julia programming language", "Julia")
ERROR: MethodError: no method matching contains(::String, ::String)

In this example, the contains function is called with two string arguments. However, Julia's contains function does not support substring matching for strings. It is important to note that the contains function in Julia is primarily used for checking if an element exists in a collection, rather than substring matching in strings.

See Also

ascii, base64decode, Base64DecodePipe, base64encode, Base64EncodePipe, bin, bits, bytestring, charwidth, chomp, chop, chr2ind, contains, endswith, escape_string, graphemes, ind2chr, iscntrl, istext, isupper, isvalid, join, lcfirst, lowercase, lpad, lstrip, normalize_string, num2hex, parseip, randstring, readuntil, replace, repr, rpad, rsplit, rstrip, search, searchindex, split, startswith, string, stringmime, strip, strwidth, summary, takebuf_string, ucfirst, unescape_string, uppercase, utf16, utf32, utf8, wstring,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: