Java Tool class for random string fetching

Source: Internet
Author: User

Source: Java Random String Tool class

Source code: Http://www.zuidaima.com/share/1550463479532544.htm

Java random string of the tool class can be all numbers, characters, can also be a combination of characters and numbers of the tool class, hope to bring you help

Package Com.zuidaima.test;import Java.util.random;public class Randomutils {public static final String Allchar = "0123456 789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";p ublic static final String Letterchar =" ABCDEFGHIJKLLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ ";p ublic static final String Numberchar =" 0123456789 ";      /** * Returns a fixed-length random string (containing only uppercase and lowercase letters, numbers) * @param length random string lengths * @author www.zuidaima.com * @return Random string */public static String generatestring (int length) {stringbuffer sb = new StringBuffer (); Random random = new random (), for (int i = 0; i < length; i++) {Sb.append (Allchar.charat (Random.nextint (Allchar.length () )));} return sb.tostring ();}      /** * Returns a fixed-length random, pure-letter string (containing only uppercase and lowercase letters) * @param length random string lengths * @author www.zuidaima.com * @return Random string             */public static String generatemixstring (int length) {stringbuffer sb = new StringBuffer ();             Random random = new random (); for (int i = 0; i < Length             i++) {sb.append (Allchar.charat (Random.nextint (Letterchar.length ())));     } return sb.tostring (); }/** * Returns a fixed-length random pure capital letter string (with uppercase and lowercase letters only) * * @param length random string lengths * @return Random String */public s     Tatic String generatelowerstring (int length) {return generatemixstring (length). toLowerCase (); }/** * Returns a fixed-length random plain lowercase letter string (with only uppercase letters) * * @param length random string lengths * @author www.zuidaima.com * @re Turn random String */public static string generateupperstring (int length) {return generatemixstring (length)     . toUpperCase (); }/** * Generates a fixed length of pure 0 strings * * @param length string lengths * @return Pure 0 Strings */public static string gene             ratezerostring (int length) {stringbuffer sb = new StringBuffer ();             for (int i = 0; i < length; i++) {sb.append (' 0 ');     } return sb.tostring (); }     /** * Generates a fixed-length string based on a number that is not long enough to be preceded by 0 * * @param num number * @param fixdlenth string length * @auth             or www.zuidaima.com * @return fixed-length string */public static string tofixdlengthstring (long num, int fixdlenth) {             StringBuffer sb = new StringBuffer ();             String strnum = string.valueof (num);              if (fixdlenth-strnum.length () >= 0) {sb.append (generatezerostring (Fixdlenth-strnum.length ())); } else {throw new RuntimeException ("Convert number + num +" to a string with a length of "+ Fixdlenth +") An exception occurred!             ");             } sb.append (Strnum);     return sb.tostring (); }/** * Generates a fixed-length string based on number, length is not enough for the previous 0 * * @param num number * @param fixdlenth string length * @aut             Hor www.zuidaima.com * @return Fixed-length string */public static string tofixdlengthstring (int num, int fixdlenth) {             StringBuffer sb = new StringBuffer (); String Strnum = StRing.valueof (num);              if (fixdlenth-strnum.length () >= 0) {sb.append (generatezerostring (Fixdlenth-strnum.length ())); } else {throw new RuntimeException ("Convert number + num +" to a string with a length of "+ Fixdlenth +") An exception occurred!             ");             } sb.append (Strnum);     return sb.tostring (); /** * generates different len digits each time * @param param * @return Fixed-length number */public static int Getnotsimple (int[         ] Param,int len) {Random rand = new Random ();             for (int i = param.length; i > 1; i--) {int index = rand.nextint (i);             int tmp = Param[index];             Param[index] = param[i-1];         PARAM[I-1] = tmp;         } int result = 0;         for (int i = 0; i < len; i++) {result = result * + Param[i];    } return result; } public static void Main (string[] args) {System.out.println (GeneratestriNg (10));             System.out.println (generatemixstring (10));             System.out.println (generatelowerstring (10));             System.out.println (generateupperstring (10));             System.out.println (generatezerostring (10));             System.out.println (tofixdlengthstring (123, 10));             System.out.println (Tofixdlengthstring (123L, 10));            Int[] in = {1,2,3,4,5,6,7};    System.out.println (Getnotsimple (in,3)); } }

Java Tool class for random string fetching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.