How could I take 8 characters of a string, for example:
If I has like this:
my $word ="take first 8 characters";
How could I print this: take fir
?
You can access the first characters of a string with substr(). To get the first character, for example, start at position 0 and grab the string of length 1.
substr() in Perl returns a substring out of the string passed to the function starting from a given index up to the length specified. This function by default returns the remaining part of the string starting from the given index if the length is not specified.
$@ The Perl syntax error or routine error message from the last eval, do-FILE, or require command. If set, either the compilation failed, or the die function was executed within the code of the eval.
\b is the backspace character only inside a character class. Outside a character class, \b alone is a word-character/non-word-character boundary.
Use substr
.
print substr($word, 0, 8);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With