创建字符串
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>字符串广泛应用在Java编程中,在Java中字符串属于对象java随机生成字符串创建字符串,Java提供了类来创建和操作字符串。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>创建字符串
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>创建字符串最简单的方式如下:
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> = “Hello world!”;
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>在代码中遇到字符串常量时,这里的值是”Hello world!”,编译器会使用该值创建一个对象。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>和其它对象一样,可以使用关键字和构造方法来创建对象。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>类有11种构造方法,这些方法提供不同的参数来初始化字符串,比如提供一个字符数组参数:
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> class {
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> void main( args){
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>char = { ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘.’};
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> = new ();
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>.out.( );
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>}}
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>以上实例编译运行结果如下:
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>hello.
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>注意:类是不可改变的,所以你一旦创建了对象,那它的值就无法改变了。 如果需要对字符串做很多修改,那么应该选择使用 & 类。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>22~tplv-tt-origin-web:gif.jpeg?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1785333919&x-signature=MTJJ1RKtTgjKE%2FWK7zq%2FeuIADEo%3D” alt=”java随机生成字符串”/ style=”max-width: 100%; height: auto; display: block; margin: 1.6em auto; border-radius: 6px; border: 1px solid #f0abfc;”>
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>字符串长度
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>用于获取有关对象的信息的方法称为访问器方法。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>类的一个访问器方法是()方法,它返回字符串对象包含的字符数。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>下面的代码执行后,len变量等于14:
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> class {
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> void main( args) {
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> site = “”;
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>int len = site.();
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>.out.( “菜鸟教程网址长度 : ” + len );
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>}}
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>以上实例编译运行结果如下:
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>菜鸟教程网址长度 : 14
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>连接字符串
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>类提供了连接两个字符串的方法:
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>.();
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>返回连接的新字符串。也可以对字符串常量使用()方法,如:
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>”My name is “.(“”);
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>更常用的是使用’+’操作符来连接字符串,如:
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>”Hello,” + ” world” + “!”
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>结果如下:
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>”Hello, world!”
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>下面是一个例子:
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> class {
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> void main( args) {
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> = “菜鸟教程网址:”;
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>.out.(“1、” + + “”);
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>}}
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>以上实例编译运行结果如下:
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>1、菜鸟教程网址:
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>创建格式化字符串
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>253&fmt=JPEG&app=138&f=JPEG?w=889&h=500″ alt=”java随机生成字符串”/ style=”max-width: 100%; height: auto; display: block; margin: 1.6em auto; border-radius: 6px; border: 1px solid #f0abfc;”>
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>我们知道输出格式化数字可以使用()和()方法。类使用静态方法()返回一个对象而不是对象。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>类的静态方法()能用来创建可复用的格式化字符串,而不仅仅是用于一次打印输出。如下所示:
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>.out.(“浮点型变量的的值为 ” +
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>”%f, 整型变量的值为 ” +
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>” %d, 字符串变量的值为 ” +
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>”is %s”, , , );
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>你也可以这样写
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> fs;fs = .(“浮点型变量的的值为 ” +
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>”%f, 整型变量的值为 ” +
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>” %d, 字符串变量的值为 ” +
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>” %s”, , , );.out.(fs);
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> 方法
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>下面是类支持的方法,更多详细java随机生成字符串,参看 Java API 文档:
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>SN(序号)方法描述
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>char (int index)返回指定索引处的 char 值。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>int ( o)把这个字符串和另一个对象比较。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>int ( )按字典顺序比较两个字符串。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>int ( str)按字典顺序比较两个字符串,不考虑大小写。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> ( str)将指定字符串连接到此字符串的结尾。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> ( sb)当且仅当字符串与指定的有相同顺序的字符时候返回真。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> (char data)返回指定数组中表示该字符序列的 。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> (char data, int , int count)返回指定数组中表示该字符序列的 。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> ( )测试此字符串是否以指定的后缀结束。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>10
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> ( )将此字符串与指定的对象比较。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>11
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> ( )将此 与另一个 比较,不考虑大小写。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>12
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>byte () 使用平台的默认字符集将此 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>13
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>byte ( )使用指定的字符集将此 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>14
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>void (int , int , char dst, int )将字符从此字符串复制到目标字符数组。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>15
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>int ()返回此字符串的哈希码。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>16
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>int (int ch)返回指定字符在此字符串中第一次出现处的索引。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>17
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>int (int ch, int )返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>18
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>int ( str) 返回指定子字符串在此字符串中第一次出现处的索引。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>19
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>int ( str, int )返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>20
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> () 返回字符串对象的规范化表示形式。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>21
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>int (int ch) 返回指定字符在此字符串中最后一次出现处的索引。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>22
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>int (int ch, int )返回指定字符在此字符串中最后一次出现处的索引java随机生成字符串,从指定的索引处开始进行反向搜索。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>23
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>2871977844,2851609072&fm=253&fmt=JPEG&app=138&f=JPEG?w=547&h=500″ alt=”java随机生成字符串”/ style=”max-width: 100%; height: auto; display: block; margin: 1.6em auto; border-radius: 6px; border: 1px solid #f0abfc;”>
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>int ( str)返回指定子字符串在此字符串中最右边出现处的索引。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>24
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>int ( str, int ) 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>25
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>int ()返回此字符串的长度。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>26
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> ( regex)告知此字符串是否匹配给定的正则表达式。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>27
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> ( , int , other, int , int len)测试两个字符串区域是否相等。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>28
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> (int , other, int , int len)测试两个字符串区域是否相等。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>29
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> (char , char )返回一个新的字符串,它是通过用 替换此字符串中出现的所有 得到的。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>30
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> ( regex, 使用给定的 替换此字符串所有匹配给定的正则表达式的子字符串。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>31
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> ( regex, ) 使用给定的 替换此字符串匹配给定的正则表达式的第一个子字符串。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>32
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> split( regex)根据给定正则表达式的匹配拆分此字符串。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>33
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> split( regex, int limit)根据匹配给定的正则表达式来拆分此字符串。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>34
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> ( )测试此字符串是否以指定的前缀开始。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>35
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> ( , int )测试此字符串从指定索引开始的子字符串是否以指定前缀开始。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>36
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> (int , int ) 返回一个新的字符序列,它是此序列的一个子序列。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>37
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> (int )返回一个新的字符串创建字符串,它是此字符串的一个子字符串。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>38
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> (int , int )返回一个新字符串,它是此字符串的一个子字符串。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>39
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>char ()将此字符串转换为一个新的字符数组。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>40
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> ()使用默认语言环境的规则将此 中的所有字符都转换为小写。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>41
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> ( ) 使用给定 的规则将此 中的所有字符都转换为小写。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>42
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> () 返回此对象本身(它已经是一个字符串!)。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>43
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> ()使用默认语言环境的规则将此 中的所有字符都转换为大写。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>44
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> ( )使用给定 的规则将此 中的所有字符都转换为大写。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>45
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> trim()返回字符串的副本,忽略前导空白和尾部空白。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>46
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”> ( data type x)返回给定data type类型x参数的字符串表示形式。
<p style="margin: 1em 0; line-height: 1.9; font-size: 16px; color: #262626; text-align: justify;”>如您还有不明白的可以在下面与我留言或是与我探讨QQ群,我们一起飞!
1. 本站所有资源来源于用户上传和网络,如有侵权请联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系站长处理!
6. 本站不售卖代码,资源标价只是站长收集整理的辛苦费!如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
7. 站长QQ号码 2205675299
资源库 - 资源分享下载网 » 创建字符串
常见问题FAQ
- 关于资源售价和售后服务的说明?
- 代码有没有售后服务和技术支持?
- 有没有搭建服务?
- 链接地址失效了怎么办?
- 关于解压密码

