String类0.021版

//module jdk2d.lang;

import std.stdio;
import std.string;

/**
* The <code>String</code> class represents character strings. All
* string literals in D programs, such as <code>String str=new String("abc");</code>,
* <br /><code>str="Hello world";</code>
* are implemented as instances of this class.<br />
* final wchar[] value; The value is the String's value.<br />
* final int offset;    The offset is the first index of the storage that is used.<br />
* final int count;     The count is the number of characters in the String.	<br />
* Authors:  Caoqi
* version: 0.021
* Date: April 5, 2007
* History: March 30,2007 V0.01
* License: BSD
* See_Also:
       My blog, <a href=" http://jinheking.iteye.com/"> http://jinheking.iteye.com/</a>
      
* <p>
*/




/// Documentation for String
public class  String{
	private:
		final wchar[] value; ///The value is the String's value.
		final int offset;    ///The offset is the first index of the storage that is used.
		final int count;     ///The count is the number of characters in the String.
		
	public:	
		/**
		 * Initializes a newly created {@code String} object so that it represents
		 * an empty character sequence.  Note that use of this constructor is
		 * unnecessary since Strings are immutable.
		 */
		this(){
			this.offset = 0;
			this.count = 0;
			this.value = null;
		}
		/**		
		* Params:wc = The initial value of the string
    * Return:
    *        String
    * <code>String str=new String("abc");</code><br />
		* <code>str="abcd";</code>
		*/
		String opAssign(wchar[] wc) {			
			this.offset = 0;
			this.count = wc.length;
			this.value = wc.dup;
			return this;
    }
		
		/**
		* <code>s1>s2</code>
		* Params: s = The initial value of the string
    * Return: int
		*/
		int opCmp(String s){
			return std.string.cmp(cast(char[])this.value, cast(char[])s.value);
		}
		/*
     * Allocates a new {@code String} so that it represents the sequence of
     * characters currently contained in the character array argument. The
     * contents of the character array are copied; subsequent modification of
     * the character array does not affect the newly created string.
     *
     * Param:  value =  The initial value of the string
     */
    public this(wchar[] value) {
			int size = value.length;
			this.offset = 0;
			this.count = size;
			this.value = value;
    }
	
		/**
     * Returns the length of this string.
     * The length is equal to the number of <a href="Character.html#unicode">Unicode
     * code units</a> in the string.
     *
     * Return:  The length of the sequence of characters represented by this
     *          object.
     */
    public int length() {
        return this.value.length;
    }

    /**
     * Returns <tt>true</tt> if, and only if, {@link #length()} is <tt>0</tt>.
     *
     * Return <tt>true</tt> if {@link #length()} is <tt>0</tt>, otherwise
     * <tt>false</tt>
     */
    public bool isEmpty() {
				return count == 0;
    }
		/**
     * Returns the index within this string of the first occurrence of the
     * specified substring. The integer returned is the smallest value
     * <i>k</i> such that:
     * <blockquote><pre>
     * this.startsWith(str, <i>k</i>)
     * </pre></blockquote>
     * is <code>true</code>.
     *
     * Params:   str =  any String.
     * Return:  if the string argument occurs as a substring within this
     *          object, then the index of the first character of the first
     *          such substring is returned; if it does not occur as a
     *          substring, <code>-1</code> is returned.
     */

    public int indexOf(String str) {
			return indexOf(str, 0);
    }
    /**
     * Returns the index within this string of the first occurrence of the
     * specified substring. The integer returned is the smallest value
     * <i>k</i> such that:
     * <blockquote><pre>
     * this.startsWith(str, <i>k</i>)
     * </pre></blockquote>
     * is <code>true</code>.
     *
     * Params:   str =  any String.
     * Return:  if the string argument occurs as a substring within this
     *          object, then the index of the first character of the first
     *          such substring is returned; if it does not occur as a
     *          substring, <code>-1</code> is returned.
     */
    public int indexOf(wchar[] str) {
			return indexOf(str, 0);
    }

