Operating System - HP-UX
1832516 Members
5137 Online
110043 Solutions
New Discussion

How to claculate the len of str having sapce?

 
SOLVED
Go to solution
diwa
Frequent Advisor

How to claculate the len of str having sapce?

Hi,

I am trying to calculate the length of the string having sapce in between.

E.G
Str="My Name is xyz"

Can some one please suggest that how to do this?

Thanks
5 REPLIES 5
Yogeeraj_1
Honored Contributor
Solution

Re: How to claculate the len of str having sapce?

hi Diwa,

please try:

# Str="My Name is xyz"
# echo $Str | awk '{ print length }'
14
#

or
#Str="My Name is xyz"
#lengthStr=$(echo $Str | awk '{ print length }')
#echo $lengthStr
14


hope this helps!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
diwa
Frequent Advisor

Re: How to claculate the len of str having sapce?

Hi,

Thanks a lot. It is working for me.

Thanks
Yogeeraj_1
Honored Contributor

Re: How to claculate the len of str having sapce?

hi again,

the perl alternative will look as follows:


lengthStr=$(echo $Str | perl -nle 'print length')


hope this helps too!

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
diwa
Frequent Advisor

Re: How to claculate the len of str having sapce?

Hi,

I got satisfactory answer for my query.

Thanks
Peter Nikitka
Honored Contributor

Re: How to claculate the len of str having sapce?

Hi,

I didn't see the most simple solution in the posix shell:
Str="My Name is xyz"
print ${#Str}

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"