`
jinheking
  • 浏览: 77818 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

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";
}
分享到:
评论
7 楼 lpp333 2007-04-07  
传说中的D语言。。。。。。。。。。。
6 楼 jinheking 2007-04-05  

我的习惯要改了

其实我喜欢使用Eclipse,自动排序

现在D语言使用UltraEdit没有这个功能郁闷
5 楼 qiezi 2007-04-05  
你代码里tab和空格混合使用了,改成一样就行了。
4 楼 jinheking 2007-04-05  
我的代码写的不乱在这里显得乱
到这里下载
www.caoqi.com/d/String.d

bool是我认为返回的是bool值,但是不知道为什么编译的时候,编译器告诉我应该是int值
3 楼 jinheking 2007-04-05  
不好意思,我修改了,但是这里没有改

我现在改一下!
2 楼 qiezi 2007-04-05  
代码对齐得怎么这么乱呢。。。还有文档中有错误,opCmp返回int,文档中标注的是bool......
1 楼 jinheking 2007-04-05  

相关推荐

    android折线图

    021 import android.view.View; 022 023 /** 024 * 025 * @author zhangyayun1@gmail.com 026 * 027 */ 028 @SuppressLint("ViewConstructor") 029 class MyChartView extends View{ 030...

    算法ppt.zip

    5. **算法分析(Analysis of Algorithms)**:021_14AnalysisOfAlgorithms.pdf 阐述了如何分析算法的时间复杂度和空间复杂度,这对于理解算法效率和优化至关重要。基本概念如渐进复杂度(O、Ω、Θ记号)会在其中讲解...

    C++语言期终2005 含答案.pdf

    6. C++中的数字前缀0表示八进制,所以`012`是八进制数,相当于十进制的10,加上`021`(八进制的17),结果是27,因此`cout&lt;&lt;012+021;`显示的结果是27。 7. `return 10&&-10;`中的`&&`是逻辑与运算符,它在两边都为...

    C++语言期终2005 含答案.docx

    5. **成员函数原型**:`setname`函数的原型可能是`void setname(string name)`,假设`name`是一个`string`类型的参数。 6. **算术运算与输出**:`cout&lt;&lt;012+021;`中,前两个数字会被当作八进制数,所以结果是十进制...

    MASM汇编编辑器 好用 简单

    0021 CD 21 * int 021h END Microsoft (R) Macro Assembler Version 6.11 08/10/05 14:09:13 li6-1.asm Symbols 2 - 1 Segments and Groups: N a m e Size Length Align Combine Class DGROUP . . . . . ...

    JavaScript中把数字转换为字符串的程序代码

    1:把数字转换为字符串的方法 代码如下: var string_value = String... //Evaluates to “10001” octal_string = “0” + n.toString(8); //Evaluates to “021” hex_string = “0x” + n.toString(16); 

    android手机开发利器B4a最新库文件(截止到20170424)

    Basic4Android最新最全类库,包含B4a上市后截止到2017.04.24的库文件。希望对大家有帮助。以下为文件列表。 2012/08/09 周四 01:55 35,901 ABExtDrawing.jar 2012/08/09 周四 01:55 191,079 ABExtDrawing.xml ...

    language18-22casesofactual.rar_21 | 19

    可能包括C++中的`std::string`类,Java的`String`类,或Python的内置字符串支持。学习者会了解到如何在程序中有效地管理和操作文本数据。 实例19:计算各点到源点的最短距离 这涉及到图论中的最短路径问题,很可能...

    常用正则表达式(总结归类)

    **解释**: 该表达式用来验证电话号码是否符合常见格式,例如0511-4405222或021-87888822。 #### QQ号码验证 ##### 1. 验证QQ号码 **正则表达式**: `[1-9][0-9]{4,}` **解释**: 这个表达式用来验证QQ号码是否有效。...

    JavaScript数字和字符串转换示例

    var octal_string = "0" + n.toString(8); // "021" var hex_string = "0x" + n.toString(16); // "0x11" ``` d. **使用`toFixed()`方法**:此方法用于格式化数字,保留小数点后指定的位数,不使用科学计数法。...

    算法exercise021

    - 使用`string.atoi(line)`将字符串转换为整数,`&gt;= 0`检查是否为非负数,返回非负整数或0。 4. **文件操作**: - `file_action(pathname)`函数用于读取文件中的数字并累加到全局变量`result`,如果路径不是目录...

    Linux-0.11 [内核源代码带中文注释]

    因为7*4 + 0 = 28,所以 /dev/PS0 (2,28)指的是1.44M A 驱动器,其设备号是0x021c ! 同理 /dev/at0 (2,8)指的是1.2M A 驱动器,其设备号是0x0208。 seg cs mov ax,root_dev ! 将根设备号 cmp ax,#0 jne root_...

    大数据知识、面试题 HDFS、Mapreduce、hive

    2. **acks=0**:在这种配置下,生产者不会等待任何Broker的确认信息,这意味着即使数据未被成功写入,生产者也会认为写入操作已完成,这会导致数据丢失。 **2.2 Broker如何保证不丢失数据** 为了防止数据丢失,...

    windbg资料集合

    WinDbg是微软开发的免费源码级调试工具。Windbg可以用于Kernel模式调试和用户模式调试,还可以调试Dump文件。... data = 0xf886b0c0 "This String is from Device Driver !!!" ntStatus = 0 mdl = 0x00000000

    C++开发中的注意点

    void print(const std::string& str) { std::cout ; } ``` #### 十、前置自增与自减的使用 对于迭代器和模板类等复杂对象,建议使用前置自增(自减)操作符,因为它们通常提供了更高效的实现。 **示例代码**: ...

    java 正则表达式

    [0-9]+", "021-7898")); } } ``` 以上代码展示了如何使用Java的`Pattern`和`Matcher`类来编写简单的正则表达式匹配示例,包括匹配字符串、邮政编码以及电话号码等。通过这些基本示例,可以帮助初学者更好地理解...

    php curl中gzip的压缩性能测试实例分析

    未使用gzip压缩的请求平均耗时为2.1秒,即0.021秒/次;而使用gzip压缩的请求平均耗时为2.6秒,即0.026秒/次。结果表明,在本例中的千兆网环境下,不使用压缩比使用压缩每次请求快了5毫秒。由于数据在局域网内传输...

Global site tag (gtag.js) - Google Analytics