Untitled
By: a guest | Feb 9th, 2010 | Syntax:
Java | Size: 1.35 KB | Hits: 15 | Expires: Never
public static String sliceString
(String source,
int begin,
int end,
int step
)
{
if (step == 0)
return newStr;
if (begin < -source.length())
begin = 0;
else if (begin > source.length()-1)
begin = source.length() -1;
if (end > source.length())
end = source.length();
else if( end < -source.length() - 1)
end = -1;
if (begin <= -1 && begin >= -source.length())
begin += source.length();
if (end <=-1 && end >= -source.length()-1)
end += source.length();
if (step>0)
{
if (end <= begin)
{
int i = begin;
while (i < source.length())
{
newStr+=source.charAt(i);
i += step;
}
i = i - source.length();
while (i < end)
{
newStr+=source.charAt(i);
i += step;
}
}
else
{
int i = begin;
while (i < end)
{
newStr+= source.charAt(i);
i+=step;
}
}
}
else if (step<0)
{
if (end >= begin)
{
int i = begin;
while (i >= 0)
{
newStr+=source.charAt(i);
i += step;
}
i = (source.length()) + i;
while (i > end)
{
newStr+=source.charAt(i);
i += step;
}
}
else
{
int i = begin;
while (i > end)
{
newStr+= source.charAt(i);
i+=step;
}
}
}
return newStr;
}