    /**
     * Returns the index within this string of the first occurrence of the
     * specified substring, starting at the specified index.  The integer
     * returned is the smallest value <tt>k</tt> for which:
     * <blockquote><pre>
     *     k &gt;= Math.min(fromIndex, this.length()) && this.startsWith(str, k)
     * </pre></blockquote>
     * If no such value of <i>k</i> exists, then -1 is returned.
     *
     * Param:   str    =     the substring for which to search.
     * Param:   fromIndex  = the index from which to start the search.
     * Return:  the index within this string of the first occurrence of the
     *          specified substring, starting at the specified index.
     */
    public int indexOf(String str, int fromIndex) {
        return indexOf(value, offset, count,
                       str.value, str.offset, str.count, fromIndex);
    }
     /**
     * Returns the index within this string of the first occurrence of the
     * specified substring, starting at the specified index.  The integer
     * returned is the smallest value <tt>k</tt> for which:
     * <blockquote><pre>
     *     k &gt;= Math.min(fromIndex, this.length()) && this.startsWith(str, k)
     * </pre></blockquote>
     * If no such value of <i>k</i> exists, then -1 is returned.
     *
     * Param:   str    =     the substring for which to search.
     * Param:   fromIndex  = the index from which to start the search.
     * Return:  the index within this string of the first occurrence of the
     *          specified substring, starting at the specified index.
     */
    public int indexOf(wchar[] wstr, int fromIndex) {
    	  String str=new String(wstr);
        return indexOf(value, offset, count,
                       str.value, str.offset, str.count, fromIndex);
    }

    /**
     * Code shared by String and StringBuffer to do searches. The
     * source is the character array being searched, and the target
     * is the string being searched for.
     *
     * Params:   source =      the characters being searched.
     * Params:   sourceOffset= offset of the source string.
     * Params:   sourceCount=  count of the source string.
     * Params:   target   =    the characters being searched for.
     * Params:   targetOffset =offset of the target string.
     * Params:   targetCount = count of the target string.
     * Params:   fromIndex  =  the index to begin searching from.
     * Return:  the index within this string of the first occurrence of the
     *          specified substring, starting at the specified index.
     */
    static int indexOf(wchar[] source, int sourceOffset, int sourceCount,
                       wchar[] target, int targetOffset, int targetCount,
                       int fromIndex) {
			if (fromIndex >= sourceCount) {
		            return (targetCount == 0 ? sourceCount : -1);
			}
		    	if (fromIndex < 0) {
		    	    fromIndex = 0;
		    	}
			if (targetCount == 0) {
			    return fromIndex;
			}
		
		        wchar first  = target[targetOffset];
		        int max = sourceOffset + (sourceCount - targetCount);
		
		        for (int i = sourceOffset + fromIndex; i <= max; i++) {
		            /* Look for first character. */
		            if (source[i] != first) {
		            		i++;
		                while (i <= max && source[i] != first){
		                	i++;
		                }
		            }
		
		            /* Found first character, now look at the rest of v2 */
		            if (i <= max) {
		                int j = i + 1;
		                int end = j + targetCount - 1;
		                for (int k = targetOffset + 1; j < end && (source[j] == target[k]);j++){
		                	 k++;
		                }

		                if (j == end) {
		                    /* Found whole string. */
		                    return i - sourceOffset;
		                }
		            }
		        }
		        return -1;
		    }
	/**
	* Return:The String's value
	*/
	char[] toString(){
		return cast(char[])std.utf.toUTF8(this.value);
	}
}
/++++++++++++++++++++++++
  + Our function.
  + Example:
  + --------------------------
  +  import std.stdio;
  +
  +  void foo()
  +  {
  +      String str=new String("Hi,jdk2d String,I need you!") ;
  +      writefln(str);  /* print the string */
  +      str="Next,I calculate the str's length"
  +      writefln(str);  /* print the string */
  +      printf("%d\n",str.length());
  +  }
  + --------------------------
  +/
public static void  main() {     
   String str = new String("The quick brown fox jumped over the lazy dog.");
   String s1 = new String("abc");
   s1="abc";
   String s2=new String("ab");
   
   writef("s1=");   
   writefln(s1);
   writef("s2=");   
   writefln(s2);
   
   if(s1>s2)
   	printf("s1>s2=%.*s\n","true");
   else
   	printf("s1>s2=%.*s\n","false");
   String s3=new String("ab");
   String s4=new String("abc");
   
   writef("s3=");   
   writefln(s3);
   writef("s4=");   
   writefln(s4);
   if(s3>s4)
   	printf("s3>s4=%.*s\n","true");
   else
   	printf("s3>s4=%.*s\n","false");
      
   printf("%d\n",str.indexOf("z"));
   printf("%d\n",str.isEmpty());
   
   //String s5="s5";
}

你可能感兴趣的:(eclipse,REST,Blog,J#,D语言)