[code]
//module jdk2d.lang;
import std.stdio;
/*
* The <code>String</code> class represents character strings. All
* string literals in D programs, such as <code>"abc"</code>, are
* implemented as instances of this class.
* @author Caoqi
* @version 0.001, 07/03/30
* @since JDK2D 0.01
* <p>
*/
public class String{
private final wchar[] value;
/** The offset is the first index of the storage that is used. */
private final int offset;
/** The count is the number of characters in the String. */
private final int count;
/*
* 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 = new wchar[0];
}
/*
* 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 count;
}
/**
* 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>
*
* @since JDK2D 0.01
*/
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>.
*
* @param 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);
}
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 >= 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);
}
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.
*
* @param source the characters being searched.
* @param sourceOffset offset of the source string.
* @param sourceCount count of the source string.
* @param target the characters being searched for.
* @param targetOffset offset of the target string.
* @param targetCount count of the target string.
* @param fromIndex the index to begin searching from.
*/
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;
}
}
public static void main() {
String str = new String("The quick brown fox jumped over the lazy dog.");
printf("%d\n",str.length());
printf("%d\n",str.indexOf("z"));
printf("%d\n",str.isEmpty());
}
[/code]
分享到:
相关推荐
在JDK的不同版本中,`String`类经历了一些优化和改进,尤其是在性能和内存管理方面。这里我们将对JDK 1.6、1.7和1.8中的`String`类进行对比研究,探讨其中的关键变化。 ### JDK 1.6中的String 在JDK 1.6中,`...
在JDK 11中,Java的String类引入了一些新的方法,这为开发人员提供了更丰富的操作字符串的手段。以下是对这些新方法的详细说明: 1. `String.repeat(int count)`: 这个方法允许你指定一个整数`count`,将字符串...
本文将深入探讨如何使用JDK来查看类和方法,以帮助开发者更好地理解和操作Java代码。 首先,JDK提供了一个名为`javadoc`的工具,用于生成Java源代码的API文档。通过`javadoc`,我们可以查看任何公开的类、接口、...
java jdk中string类的源码 ,了解string类的书写,定义和声明
pos文件是ProcessOn的源文件,可以导入后直接打开编辑。 内容是:不同jdk版本下对String的intern()的分析
在JDK 11中,Java的String类引入了多个新方法,极大地扩展了其功能,使得开发者在处理字符串时更加便捷。以下是对这些新方法的详细介绍: 1. **String.repeat(int)**: 这个方法允许你指定一个整数,将字符串重复...
java代码-使用java解决仿照JDK源码实现数组的增删功能的源代码 ——学习参考资料:仅用于个人学习使用!
在Java编程语言中,`Object`和`String`类是非常基础且重要的概念。这里我们将针对提供的文件内容,详细解析和扩展相关知识点。 1. **重写`equals()`方法**: 在示例代码中,尝试重写了`equals()`方法来判断两个`...
在Java的JDK中,`String`类被设计为`final`的主要原因在于效率和安全性。首先,让我们深入了解这两个方面。 1. **效率**: - **字符串池**:Java中的`String`类广泛用于存储和操作文本。为了提高性能,Java引入了...
在这个主题中,我们将重点关注三个方面:Eclipse IDE的快捷键、JDK API的理解以及String类及其相关类的使用。 首先,Eclipse作为Java开发的主要IDE之一,其丰富的快捷键可以显著提高开发效率。`Eclipse快捷键大全....
- `java`包:这是Java的核心包,包含了许多基础类,如`Object`、`String`、`System`等。`Object`是所有Java类的父类,定义了类的基本行为,如`equals()`、`hashCode()`和`toString()`。`String`类是不可变字符串的...
JDK11安装包,JDK11安装包JDK11安装包,JDK11安装包JDK11安装包,JDK11安装包JDK11安装包,JDK11安装包JDK11安装包,JDK11安装包JDK11安装包,JDK11安装包JDK11安装包,JDK11安装包JDK11安装包,JDK11安装包JDK11...
在Java编程中,活用JDK提供的类可以极大地提高开发效率和代码质量。在这个简单的Java数学计算小程序中,我们可以利用JDK内置的日期处理类`java.util.Date`以及整数处理类`java.lang.Integer`来实现功能。下面将详细...
在JDK 1.6中,Sun提供了自己的Base64编码类`sun.misc.BASE64Encoder`和解码类`sun.misc.BASE64Decoder`。但这些类并不是公开的API,而是Sun内部使用的工具类。这意味着它们并不被官方文档所支持,并且可能会随着JDK...
jdk1.8 jdk1.8 jdk1.8 jdk1.8 jdk1.8 jdk1.8 jdk1.8 jdk1.8
本次我们将主要探讨解压后的`jdk-687fd7c7986d\src\share\classes\java`目录下的核心包——lang、io、nio、util,揭示它们背后的设计理念与实现原理。 首先,我们关注的是`java.lang`包,这是Java平台的核心包,...
**JDK 1.8 API 中文 CHM** 是Java开发者的重要参考资料,它包含了Java开发工具包(JDK)1.8版本的所有公共类、接口和框架的详细文档。这个CHM(Compiled HTML Help)文件是Windows平台上的帮助文档格式,用户可以...
**Java JDK 8 使用详解** Java Development Kit (JDK) 是Java编程语言的软件开发工具包,它提供了编译、调试和运行Java程序所需的所有工具。JDK 8是Java历史上的一个重要版本,引入了许多新特性,提高了开发效率并...