Hi,
I have a string like 'xyz' and i want to to split the above string into letters for comparision purpose.
any idea pls.
cheers
RRK.
You can use expr
var="abc"
expr substr $var 1 1
expr substr $var 2 1
expr substr $var 3 1
Hi.
You can use the special parameter expansion syntax:
#!/usr/bin/env sh
# @(#) s1 Demonstrate sub-string extraction of parameter expansion.
set -o nounset
echo
## Define function to list versions.
version()
{
for command
do
if command -v $command >/dev/null 2>&1
then
command -p $command --version | head -1
else
echo " (Warning -- command \"$command\" not found in PATH.)" >&2
fi
done
return 0
}
## List the version of the commands in this demonstration.
version bash head
echo
s="xyz"
l=${#s}
echo " There are $l characters in |$s|"
echo
for ((i=0;i<l;i++))
do
echo " Character $i of the string is |${s:$i:1}|"
done
exit 0
Producing:
% ./s1
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
head (coreutils) 5.2.1
There are 3 characters in |xyz|
Character 0 of the string is |x|
Character 1 of the string is |y|
Character 2 of the string is |z|
See man bash for details ... cheers, drl
string=xyz
while [ -n "$string" ]
do
temp=${string#?}
letter=${string%"$temp"}
do_something_with "$letter"
string=$temp
done