Muthukumar
Muthukumar

Reputation: 113

get Starting space in String using perl

I'm trying to get white space and assign it to a string using perl.

I have a string

$line = " Testing purpose"

I want to get the white space in that string and assign it to another string. Can any one help me here?

$line = "       Testing purpose";

$space = " "; ( This space variable will have space from $line.)

Upvotes: 0

Views: 62

Answers (1)

ali-hussain
ali-hussain

Reputation: 511

Use regular expressions.

$line =~ m/^(\s*)/;
$space = $1;

This will match spaces and tabs, leaving original string unmodified. If you want to also remove space from original string, use this:

$line =~ s/^(\s*)//;
$space = $1;

Upvotes: 2

Related Questions