Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…
Cannot retrieve contributors at this time
69 lines (54 sloc) 1.83 KB
package DDG::Goodie::Chars;
# ABSTRACT: Give the number of characters (length) of the query.
use strict;
use DDG::Goodie;
use Text::Trim;
triggers startend =>
'number of characters',
'number of chars',
'num chars',
'num characters',
'char count',
'character count',
'characters count',
'length of string',
'length in characters',
'length in chars';
zci answer_type => "chars";
zci is_cached => 1;
name 'Character Counter';
description 'Count the number of charaters in a query';
primary_example_queries 'chars in "my string"';
code_url '';
category 'computing_tools';
topics 'programming';
handle remainder => sub {
my ($str) = @_;
return if !$str;
# remove leading word 'in',
# e.g. 'chars in mississippi' would just count the string 'mississippi'.
$str =~ s/^\s*in\b//;
# trim spaces at beg and end of string
$str = trim $str;
# if nothing left in the string, return without triggering the IA.
# this means the remainder contained only the word 'in' and/or spaces.
return if !$str;
# if surrounded by quotation marks (either ' or ")
# remove so we don't include them in the count
$str =~ s/^["'](.*)["']$/$1/;
# get the length of the string in characters
my $len = length($str);
# pluralize the word 'character' unless length is 1.
# note that this works for length=0, i.e. we'll correctly get '0 characters'.
my $characters_pluralized = ($len == 1 ? 'character' : 'characters');
# build the output string
my $text_out = qq("$str" is $len $characters_pluralized long.);
return $text_out,
structured_answer => {
input => [html_enc($str)],
operation => 'Character count',
result => $len
Jump to Line
Something went wrong with that request. Please try again.