Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: Java | Size: 1.35 KB | Hits: 15 | Expires: Never
Copy text to clipboard
  1. public static String sliceString(String source, int begin, int end, int step)
  2.         {
  3.                 String newStr = "";
  4.                 if (step == 0)
  5.                         return newStr;
  6.                 if (begin < -source.length())
  7.                         begin = 0;
  8.                 else if (begin > source.length()-1)
  9.                         begin = source.length() -1;
  10.                 if (end > source.length())
  11.                         end = source.length();
  12.                 else if( end < -source.length() - 1)
  13.                         end = -1;
  14.                 if (begin <= -1 && begin >= -source.length())
  15.                         begin += source.length();
  16.                 if (end <=-1 && end >= -source.length()-1)
  17.                         end += source.length();
  18.                 if (step>0)
  19.                 {
  20.                         if (end <= begin)
  21.                         {
  22.                                 int i = begin;
  23.                                 while (i < source.length())
  24.                                 {
  25.                                         newStr+=source.charAt(i);
  26.                                         i += step;
  27.                                 }
  28.                                
  29.                                 i = i - source.length();
  30.                                 while (i < end)
  31.                                 {
  32.                                         newStr+=source.charAt(i);
  33.                                         i += step;
  34.                                 }
  35.                         }
  36.                         else
  37.                         {
  38.                                 int i = begin;
  39.                                 while (i < end)
  40.                                 {
  41.                                         newStr+= source.charAt(i);
  42.                                         i+=step;
  43.                                 }
  44.                         }
  45.                 }
  46.                 else if (step<0)
  47.                 {
  48.                         if (end >= begin)
  49.                         {
  50.                                 int i = begin;
  51.                                 while (i >= 0)
  52.                                 {
  53.                                         newStr+=source.charAt(i);
  54.                                         i += step;
  55.                                 }
  56.                                
  57.                                 i = (source.length()) + i;
  58.                                 while (i > end)
  59.                                 {
  60.                                         newStr+=source.charAt(i);
  61.                                         i += step;
  62.                                 }
  63.                         }
  64.                         else
  65.                         {
  66.                                 int i = begin;
  67.                                 while (i > end)
  68.                                 {
  69.                                         newStr+= source.charAt(i);
  70.                                         i+=step;
  71.                                 }
  72.                         }
  73.                 }
  74.                 return newStr;
  75.         }