{
    "mode": "perldoc",
    "parameter": "String::Util",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/String%3A%3AUtil/json",
    "generated": "2026-10-07T11:10:50Z",
    "sections": {
        "NAME": {
            "content": "String::Util -- String processing utility functions\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "String::Util provides a collection of small, handy functions for processing strings in various\nways.\n",
            "subsections": []
        },
        "INSTALLATION": {
            "content": "cpanm String::Util\n",
            "subsections": []
        },
        "USAGE": {
            "content": "No functions are exported by default, they must be specified:\n\nuse String::Util qw(trim eqq contains)\n\nalternately you can use \":all\" to export all of the functions\n\nuse String::Util qw(:all)\n",
            "subsections": []
        },
        "FUNCTIONS": {
            "content": "collapse($string)",
            "subsections": [
                {
                    "name": "collapse",
                    "content": "leading and trailing whitespace. Undefined input results in undefined output.\n\n$var = collapse(\"  Hello     world!    \"); # \"Hello world!\"\n\nhascontent($scalar), nocontent($scalar)"
                },
                {
                    "name": "hascontent",
                    "content": "whitespace.\n\nAn undefined value returns false. An empty string returns false. A value containing nothing but\nwhitespace (spaces, tabs, carriage returns, newlines, backspace) returns false. A string\ncontaining any other characters (including zero) returns true.\n"
                },
                {
                    "name": "nocontent",
                    "content": "$var = hascontent(\"\");  # False\n$var = hascontent(\" \"); # False\n$var = hascontent(\"a\"); # True\n\n$var = nocontent(\"\");   # True\n$var = nocontent(\"a\");  # False\n\ntrim($string), ltrim($string), rtrim($string)\nReturns the string with all leading and trailing whitespace removed.\n\n$var = trim(\" my string  \"); # \"my string\"\n"
                },
                {
                    "name": "ltrim",
                    "content": ""
                },
                {
                    "name": "rtrim",
                    "content": "nospace($string)\nRemoves all whitespace characters from the given string. This includes spaces between words.\n\n$var = nospace(\"  Hello World!   \"); # \"HelloWorld!\"\n\nhtmlesc($string)\nFormats a string for literal output in HTML. An undefined value is returned as an empty string.\n"
                },
                {
                    "name": "htmlesc",
                    "content": ""
                },
                {
                    "name": "htmlesc",
                    "content": "undefs.\n\njsquote($string)\nEscapes and quotes a string for use in JavaScript. Escapes single quotes and surrounds the\nstring in single quotes. Returns the modified string.\n\nunquote($string)\nIf the given string starts and ends with quotes, removes them. Recognizes single quotes and\ndouble quotes. The value must begin and end with same type of quotes or nothing is done to the\nvalue. Undef input results in undef output. Some examples and what they return:\n\nunquote(q|'Hendrix'|);   # Hendrix\nunquote(q|\"Hendrix\"|);   # Hendrix\nunquote(q|Hendrix|);     # Hendrix\nunquote(q|\"Hendrix'|);   # \"Hendrix'\nunquote(q|O'Sullivan|);  # O'Sullivan\n\noption: braces\n\nIf the braces option is true, surrounding braces such as [] and {} are also removed. Some\nexamples:\n\nunquote(q|[Janis]|, braces=>1);  # Janis\nunquote(q|{Janis}|, braces=>1);  # Janis\nunquote(q|(Janis)|, braces=>1);  # Janis\n\nrepeat($string, $count)\nReturns the given string repeated the given number of times. The following command outputs\n\"Fred\" three times:\n\nprint repeat('Fred', 3), \"\\n\";\n\nNote that repeat() was created a long time based on a misunderstanding of how the perl operator\n'x' works. The following command using \"x\" would perform exactly the same as the above command.\n\nprint 'Fred' x 3, \"\\n\";\n\nUse whichever you prefer.\n\neqq($scalar1, $scalar2)\nReturns true if the two given values are equal. Also returns true if both are \"undef\". If only\none is \"undef\", or if they are both defined but different, returns false. Here are some examples\nand what they return.\n\n$var = eqq('x', 'x');     # True\n$var = eqq('x', undef);   # False\n$var = eqq(undef, undef); # True\n\nneqq($scalar1, $scalar2)\nThe opposite of \"neqq\", returns true if the two values are *not* the same. Here are some\nexamples and what they return.\n\n$var = neqq('x', 'x');     # False\n$var = neqq('x', undef);   # True\n$var = neqq(undef, undef); # False\n\nords($string)\nReturns the given string represented as the ascii value of each character.\n\n$var = ords('Hendrix'); # {72}{101}{110}{100}{114}{105}{120}\n\noptions\n\n*   convertspaces=>[true|false]\n\nIf convertspaces is true (which is the default) then spaces are converted to their matching\nord values. So, for example, this code:\n\n$var = ords('a b', convertspaces=>1); # {97}{32}{98}\n\nThis code returns the same thing:\n\n$var = ords('a b');                    # {97}{32}{98}\n\nIf convertspaces is false, then spaces are just returned as spaces. So this code:\n\nords('a b', convertspaces=>0);        # {97} {98}\n\n*   alphanums\n\nIf the alphanums option is false, then characters 0-9, a-z, and A-Z are not converted. For\nexample, this code:\n\n$var = ords('a=b', alphanums=>0); # a{61}b\n\ndeords($string)\nTakes the output from ords() and returns the string that original created that output.\n\n$var = deords('{72}{101}{110}{100}{114}{105}{120}'); # 'Hendrix'\n\ncontains($string, $substring)\nChecks if the string contains substring\n\n$var = contains(\"Hello world\", \"Hello\");   # true\n$var = contains(\"Hello world\", \"llo wor\"); # true\n$var = contains(\"Hello world\", \"QQQ\");     # false\n\n# Also works with grep\n@arr = grep { contains(\"cat\") } @input;\n\nstartswith($string, $substring)\nChecks if the string starts with the characters in substring\n\n$var = startwith(\"Hello world\", \"Hello\"); # true\n$var = startwith(\"Hello world\", \"H\");     # true\n$var = startwith(\"Hello world\", \"Q\");     # false\n\n# Also works with grep\n@arr = grep { startswith(\"X\") } @input;\n\nendswith($string, $substring)\nChecks if the string ends with the characters in substring\n\n$var = endswith(\"Hello world\", \"world\");   # true\n$var = endswith(\"Hello world\", \"d\");       # true\n$var = endswith(\"Hello world\", \"QQQ\");     # false\n\n# Also works with grep\n@arr = grep { endswith(\"z\") } @input;\n\ncrunchlines($string)\nCompacts contiguous newlines into single newlines. Whitespace between newlines is ignored, so\nthat two newlines separated by whitespace is compacted down to a single newline.\n\n$var = crunchlines(\"x\\n\\n\\nx\"); # \"x\\nx\";\n\nsanitize($string, $separator = \"\")\nSanitize all non alpha-numeric characters in a string to underscores. This is useful to take a\nURL, or filename, or text description and know you can use it safely in a URL or a filename.\n\nNote: This will remove any trailing or leading '' on the string\n\n$var = sanitize(\"http://www.google.com/\") # httpwwwgooglecom\n$var = sanitize(\"foobar()\";              # foobar\n$var = sanitize(\"/path/to/file.txt\");     # pathtofiletxt\n$var = sanitize(\"Big yellow bird!\", \".\"); # Big.yellow.bird\n\nfilegetcontents($string, $boolean)\nRead an entire file from disk into a string. Returns undef if the file cannot be read for any\nreason. Can also return the file as an array of lines.\n\n$str   = filegetcontents(\"/tmp/file.txt\");    # Return a string\n@lines = filegetcontents(\"/tmp/file.txt\", 1); # Return an array\n\nNote: If you opt to return an array, carriage returns and line feeds are removed from the end of\neach line.\n\nNote: File is read in UTF-8 mode, unless $FGCMODE is set to an appropriate encoding.\n"
                }
            ]
        },
        "COPYRIGHT AND LICENSE": {
            "content": "Copyright (c) 2012-2016 by Miko O'Sullivan. All rights reserved. This program is free software;\nyou can redistribute it and/or modify it under the same terms as Perl itself. This software\ncomes with NO WARRANTY of any kind.\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "Miko O'Sullivan <miko@idocs.com>\n\nScott Baker <scott@perturb.org>\n",
            "subsections": []
        }
    },
    "summary": "String::Util -- String processing utility functions",
    "flags": [],
    "examples": [],
    "see_also": []
}