Recent Posts
JavaScript | 20 sec ago
None | 58 sec ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
C++ | 2 min ago
C++ | 2 min ago
None | 2 min ago
PHP | 2 min ago
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
By Anonymous on the 10th of Feb 2010 12:20:09 AM
Download |
Raw |
Embed |
Report
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;
}
Submit a correction or amendment below.
Make A New Post