⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringutil.java

📁 这是一个基于java编写的torrent的P2P源码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
	 * @param start Index of the first element to copy
	 */
	public String[] copyFrom( String[] from, int start )
	{
		if ( from == null )
			return null ;

		return this.copyFrom( from, start, from.length - 1 ) ;
	} // copyFrom()
  
  // -------------------------------------------------------------------------

	/**
	 * Returns all elements of string array <i>from</i> in a new array
	 * from index start up to index end (inclusive).
	 * If end is larger than the last valid index, it will be reduced
	 * to the last index.
	 * If end index is less than start index, an empty array will be
	 * returned.
	 * 
	 * @param from The string array the elements should be copied from
	 * @param start Index of the first element to copy
	 * @param end Index of last element to be copied
	 */
	public String[] copyFrom( String[] from, int start, int end )
	{
		String[] result ;
		int count ;
		int stop = end ;
		
		if ( from == null )
			return null ;
		
		if ( stop > ( from.length - 1 ) )
			stop = from.length - 1 ;
		
		count = stop - start + 1 ;
			
		if ( count < 1 )
			return new String[0] ;	
			
		result = new String[count] ;
		
		System.arraycopy( from, start, result, 0, count ) ;
			
		return result ;	
	} // copyFrom()
  
  // -------------------------------------------------------------------------
  
  /**
   * Returns the portion of the given string that comes before the last
   * occurance of the specified separator.    <br>
   * If the separator could not be found in the given string, then the
   * string is returned unchanged.
   * <p>
   * Examples:
   * <p>
   * cutTail( "A/B/C", "/" ) ;   // returns "A/B"
   * <br>
   * cutTail( "A/B/C", "," ) ;   // returns "A/B/C"
   * <p>
	 * @see #prefix( String, String )
	 * @see #suffix( String, String )
	 * @see #cutHead( String, String )
	 * @see #startingFrom( String, String )
	 * @see #upTo( String, String )
   * @param text The string from which to cut off the tail
   * @param separator The separator from where to cut off
   * @return the string without the separator and without the characters after the separator
   */
	public String cutTail( String text, String separator )
	{
		int index ;
		
		if ( ( text == null ) || ( separator == null ) )
			return text ;
			
		index = text.lastIndexOf( separator ) ;
		if ( index < 0 )
			return text ;
			
		return text.substring( 0, index ) ;	
	} // cutTail()

	// ------------------------------------------------------------------------

  /**
   * Returns the portion of the given string that stands after the last
   * occurance of the specified separator.    <br>
   * If the separator could not be found in the given string, then the
   * string is returned unchanged.
   * <p>
   * Examples:
   * <p>
   * cutHead( "A/B/C", "/" ) ;   // returns "C"
   * <br>
   * cutHead( "A/B/C", "," ) ;   // returns "A/B/C"
   * <p>
	 * @see #prefix( String, String )
	 * @see #cutTail( String, String )
	 * @see #suffix( String, String )
	 * @see #startingFrom( String, String )
	 * @see #upTo( String, String )
   * @param text The string from which to cut off the head
   * @param separator The separator up to which to cut off
   * @return the string without the separator and without the characters before the separator
   */
	public String cutHead( String text, String separator )
	{
		int index ;
		
		if ( ( text == null ) || ( separator == null ) )
			return text ;
			
		index = text.lastIndexOf( separator ) ;
		if ( index < 0 )
			return text ;
			
		return text.substring( index + 1 ) ;	
	} // cutHead()

	// ------------------------------------------------------------------------

	/**
	 * Returns a string array with two elements where the first is the attribute
	 * name and the second is the attribute value.
	 * Splits the given string at the first occurance of separator and returns
	 * the piece before the separator in element 0 and the piece after the 
	 * separator in the returned array.
	 * If the separator is not found, the first element contains the full
	 * string and the second an empty string.
	 * 
	 * @param str The string that contains the name-value pair
	 * @param separator The separator between name and value
	 */
	public String[] splitNameValue( String str, String separator )
	{
		String[] result = { "", "" } ;
		int index	;
		
		if ( str != null )
		{
			index = str.indexOf( separator ) ;
			if ( index > 0 )
			{
				result[0] = str.substring( 0, index ) ;
				result[1] = str.substring( index + separator.length() ) ;
			}
			else
			{
				result[0] = str ;
			}
		}
		
		return result ;
	} // splitNameValue()

  // -------------------------------------------------------------------------

	/**
	 * Returns the substring of the given string that comes before the
	 * first occurance of the specified separator.
	 * If the string starts with a separator, the result will be an empty string.
	 * If the string doesn't contain the separator the method returns null.
   * <p>
   * Examples:
   * <p>
   * prefix( "A/B/C", "/" ) ;   // returns "A"
   * <br>
   * prefix( "A/B/C", "," ) ;   // returns null
   * <p>
	 * @see #suffix( String, String )
	 * @see #cutTail( String, String )
	 * @see #cutHead( String, String )
	 * @see #startingFrom( String, String )
	 * @see #upTo( String, String )
	 * @param str The string of which the prefix is desired
	 * @param separator Separates the prefix from the rest of the string
	 */
	public String prefix( String str, String separator )
	{
		return this.prefix( str, separator, true ) ;
	} // prefix()

  // -------------------------------------------------------------------------

	/**
	 * Returns the substring of the given string that comes after the
	 * first occurance of the specified separator.
	 * If the string ends with a separator, the result will be an empty string.
	 * If the string doesn't contain the separator the method returns null.
   * <p>
   * Examples:
   * <p>
   * suffix( "A/B/C", "/" ) ;   // returns "B/C"
   * <br>
   * suffix( "A/B/C", "," ) ;   // returns null
   * <p>
	 * @see #prefix( String, String )
	 * @see #cutTail( String, String )
	 * @see #cutHead( String, String )
	 * @see #startingFrom( String, String )
	 * @see #upTo( String, String )
	 * @param str The string of which the suffix is desired
	 * @param separator Separates the suffix from the rest of the string
	 */
	public String suffix( String str, String separator )
	{
		return this.suffix( str, separator, true ) ;
	} // suffix()

  // -------------------------------------------------------------------------

	/**
	 * Returns the substring of the given string that comes before the
	 * first occurance of the specified separator.
	 * If the string starts with a separator, the result will be an empty string.
	 * If the string doesn't contain the separator the method returns the
	 * whole string unchanged.
   * <p>
   * Examples:
   * <p>
   * upTo( "A/B/C", "/" ) ;   // returns "A"
   * <br>
   * upTo( "A/B/C", "," ) ;   // returns "A/B/C"
   * <br>
   * upTo( "/A/B/C", "/" ) ;   // returns ""
   * <p>
	 * @see #prefix( String, String )
	 * @see #cutTail( String, String )
	 * @see #cutHead( String, String )
	 * @see #startingFrom( String, String )
	 * @see #suffix( String, String )
	 * @param str The string of which the prefix is desired
	 * @param separator Separates the prefix from the rest of the string
	 */
	public String upTo( String str, String separator )
	{
		return this.prefix( str, separator, false ) ;
	} // upTo()

  // -------------------------------------------------------------------------

	/**
	 * Returns the substring of the given string that comes after the
	 * first occurance of the specified separator.
	 * If the string doesn't contain the separator the method returns the
	 * whole string unchanged.
   * <p>
   * Examples:
   * <p>
   * startingFrom( "A/B/C", "/" ) ;   // returns "B/C"
   * <br>
   * startingFrom( "A/B/C", "," ) ;   // returns "A/B/C"
   * <p>
	 * @see #prefix( String, String )
	 * @see #cutTail( String, String )
	 * @see #cutHead( String, String )
	 * @see #suffix( String, String )
	 * @see #upTo( String, String )
	 * @param str The string of which the suffix is desired
	 * @param separator Separates the suffix from the rest of the string
	 */
	public String startingFrom( String str, String separator )
	{
		return this.suffix( str, separator, false ) ;
	} // startingFrom()

  // -------------------------------------------------------------------------

	/**
	 * Returns a string that contains all characters of the given string in
	 * reverse order.
	 */
	public String reverse( String str )
	{
		if ( str == null )
			return null ;
			
		char[] newStr = new char[str.length()] ;
		StringCharacterIterator iterator = new StringCharacterIterator(str) ;
		int i = 0 ;
				
		for(char ch = iterator.last(); ch != CharacterIterator.DONE; ch = iterator.previous())
		{
			newStr[i] = ch ;
			i++ ;
		}
		return new String( newStr ) ;	
	} // reverse()

	// -------------------------------------------------------------------------

	/**
	 * Returns the given map with new entries from the specified String.
	 * If the specified map is null a new empty java.util.Hashtable will be 
	 * created.
	 * <br>
	 * The string is split up into elements separated by the elementSeparator
	 * parameter. If this parameter is null the default separator "," is used.
	 * <br>
	 * After that each part is split up to a key-value pair separated by the
	 * keyValueSeparator parameter. If this parameter is null the default "=" is
	 * used.
	 * <br>
	 * Then the key-value pairs are added to the map and the map is returned.
	 * <p>
	 * <b>Be aware that all leading and trailing whitespaces of keys and values
	 * will be removed!</b>
	 * 
	 * @param str The string that contains the list of key-value pairs
	 * @param elementSeparator The separator between the elements of the list
	 * @param keyValueSeparator The separator between the keys and values
	 * @param map The map to which the key-value pairs are added
	 */
	public Map toMap(	String str, String elementSeparator, 
										String keyValueSeparator, Map map )
	{
		Map result ;
		String elemSep ;
		String kvSep ;
		String[] assignments ;
		String[] nameValue ;
		
		if ( str == null )
			return map ;
			
		result = ( map == null ? new Hashtable() : map ) ;
		elemSep = ( elementSeparator == null ) ? "," : elementSeparator ;
		kvSep = ( keyValueSeparator == null ) ? "=" : keyValueSeparator ;
		
		assignments = this.parts( str, elemSep ) ;
		for ( int i = 0 ; i < assignments.length ; i++ )
		{
			nameValue = this.splitNameValue( assignments[i], kvSep ) ;
			nameValue[0] = nameValue[0].trim() ;
			nameValue[1] = nameValue[1].trim() ;
			if ( nameValue[0].length() > 0 )
				result.put( nameValue[0], nameValue[1] ) ;
		}
		
		return result ;
	} // asMap()

	// -------------------------------------------------------------------------
	
	/**
	 * Returns a new map object that contains all key-value pairs of the
	 * specified string. 
	 * <br>
	 * The separator between the elements is assumed to be ","
	 * and "=" between key and value.
	 * <p>
	 * Example:<br>
	 * "main=Fred,support1=John,support2=Stella,manager=Oscar"
	 * <p>
	 * <b>Be aware that all leading and trailing whitespaces of keys and values
	 * will be removed!</b>
	 * 
	 * @param str The string with the list of key-value pairs
	 */
	public Map asMap( String str )
	{
		return this.toMap( str, null, null, null ) ;
	} // asMap()

	// -------------------------------------------------------------------------

	/**
	 * Returns a new map object that contains all key-value pairs of the
	 * specified string. 
	 * <br>
	 * The separator between the keys and values is assumed to be "=".
	 * <p>
	 * <b>Be aware that all leading and trailing whitespaces of keys and values
	 * will be removed!</b>
	 * 
	 * @param str The string that contains the list of key-value pairs
	 * @param elementSeparator The separator between the elements of the list
	 */	
	public Map asMap( String str, String elementSeparator )
	{
		return this.toMap( str, elementSeparator, null, null ) ;		
	} // asMap()

	// -------------------------------------------------------------------------	
	
	/**
	 * Returns a new map object that contains all key-value pairs of the
	 * specified string. 
	 * <p>
	 * <b>Be aware that all leading and trailing whitespaces of keys and values
	 * will be removed!</b>
	 * 
	 * @param str The string that contains the list of key-value pairs

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -