Accumulate code snippets for some common functions. Continuous update

Source: Internet
Author: User
Tags lowercase

1. Gets the position of the first occurrence of a character segment in a string: for example, take the position of the third '/' in the URL and output the string after that position (including the position):

@Test public
	void Tetssss ()
	{
		System.out.print (testss ("http://localhost:8080/ylitsm/androidpublic/ Alogin.do? ","/", 3));
	}
	
	/**
	 * 
	 *author:cwy
	 * Description:
	 * Parameters:
	 * @param string----------string
	 * @param sub------------substring
	 * @param index----------number (starting from 1)
	 * @return *
	/Public  string Testss (String string,string Sub,int index)
	{/
		 /here is the location to get the "/" symbol
	    Matcher Slashmatcher = Pattern.compile (sub). Matcher (string);
	    int midx = 0;
	    while (Slashmatcher.find ()) {
	       midx++;
	       When the "/" symbol appears for the third time
	       if (midx = = index) {break
	          ;
	       }
	    }
	  Return string.substring (Slashmatcher.start ()) + "";
	}


2. Based on two IP, start IP, end IP, calculate the IP that exists between them

/** * @author cwy * @date 2017-1-4 PM 8:54:22 * @version Version number * @TODO Description: Calculates the address between the two according to the IP start address and end address * ipfrom------- --Start IP * ipto-----------End IP */public static object[] Get_ip_arr (String ipfrom, String ipto) {LIST<STRING&G T
	    ips = new arraylist<string> ();
	    string[] ipfromd = ipfrom.split ("\ \");
	    string[] Iptod = ipto.split ("\ \");
	    int[] Int_ipf = new Int[4];
	    int[] Int_ipt = new Int[4];
	        for (int i = 0; i < 4; i++) {Int_ipf[i] = Integer.parseint (Ipfromd[i]);
	    Int_ipt[i] = Integer.parseint (Iptod[i]); } for (int A = int_ipf[0]; A <= int_ipt[0]; a++) {for (int B = (A = = Int_ipf[0]? Int_ipf[1]: 0); B <= (A = = Int_ipt[0]? int_ipt[1]: 255); b++) {for (int C = (B = = Int_ipf[1]? Int_ipf[2]: 0); C <= (B = = Int_ipt[1]? int_ipt[2]: 255); C + +) {for (int D = (C = = int_ipf[2]? Int_ipf[3]: 0); D <= (C = = int_ipt[2]? int_IPT[3]: 255);
	                d++) {Ips.add (new String (A +). "+ B +". "+ C +". "+ D));
	}}}} return Ips.toarray ();
	    	} public static void Main (string[] args) {object[] Str=iputil.get_ip_arr ("192.168.1.1", "192.168.1.255");
	    	for (int i=0;i<str.length;i++) {System.out.println (str[i].tostring ()); }
	    }


3. When data is taken from the database, remove the underlined field and capitalize the first letter after the underscore. Commons.lang3 Bag is very useful

1. Start all lowercase characters, 2, change

String property = Convertproperty (Stringutils.lowercase (ColumnLabel)); Attribute name (lowercase);
/**
	 * Format database field, remove underscore and immediately capitalize, convert to object property
	 * 
	 * @param srcstr
	 * @return
	*/ private static string Convertproperty (String srcstr) {
		string property = "";
		if (Srcstr.indexof ("_") >-1) {//Presence underscore
			StringBuilder sb = new StringBuilder ();

			string[] STRs = Srcstr.split ("_");
			Sb.append (Strs[0]);
			for (int i = 1; i < strs.length; i++) {
				sb.append (stringutils.capitalize (strs[i]));//First Letter Capital
			} Property
			= Sb.tostring ();
		} else {Property
			= Srcstr;
		}
		return property;
	}

4. Generate the first uppercase set, or get method:

	/**
	 * Capitalize and generate Setxx method
	 * 
	 * @param srcstr
	 * @return */
	private static String Setfirstcharactertoupper (String srcstr) {
		string setmethod = "Set" + stringutils.capitalize (SRCSTR);//first Letter uppercase
		return setmethod;
	}

5. Determine if the request is from a mobile phone or web page

	/** 
	 * @author cwy 
	 * @date 2017-1-13 morning 9:36:54
	 * @version version number
	 * @TODO Description: Determine whether the device belongs to a mobile phone or a webpage
	
	
	*/ public static Boolean  IsMobileDevice (String requestheader) {
		/**
		 * android: All Android devices
		 * Mac OS: iphone ipad
		 * Windows Phone:nokia and other Windows system phone
		 */
		string[] Devicearray = new string[]{"Android", "Mac OS "," Windows Phone "};
		if (Requestheader = = null)
			return false;
		Requestheader = Requestheader.tolowercase ();
		for (int i=0;i<devicearray.length;i++) {
			if (Requestheader.indexof (Devicearray[i]) >0) {
				return true;
			}
		}
		return false;
}


6. Generate random numbers by digit, very useful when generating work orders

/**
	 * Generates a string in bits
	 * 
	 * @param charcount
	 *            @return *
	* * Number * *, and public static string Getrandnum (int charcount) {
		String charvalue = "";
		for (int i = 0; i < charcount; i++) {
			char c = (char) (randomint () + ' 0 ');
			Charvalue + = string.valueof (c);
		}
		return charvalue;
	}

	/**
	 * Randomly generated 0~9 between numbers
	 *
	/private static int randomint () {
		random r = new Random ();
		int num = R.nextint (10); Randomly generated 0~9 between digits
		return num;
	}

7. Read the Properties File Tool class:

public class Propertiesutil {

	private static Logger Logger = Logmanager.getlogger (propertiesutil.class);

	private static Properties prop = null;

	Private Propertiesutil () {
	}

	static {
		prop = new Properties ();
		InputStream in = PropertiesUtil.class.getResourceAsStream ("/properties/prop.properties"); Read the file from the root
		try {
			prop.load (in);
		} catch (IOException e) {
			logger.debug ("Initialize Properties file Error ...");
			e.printstacktrace ();
		}
	}

	/**
	 * Gets the value
	 * 
	 * @param key
	 * @return */public
	static String get (String key) {
		string valstring = Prop.getproperty (Key, "");		logger.debug ("key:" + key + "      value:" + valstring);
		return valstring;
	}

	/**
	 * 
	 * Modify key corresponding value value in memory *
	 /public
	static void Setproper (string key, String value) {
		Prop.setproperty (key, value);
	}

}

8. Two-d array deletes the column with the specified name:

 /**  * * @param org---arrays * @param name---Specify the name of the column you want to delete * @return returns the subscript to delete the column----------starting with 0, 1 means the column you want to delete is not found */public static int Getcol (String org[][],string name) {for (int i=0;i<org[0].length;i++) {if (Org[0][i].equa
				LS (name)) {return i;
		}} return-1; }/** * * @param org---data source * @param name-----The names of the columns to be deleted * @return two-dimensional array after the column is deleted */public static String
			[[] Delcol (string org[][],string name) {string Target[][]=new string[org.length][org[0].length-1];
			int Lie=getcol (org,name);
			if (lie==-1) {return org; } else {for (; lie<org[0].length-1;lie++)//Delete column, move the data to the right of the column to the left {for (int i=0;i<org.length   
					; i++) {org[i][lie]=org[i][lie+1]; }}} for (int k=0;k<org.length;k++)//Assign a value to the target array {for (int a=0;a<org[0].length-1
				; a++) {target[k][a]=org[k][a];
		}} return target; }


9. Delete the two-dimensional array to specify the row:

		/**
		 * 
		 * @param org------data source
		 * @param name-------Specify the line name
		 * @return *
		 /public
		static int GetRow ( String org[][],string name)      
		{for
			(int i=0;i<org.length;i++)
			{
				if (org[i][0].equals (name))
				{
					return i;
				}
			}
			return-1;
		}

public static  string[][] Delrow (string org[][],string name)
		{
			string target[][]=new string[ Org.length-1][org[0].length];
			int Hang=getrow (org,name);
			if (hang==-1)
			{
				return org;
			}
			else
			{for
				(; hang<org.length-1;hang++)              //Delete column, move the data below the row
				{for
					(int i=0;i<org[0]. length;i++)
					{
						
							org[hang][i]=org[hang+1][i];}}
			}
			
			for (int k=0;k<org.length-1;k++)            //assignment to the target array
			{for
				
				(int a=0;a<org[0].length;a++)
				{
					Target[k][a]=org[k][a];
				}
			}
			return target;
		}
		










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.