一、String类
- String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
- 字符串是一个特殊的对象
- 字符串是常量,一旦初始化就不可以被改变
- String str = “abc”;和String str1 = new String(“abc”);的区别在于:
- str==str1----false str.equals(str1)----true
- str在内存中有一个对象“abc”
- str1在内存中有两对象“abc”和new String
- String类重写了Object类中的equals方法,该方法用于判断字符串内容是否相等
- “abc”是一个常量,存放于方法区的常量池
二、常用方法
/** * */ public final class String implements java.io.Serializable, Comparable<String>, CharSequence { private final char value[];//字符数组 private final int offset; private final int count;//length private int hash; private static final long serialVersionUID = -6849794470754667710L; //构造函数 //初始化一个新创建的 String 对象,使其表示一个空字符序列。 public String() { this.offset = 0; this.count = 0; this.value = new char[0]; } //初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列;换句话说,新创建的字符串是该参数字符串的副本。 public String(String original) { int size = original.count; char[] originalValue = original.value; char[] v; if (originalValue.length > size) { int off = original.offset; v = Arrays.copyOfRange(originalValue, off, off+size); } else { v = originalValue; } this.offset = 0; this.count = size; this.value = v; } //分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。 public String(char value[]) { this.offset = 0; this.count = value.length; this.value = StringValue.from(value); } //分配一个新的 String,它包含取自字符数组参数一个子数组的字符。 public String(char value[], int offset, int count) { if (offset < 0) { throw new StringIndexOutOfBoundsException(offset); } if (count < 0) { throw new StringIndexOutOfBoundsException(count); } if (offset > value.length - count) { throw new StringIndexOutOfBoundsException(offset + count); } this.offset = 0; this.count = count; this.value = Arrays.copyOfRange(value, offset, offset+count); } //通过使用指定的字符集解码指定的 byte 子数组,构造一个新的 String。 public String(byte bytes[], int offset, int length, String charsetName)throws UnsupportedEncodingException { if (charsetName == null) throw new NullPointerException("charsetName"); checkBounds(bytes, offset, length); char[] v = StringCoding.decode(charsetName, bytes, offset, length); this.offset = 0; this.count = v.length; this.value = v; } //通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。 public String(byte bytes[], String charsetName)throws UnsupportedEncodingException { this(bytes, 0, bytes.length, charsetName); } public String(byte bytes[], int offset, int length) { } public String(byte bytes[]) { this(bytes, 0, bytes.length); } public String(StringBuffer buffer) { String result = buffer.toString(); this.value = result.value; this.count = result.count; this.offset = result.offset; } public String(StringBuilder builder) { String result = builder.toString(); this.value = result.value; this.count = result.count; this.offset = result.offset; } //获取方法************************************ //1,返回此字符串的长度 public int length() { return count; } //2,返回指定索引处的 char 值 public char charAt(int index) { if ((index < 0) || (index >= count)) { throw new StringIndexOutOfBoundsException(index); } return value[index + offset]; } //3,返回指定字符在此字符串中第一次出现处的索引。 public int indexOf(int ch) { return indexOf(ch, 0); } //4,返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索 public int indexOf(int ch, int fromIndex) { //.... } //5,返回指定子字符串在此字符串中第一次出现处的索引 public int indexOf(String str) //6,返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始 public int indexOf(String str, int fromIndex) //7,返回指定字符在此字符串中最后一次出现处的索引。 public int lastIndexOf(int ch) //8,返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索。 public int lastIndexOf(int ch, int fromIndex) //9,返回指定子字符串在此字符串中最右边出现处的索引 public int lastIndexOf(String str) //10,返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。 public int lastIndexOf(String str, int fromIndex) /** *所有查找字符或字符串位置的如果没有,则返回-1 *charAt函数中的index如果超过字符串的长度则会出现字符串角标越界异常IndexOutOfBoundsException是RuntimeException的子类 */ }
{ //判断方法***************************************** //测试此字符串是否以指定的前缀开始。 public boolean startsWith(String prefix) //测试此字符串从指定索引开始的子字符串是否以指定前缀开始。 public boolean startsWith(String prefix, int toffset) //测试此字符串是否以指定的后缀结束。 public boolean endsWith(String suffix) //当且仅当 length() 为 0 时返回 true。 即可以判断字符串是否为空 public boolean isEmpty() //当且仅当此字符串包含指定的 char 值序列时,返回 true。 即可以判断字符串中是否包含指定字符串,也可以用indexOf(String str)来判断 //接口 java.lang.CharSequence //所有已知子接口:Name //所有已知实现类:CharBuffer, Segment, String, StringBuffer, StringBuilder public boolean contains(CharSequence s) //判断两个字符串内容是否相同 public boolean equals(Object anObject) //将此 String 与另一个 String 比较,不考虑大小写 public boolean equalsIgnoreCase(String anotherString) }
{ //转换方法*********** //1,将字符数组,转换成字符串 public String(char[] value) 分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。该字符数组的内容已被复制;后续对字符数组的修改不会影响新创建的字符串-----将字符数组,转换成字符串 public String(char[] value, int offset,int count) //2,将字符数组的一部分,转换成字符串offset是起始位置,count是长度 public static String copyValueOf(char[] data) //3,返回指定数组中表示该字符序列的 String。 public static String copyValueOf(char[] data, int offset, int count) //4,返回指定数组中表示该字符序列的 String public static String valueOf(char[] data,int offset, int count) public static String valueOf(char[] data) //5,将字符串转换成字符数组 public char[] toCharArray() //6,将字节数组转换成字符串 public String(byte[] bytes) public String(byte[] bytes, int offset,int length) //7,将字符串转换成字节数组,使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中 public byte[] getBytes() //8,使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 public byte[] getBytes(String charsetName) //9,将基本数据类型转换成字符串 public static String valueOf(int i)。。。。。 注意:字符串和字节数组在转换过程中,是可以指定编码表的 }
{ //替换方法********************* //返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 得到的。 public String replace(char oldChar,char newChar) //用新字符串替换原有字符串,如果要替换的部分不存在就返回原字符串 public String replace(CharSequence target,CharSequence replacement) //使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串 public String replaceAll(String regex, String replacement) }
//切割方法******************** //根据给定规则的匹配拆分此字符串。 public String[] split(String regex)
//获取字符串中子串******************* //返回一个新的字符串,它是此字符串的一个子字符串。该子字符串从指定索引处的字符开始,直到此字符串末尾。 public String substring(int beginIndex) //包含头不包含尾 public String substring(int beginIndex,int endIndex)
//转换,去除空格,比较******************* //1,将字符串转换成大写小写 public String toLowerCase()//转成小写 public String toUpperCase()//转成大写 //2,将字符串两端多余的空格去除 public String trim() //3,对两个字符串进行自然顺序的比较 public int compareTo(String anotherString) 大于就返回正数 小于返回负数 等于返回0 //比较时不考虑大小写 public int compareToIgnoreCase(String str)
相关推荐
创建String字符串 目录 课程导入 掌握String字符串对象的创建 掌握字符串对象的输入与输出 一般程序需要处理大量文本数据Java语言的文本数据被保存为字符或字符串类型。 若干个字符在计算机里面如何存储? 如何引用...
java中String类型转换方法.pdf
< java.lang >String字符串:★★★☆ 71 < java.lang >StringBuffer字符串缓冲区:★★★☆ 73 < java.lang >StringBuilder字符串缓冲区:★★★☆ 74 基本数据类型对象包装类★★★☆ 75 集合框架:★★★★★,...
字符串处理例子一些相关的java代码 在Java中,字符串是一个对象,不是字符的数组,这和其他编程语言不一样。java.lang.String定义了字符串类型。
客户要求,ID列的数据类型必须是uniqueidentifier,一开始实体类的ID设计成java.lang.String类型;映射文件中ID的增长方式是uuid.hex private java.lang.String id; public java.lang.String getId(){ return id; }...
字符串哈希码 字符串的其他实用程序。安装npm install string-hashcode 例子var hashCode = require ( 'string-hashcode' ) ;var s = 'abc' ;console . log ( s . hashCode ) ; // undefinedvar code = hashCode ( s...
前言 由于在测试一些用例时,手残使系统导入了...该类与Java lang String是存在冲突的。 可以看到其中的xpath,它是用于解析和处理XML内容的语言,它具有一些简单的功能,当出现数字或者数据转换时,String作为一
在 Java 中,字符串类型对应的是 java.lang.String 类型。在 MySql 中,字符串类型对应的是 VARCHAR、NVARCHAR、CHAR、NCHAR 等类型。其中,VARCHAR 类型用于存储可变长度的字符串,NVARCHAR 类型用于存储 Unicode ...
Java-String类的常用方法总结,String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。java把String类声明的final类,不能有类。String类对象创建后不能修改,由0或多个字符组成,包含...
文章目录字符串一、String 类1.1 声明字符串1.2 创建字符串二、连接字符串2.1 连接多个字符串2.2 连接其他数据类型三、获取字符串信息3.1 获取字符串长度3.2 字符串查找3.3 获取指定索引位置的字符四、字符串操作4.1...
主要介绍了Java中分割字符串的两种方法,一种是java.lang.String 的 split() 方法,,另外一种是用String Tokenizer类。文中的每种方法都给出了详细的示例代码,相信对大家的理解和学习具有一定的参考借鉴价值,有...
第一章 String类概述java.lang.String 类代表字符串。Java程序中所有的字符串文字(例如 "abc" )都可以被看作是实现此类的实例。类
String 类是 Java 语言中最重要的类之一,它用于处理字符串。String 类提供了许多有用的方法,例如 concat() 方法可以连接两个字符串,substring() 方法可以获取子字符串,indexOf() 方法可以搜索字符串,replace() ...
* 将字符串中的中文转化为拼音,其他字符不变 * * @param inputString * @return */ public static String getPingYin(String inputString) { HanyuPinyinOutputFormat format = new ...
除了6个Exception类和2个已经deprecated的数字类之外,commons.lang包共包含了17个实用的类: ArrayUtils – 用于对数组的操作,如添加、查找、删除、子数组、倒序、元素类型转换等; BitField – 用于操作位元,...
rules.put("java.lang.String", "VARCHAR"); rules.put("java.lang.Long", "DECIMAL"); rules.put("java.lang.Integer", "DECIMAL"); rules.put("java.math.BigDecimal", "DECIMAL"); rules.put("java.util.Date", ...
1、字符串String类存放与 java.lang 包中,因为java.lang包是系统默认引入的,所以使用该包时,不用再通过 import 关键字进行引入了; 2、String 类是 final 类型的类,所以 String 类不能被其他类继承,即没有子类...
35、如何把一段逗号分割的字符串转换成一个数组? ............................................... 25 36、数组有没有 length()这个方法? String 有没有 length()这个方法? .................... 25 37、下面这...
String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。String类对象创建后是一个常量,有final修饰,不能被修改。字符串重新赋值不是修改,而是生成了一个新的字符串,原先的字符串...
它可以使用tostring()转换成string字符串。 String x=”a” 4 ”c”编译时等效于String x=new StringBuffer().append(“a”).append(4).append(“c”).toString(); 字符串常量是一种特殊的匿名对象,String s1=...