String类(三)

构造函数

  • public String(char value[], int offset, int count)

构造函数的源码

    public String(char value[], int offset, int count) {
        if (offset < 0) {
            throw new StringIndexOutOfBoundsException(offset);
        }
        if (count < 0) {
            throw new StringIndexOutOfBoundsException(count);
        }
        // Note: offset or count might be near -1>>>1.
        if (offset > value.length - count) {
            throw new StringIndexOutOfBoundsException(offset + count);
        }
        this.offset = 0;
        this.count = count;
        this.value = Arrays.copyOfRange(value, offset, offset+count);
    }

String中的value[]中的值为传入数组中offset-(offset+count)索引指向的数组。

其中 public static char[] copyOfRange(char[] original, int from, int to)代码为:

    public static char[] copyOfRange(char[] original, int from, int to) {
        int newLength = to - from;
        if (newLength < 0)
            throw new IllegalArgumentException(from + " > " + to);
        char[] copy = new char[newLength];
        System.arraycopy(original, from, copy, 0,
                         Math.min(original.length - from, newLength));
        return copy;
    }

数组copy函数。

时间: 2024-08-24 12:28:54

String类(三)的相关文章

C++ 中string类的三种模拟实现方式

1.原始版本(拷贝构造和赋值运算符重载时,需要重新开辟空间) #include <iostream> #include <string> using namespace std; class String { friend ostream& operator<<(ostream& os, const String& S); public: String(char* str = "") :_str(new char[strlen

第五周学习总结&amp;第三次实验报告(String类的应用)

第五周学习总结 1.学习了继承的相关知识点: (1) 继承的格式为class 子类 extends 父类{} (2) 继承实际上是通过子类去扩展父类的功能 (3) 一个子类只能继承一个父类,也就是说,继承只允许多层继承不能多重继承 (4) 子类不能直接访问父类中的私有属性,但是可以调用父类中的getter或者setter去访问 (5) 子类对象在实例化之前必须首先调用父类中的构造方法再调用子类自己的构造方法,子类也可以直接使用super()调用父类中的无参构造 (6) 方法的覆写即子类定义了与父

C++11之右值引用(三):使用C++11编写string类以及&ldquo;异常安全&rdquo;的=运算符

前面两节,说明了右值引用和它的作用.下面通过一个string类的编写,来说明右值引用的使用. 相对于C++98,主要是多了移动构造函数和移动赋值运算符. 先给出一个简要的声明: class String { public: String(); String(const char *s); //转化语义 String(const String &s); String(String &&s); ~String(); String &operator=(const String

Java(三)String类

一.String类初始化方法 1.初始化一个空字符串 String str=new String();//这里调用了String的无参构造方法 2.初始化一个有值的字符串 String str1="abc"; //直接用字符串常量赋值 String str2=new String("abc"); //这里调用了String的有参构造函数 二.String类的常用方法 1. 原型:int indexOf(int ch) 功能:返回指定字符在此字符串中第一次出现的索引

java String 类 基础笔记

字符串是一个特殊的对象. 字符串一旦初始化就不可以被改变. String s = "abc";//存放于字符串常量池,产生1个对象 String s1=new String("abc");//堆内存中new创建了一个String对象,产生2个对象 String类中的equals比较字符串中的内容. 常用方法: 一:获取 1.获取字符串中字符的个数(长度):length();方法. 2.根据位置获取字符:charAt(int index); 3.根据字符获取在字符串中

String类

一.概述 Sting s1 = "abc";    //s1是一个类类型变量,"abc"是一个对象. String s2 = new String("abc"); //s1 . s2 的区别: 前者在内存中有一个对象,后者在内存中有两个对象. s1.equals(s2) 为true  因为 String复写了equals方法 二.常见的功能-获取和判断 获取: 1.int length(); 获取长度 2.char chatAt(int inde

String类详解(1)

首先String是一个类. 1,实例化String类方法. 1)直接赋值:String name="haha"; 2)通过关键字:String name=new String("haha"); 2,String类的数据比较. 首先回顾一下,基础数据的比较使用"=="比较. public class StringDemo04{ public static void main(String args[]){ String str1 = "h

C++ Primer Plus 第六版 第16章 string类和标准模板库

1.string实际上是模板具体化basic_string<char> 的一个typedef,有默认参数,所以省略了初始化参数 2.size_type是一个依赖于实现的整形 string将string::npos定义为字符串的最大长度 3.string类的构造函数P656 4.对于c-风格字符串,3种输入方法:cin>>   cin.getline(),cin.get 对于string   ,2种输入方法:cin>>,getline(cin,string对象) 5.st

一大波Java来袭(四)String类、StringBuilder类、StringBuffer类对比

本文主要介绍String类.StringBuffer类.StringBuilder类的区别  : 一.概述 (一)String 字符串常量,但是它具有不可变性,就是一旦创建,对它进行的任何修改操作都会创建一个新的字符串对象. (二)StringBuffer 字符串可变量,是线程安全的,和StringBuilder类提供的方法完全相同. 区别在于StringBuffer每个方法中前面添加了"synchronized",保证其是线程安全的. (三)StringBuilder 字符串可变量,

Java中String类学习总结

java中String类的使用频率非常高,本人在学习此模块时,认为下列几点知识值得注意: 一.String是不可变对象 java.lang.String类使用了final修饰,不能被继承.Java程序中的所有字面值,即双引号括起的字符串,如"abc",都是作为String类的实例实现的.String是常量,其对象一旦构造就不能再被改变.换句话说,String对象是不可变的,每一个看起来会修改String值的方法,实际上都是创造了一个全新的String对象,以包含修改后的字符串内容.而最