Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1.     /**
  2.      * replaces a placeholders on an arguments. <br>For example: <br>"parseArg(a, b);" <br>with a equal to <br><i>"lp user %arg_0%meta setprefix %arg_1%"</i><br>and
  3.      * b is the array with elements: <br>"adffds", "fdsd" <br>will replace on <br><i>lp user adffds meta setprefix fdsd</i>
  4.      * @param str - string to replace from
  5.      * @param arg - arguments to which placeholders will replace
  6.      * @return parse with replaced placeholders
  7.      */
  8.     private static String parseArg(String str, String... args)
  9.     {
  10.         if (args.length < 1) return str;
  11.         boolean begin = false;
  12.         int first = -1;
  13.         int end = -1;
  14.         int j = -1;
  15.         String tmp = "";
  16.         for (int i = 0; i< str.length(); i++)
  17.         {
  18.             if (str.charAt(i) == '%')
  19.             {
  20.                 if (begin)
  21.                 {
  22.                     end = i;
  23.                     begin = false;
  24.                     if (tmp.equals(""))
  25.                     {
  26.                         str = str.subSequence(0, i-1).toString().concat(str.subSequence(i, str.length()).toString());
  27.                         i--;
  28.                         continue;
  29.                     }
  30.                     try
  31.                     {
  32.                         String[] s = tmp.split("_");
  33.                         if (s[0].equals("arg"))
  34.                         {
  35.                             j = Integer.parseInt(s[1]);                        
  36.                             String str1 = str.subSequence(0, first).toString();
  37.                             str1 = str1.concat(args[j]);
  38.                             str = str1.concat(str.subSequence(end+1, str.length()).toString());
  39.                             i = str1.length()-1;
  40.                         }
  41.                     }
  42.                     catch(ArrayIndexOutOfBoundsException e) {}
  43.                     continue;
  44.                 }
  45.                 begin = true;
  46.  
  47.                 first = i;
  48.                 tmp = "";
  49.                 i++;
  50.             }
  51.            
  52.             if (begin)
  53.             {
  54.                 while (str.charAt(i) != '%')
  55.                 {
  56.                     tmp += str.charAt(i);
  57.                     i++;
  58.                     if (i >= str.length()) return str;
  59.                 }i--;
  60.                
  61.            
  62.             }
  63.         }
  64.         return str;
  65.  
  66.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement