java基础:String详解

package com.string;
import java.io.UnsupportedEncodingException;
/**
 * 
 * @author 郑
 *
 */
public class TestString {
public static void main(String[] args) {


// 注意:String 类是不可改变的,所以你一旦创建了 String 对象,那它的值就无法改变了


String str = "abc";
System.out.println("常规赋值:" + str);


// 通过构造函数赋值
// String str1=new String("accp");
System.out.println("通过构造函数赋值:" + new String("accp"));


// 通过字符数组赋值
// String strChar=new String(new char[]{'a','b','c'});
char[] ch = new char[] { 'a', 'b', 'c' };
System.out.println("通过String有参char[]赋值:" + new String(new char[] { 'a', 'b', 'c' }));
// 查看字符串的长度
System.out.println("通过length()查看字符串的长度:" + str.length());
// 判断字符串是否为空,返回boolean
System.out.println("isEmpty()验证:" + str.isEmpty());
// 根据传入的下标返回该字符,下标从零开始。
System.out.println("charAt()验证:" + str.charAt(2));
// 判断该字符串中是否有包含defg字符串,返回Boolean
System.out.println("contains()验证:" + str.contains(new String("cb")));
// substring(开始下标,结束下标) 下标从零开始
System.out.println("substring()验证:" + str.substring(1, 2));
// concat()连接两个字符串的方法,就是将两个字符串连接起来
String str123 = new String("123");
System.out.println("concat()验证:" + str.concat(str123));
// 格式化字符串 %s,%d和%f分别用来表示输出时,替换整型输出和浮点型输出的占位符,%n是换行的格式字符串
float floatVar = 10f;
int intVar = 20;
String stringVar = "abcd321";
System.out.println("方式一:");
System.out.printf("浮点型变量的值为 " + "%f , 整型变量的值为 " + " %d, 字符串变量的值为 " + "is %s", floatVar, intVar, stringVar);


// 可以这样写
System.out.println("\n方式二:");
String fs;
fs = String.format("浮点型变量的值为 " + "%f, 整型变量的值为 " + " %d, 字符串变量的值为 " + " %s", floatVar, intVar, stringVar);


System.out.println(fs);


// compareTo() 1.把这个字符串和另一个对象比较。 2.按字典顺序比较两个字符串。 不常用
// 返回值是整型,它是先比较对应字符的大小(ASCII码顺序),
// 如果第一个字符和参数的第一个字符不等,结束比较,返回他们之间的差值,
// 如果第一个字符和参数的第一个字符相等,则以第二个字符和参数的第二个字符做比较,
// 以此类推,直至比较的字符或被比较的字符有一方。
// 如果参数字符串等于此字符串,则返回值 0;
// 如果此字符串小于字符串参数,则返回一个小于 0 的值;
// 如果此字符串大于字符串参数,则返回一个大于 0 的值。


String strObj = "Strings";


String str1 = "Strings";
String str2 = "Strings";
String str3 = "Strings123";
int result = str1.compareTo(str2);


System.out.println("compareTo()验证,一般不常用:");
System.out.println("compareTo(),str1和str2比较后::" + result);


result = str2.compareTo(str3);
System.out.println("compareTo(),str2和str3比较后:" + result);


result = str3.compareTo(str1);
System.out.println("compareTo(),str3和str1比较后:" + result);


// contentEquals():用于将此字符串与指定的 StringBuffer 比较。


String strSb1 = "String1";
String strSb2 = "String2";
StringBuffer sb = new StringBuffer("String1");


boolean bol = strSb1.contentEquals(sb);
System.out.println("contentEquals(),strSb1和sb比较后:" + bol);


bol = strSb2.contentEquals(sb);
System.out.println("contentEquals(),strSb2和sb比较后:" + bol);


// copyValueOf() 方法有两种形式:
// public static String copyValueOf(char[] data): 返回指定数组中表示该字符序列的字符串。
// public static String copyValueOf(char[] data, int offset, int count): data --
// 字符数组。offset -- 子数组的初始偏移量。count -- 子数组的长度。
// 返回指定数组中表示该字符序列的 字符串。


char[] copyChar = { 'a', 'b', 'c', 'b', ' ', 'c', 'd', 'e', 'f', 'g' };
String copyStr = "";
copyStr = copyStr.copyValueOf(copyChar);
System.out.println("copyStr和copyChar返回结果:" + copyStr);


copyStr = copyStr.copyValueOf(copyChar, 2, 6);// 下标从零开始,2是开始下标,6是结束下标
System.out.println("copyChar,2~6返回结果:" + copyStr);


// endsWith():用于测试字符串是否以指定的后缀结束。 public boolean endsWith(String suffix):suffix
// -- 指定的后缀。


String Str = new String("菜鸟教程:www.runoob.com");
boolean retVal;


retVal = Str.endsWith("runoob");
System.out.println("endsWith(),是否以runoob为后缀结束的:返回值 = " + retVal);


retVal = Str.endsWith(".com");
System.out.println("endsWith(),是否以com为后缀结束的:返回值 = " + retVal);


// equals() :用于将字符串与指定的对象比较。public boolean equals(Object anObject):anObject --
// 与字符串进行比较的对象。
// 如果给定对象与字符串相等,则返回 true;否则返回 false。


String Str1 = new String("runoob");
String Str2 = Str1;
String Str3 = new String("runoob");
boolean retEquest;


retEquest = Str1.equals(Str2);
System.out.println("equals(),Str1和str2比较后:返回值 = " + retEquest);


retEquest = Str1.equals(Str3);
System.out.println("equals(),Str1和str3比较后:返回值 = " + retEquest);


// equalsIgnoreCase() :用于将字符串与指定的对象比较,不考虑大小写。
// 语法:public boolean equalsIgnoreCase(String anotherString),
// 参数:anObject -- 与字符串进行比较的对象。
// 返回值:如果给定对象与字符串相等,则返回 true;否则返回 false。
String equalsIgnoreCaseStr1 = new String("runoob");
String equalsIgnoreCaseStr2 = equalsIgnoreCaseStr1;
String equalsIgnoreCaseStr3 = new String("runoob");
String equalsIgnoreCaseStr4 = new String("RUNOOB");
boolean equalsIgnoreCaseRetVal;


equalsIgnoreCaseRetVal = equalsIgnoreCaseStr1.equals(equalsIgnoreCaseStr2);
System.out.println("equals(),equalsIgnoreCaseStr1和equalsIgnoreCaseStr2比较后:返回值 = " + equalsIgnoreCaseRetVal);


equalsIgnoreCaseRetVal = equalsIgnoreCaseStr1.equals(equalsIgnoreCaseStr3);
System.out.println("equals(),equalsIgnoreCaseStr1和equalsIgnoreCaseStr3比较后:返回值 = " + equalsIgnoreCaseRetVal);


equalsIgnoreCaseRetVal = equalsIgnoreCaseStr1.equalsIgnoreCase(equalsIgnoreCaseStr4);
System.out.println(
"equalsIgnoreCase(),equalsIgnoreCaseStr1和equalsIgnoreCaseStr4比较后:返回值 = " + equalsIgnoreCaseRetVal);


// getBytes() 方法有两种形式:
// getBytes(String charsetName): 使用指定的字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
// getBytes(): 使用平台的默认字符集将字符串编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
// 方式一: public byte[] getBytes(String charsetName) throws
// UnsupportedEncodingException
// 方式二: public byte[] getBytes()
// 方式一参数:charsetName -- 支持的字符集名称。
// 返回值:返回 byte 数组。


String byteStr1 = new String("zheng");
try {
byte[] bytes = byteStr1.getBytes();
System.out.println("getBytes(),byteStr1.getBytes():返回值:" + bytes);


bytes = byteStr1.getBytes("UTF-8");
System.out.println("getBytes(),byteStr1.getBytes(\"UTF-8\"),:返回值:" + bytes);


bytes = byteStr1.getBytes("ISO-8859-1");
System.out.println("getBytes(),byteStr1.getBytes( \"ISO-8859-1\"),:返回值:" + bytes);


} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


// getChars() 方法将字符从字符串复制到目标字符数组
// 语法:
// public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
// 参数:
// srcBegin -- 字符串中要复制的第一个字符的索引。
// srcEnd -- 字符串中要复制的最后一个字符之后的索引。
// dst -- 目标数组。
// dstBegin -- 目标数组中的起始偏移量。
// 返回值:
// 没有返回值,但会抛出 IndexOutOfBoundsException 异常。


String charsStr1 = new String("www.baidu.com");
char[] chars = new char[6];


try {
charsStr1.getChars(4, 9, chars, 0);
System.out.print("拷贝的字符串为:");
System.out.println(chars);
} catch (Exception ex) {
System.out.println("触发异常...");
}


// indexOf() 方法有以下四种形式:
// public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
// public int indexOf(int ch, int fromIndex): 返回从
// fromIndex:位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,,传入两个int类型的值,另一个值是哈希码值。则返回
// -1。
// int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
// int indexOf(String str, int fromIndex): 返回从 fromIndex:
// 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
// 语法:
// public int indexOf(int ch )
// 或
// public int indexOf(int ch, int fromIndex)
// 或
// int indexOf(String str)
// 或
// int indexOf(String str, int fromIndex)
// 参数:
// ch -- 字符,Unicode 编码。
// fromIndex -- 开始搜索的索引位置。
// str -- 要搜索的子字符串。
// 返回值:
// 查找字符串,或字符 Unicode 编码在字符串出现的位置:


String indexOfStr = "aaa456ac";
// 查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.
System.out.println(indexOfStr.indexOf("b")); // indexOf(String str); 返回结果:-1,"b"不存在


// 从第四个字符位置开始往后继续查找,包含当前位置
System.out.println(indexOfStr.indexOf("a", 3));// indexOf(String str, int fromIndex); 返回下标结果:6


// (与之前的差别:上面的参数是 String 类型,下面的参数是 int 类型)97,98,99是哈希码,参考数据:a-97,b-98,c-99
System.out.println("97,98,99是哈希码");
// 从头开始查找是否存在指定的字符
System.out.println("indexOfStr.indexOf(99),返回值:" + indexOfStr.indexOf(99));// indexOf(int ch);99是哈希码。 返回结果:7
System.out.println("indexOfStr.indexOf('c'),返回值:" + indexOfStr.indexOf('c'));// indexOf(int ch);返回结果:7


// 从fromIndex查找ch,这个是字符型变量,不是字符串。字符a对应的数字就是97,97是哈希码。
System.out.println("indexOfStr.indexOf(97, 3),返回值:" + indexOfStr.indexOf(97, 3));// indexOf(int ch, int
// fromIndex); 返回结果:6
System.out.println("indexOfStr.indexOf('a', 3),返回值:" + indexOfStr.indexOf('a', 3));// indexOf(int ch, int
// fromIndex); 返回结果:6


// intern() 方法返回字符串对象的规范化表示形式。


// 它遵循以下规则:对于任意两个字符串 s 和 t,当且仅当 s.equals(t) 为 true 时,s.intern() == t.intern()
// 才为: true。
// 语法:没有参数
// public String intern()
// 返回值:
// 一个字符串,内容与此字符串相同,但一定取自具有唯一字符串的池。


String internStr1 = new String("www.runoob.com");
String internStr2 = new String("WWW.RUNOOB.COM");


System.out.print("internStr1.intern(),规范表示:");
System.out.println(internStr1.intern());


System.out.print("internStr2.intern():规范表示:");
System.out.println(internStr2.intern());


// lastIndexOf() 方法有以下四种形式:
//
// public int lastIndexOf(int ch): 返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
//
// public int lastIndexOf(int ch, int fromIndex):
// 返返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
//
// public int lastIndexOf(String str): 返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回
// -1。
//
// public int lastIndexOf(String str, int fromIndex):
// 返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
//
// 语法:
// public int lastIndexOf(int ch)
//
// 或
//
// public int lastIndexOf(int ch, int fromIndex)
//
// 或
//
// public int lastIndexOf(String str)
//
// 或
//
// public int lastIndexOf(String str, int fromIndex)
// 参数:
// ch -- 字符。
//
// fromIndex -- 开始搜索的索引位置。
//
// str -- 要搜索的子字符串。
//
// 返回值:
// 指定子字符串在字符串中第一次出现处的索引值。


String lastIndexOfStr = new String("菜鸟教程:www.runoob.com");
String SubStr1 = new String("runoob");
String SubStr2 = new String("com");


System.out.print("查找字符 o 最后出现的位置 :");
System.out.println("lastIndexOfStr.lastIndexOf( 'o' ),返回值" + lastIndexOfStr.lastIndexOf('o'));
System.out.print("从第14个位置查找字符 o 最后出现的位置 :");
System.out.println("lastIndexOfStr.lastIndexOf( 'o',14 ),返回值" + lastIndexOfStr.lastIndexOf('o', 14));
System.out.print("子字符串 SubStr1 最后出现的位置:");
System.out.println("lastIndexOfStr.lastIndexOf( SubStr1 ),返回值" + lastIndexOfStr.lastIndexOf(SubStr1));
System.out.print("从第十五个位置开始搜索子字符串 SubStr1最后出现的位置 :");
System.out.println("lastIndexOfStr.lastIndexOf( SubStr1,15 ),返回值" + lastIndexOfStr.lastIndexOf(SubStr1, 15));
System.out.print("子字符串 SubStr2 最后出现的位置 :");
System.out.println("lastIndexOfStr.lastIndexOf( SubStr2 ),返回值" + lastIndexOfStr.lastIndexOf(SubStr2));


// matches() 方法用于检测字符串是否匹配给定的正则表达式。
//
// 调用此方法的 str.matches(regex) 形式与以下表达式产生的结果完全相同:
//
// Pattern.matches(regex, str)
// 语法
// public boolean matches(String regex)
// 参数
// regex -- 匹配字符串的正则表达式。
//
// 返回值
// 在字符串匹配给定的正则表达式时,返回 true。


String matchesStr = new String("www.runoob.com");


System.out.print("matchesStr.matches(\"(.*)runoob(.*)\"),返回值 :");
System.out.println(matchesStr.matches("(.*)runoob(.*)"));


System.out.print("matchesStr.matches(\"(.*)google(.*)\"),返回值 :");
System.out.println(matchesStr.matches("(.*)google(.*)"));


System.out.print("matchesStr.matches(\"www(.*)\"),返回值 :");
System.out.println(matchesStr.matches("www(.*)"));


// egionMatches() 方法用于检测两个字符串在一个区域内是否相等。
//
// 语法
// public boolean regionMatches(int toffset,
// String other,
// int ooffset,
// int len)
//
// 或
//
// public boolean regionMatches(boolean ignoreCase,
// int toffset,
// String other,
// int ooffset,
// int len)
// 参数
// ignoreCase -- 如果为 true,则比较字符时忽略大小写。
//
// toffset -- 此字符串中子区域的起始偏移量。
//
// other -- 字符串参数。
//
// ooffset -- 字符串参数中子区域的起始偏移量。
//
// len -- 要比较的字符数。
//
// 返回值
// 如果字符串的指定子区域匹配字符串参数的指定子区域,则返回 true;否则返回 false。是否完全匹配或考虑大小写取决于 ignoreCase 参数。


String regionMatchesStr1 = new String("www.runoob.com");
String regionMatchesStr2 = new String("runoob");
String regionMatchesStr3 = new String("RUNOOB");


System.out.print("regionMatchesStr1.regionMatches(4, regionMatchesStr2, 0, 5),返回值 :");
System.out.println(regionMatchesStr1.regionMatches(4, regionMatchesStr2, 0, 5));


System.out.print("regionMatchesStr1.regionMatches(4, regionMatchesStr3, 0, 5),返回值 :");
System.out.println(regionMatchesStr1.regionMatches(4, regionMatchesStr3, 0, 5));


System.out.print("regionMatchesStr1.regionMatches(true, 4, regionMatchesStr3, 0, 5),返回值 :");
System.out.println(regionMatchesStr1.regionMatches(true, 4, regionMatchesStr3, 0, 5));


// replace() 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串。
//
// 语法
// public String replace(char oldChar,
// char newChar)
// 参数
// oldChar -- 原字符。
//
// newChar -- 新字符。
//
// 返回值
// 替换后生成的新字符串。
String replaceStr = new String("hello");


System.out.print("返回值 :");
System.out.println("replaceStr.replace('o', 'T'),返回值:" + replaceStr.replace('o', 'T'));


System.out.print("返回值 :");
System.out.println("replaceStr.replace('l', 'D'),返回值:" + replaceStr.replace('l', 'D'));


// replaceAll() 方法使用给定的参数 replacement 替换字符串所有匹配给定的正则表达式的子字符串。
//
// 语法
// public String replaceAll(String regex, String replacement)
// 参数
// regex -- 匹配此字符串的正则表达式。
//
// newChar -- 用来替换每个匹配项的字符串。
//
// 返回值
// 成功则返回替换的字符串,失败则返回原始字符串。
String replaceAllStr = new String("www.google.com");


System.out.print("匹配成功返回值 :");
System.out.println("replaceAllStr.replaceAll(\"(.*)google(.*)\", \"runoob\" ),匹配成功返回值:"
+ replaceAllStr.replaceAll("(.*)google(.*)", "runoob"));
System.out.print("匹配失败返回值 :");
System.out.println("replaceAllStr.replaceAll(\"(.*)taobao(.*)\", \"runoob\" ),匹配失败返回值:"
+ replaceAllStr.replaceAll("(.*)taobao(.*)", "runoob"));


// eplaceFirst() 方法使用给定的参数 replacement 替换字符串第一个匹配给定的正则表达式的子字符串。
//
// 语法
// public String replaceFirst(String regex,
// String replacement)
// 参数
// regex -- 匹配此字符串的正则表达式。
//
// replacement -- 用来替换第一个匹配项的字符串。
//
// 返回值
// 成功则返回替换的字符串,失败则返回原始字符串。


String replaceFirstStr = new String("hello runoob,I am from runoob。");
System.out.print("replaceFirstStr.replaceFirst(runoob, google ),返回值 :");
System.out.println(replaceFirstStr.replaceFirst("runoob", "google"));
System.out.print("replaceFirstStr.replaceFirst((.*)runoob(.*), google ),返回值 :");
System.out.println(replaceFirstStr.replaceFirst("(.*)runoob(.*)", "google"));


// split() 方法根据匹配给定的正则表达式来拆分字符串。
//
// 注意: . 、 | 和 * 等转义字符,必须得加 \\。
//
// 注意:多个分隔符,可以用 | 作为连字符。
//
// 语法
// public String[] split(String regex, int limit)
// 参数
// regex -- 正则表达式分隔符。
//
// limit -- 分割的份数。
//
// 返回值
// 字符串数组。


String splitStr = new String("Welcome-to-Runoob");


System.out.println("- 分隔符返回值 :");
for (String retval : splitStr.split("-")) {
System.out.println(retval);
}


System.out.println("");
System.out.println("- 分隔符设置分割份数返回值 :");
for (String retval : splitStr.split("-", 2)) {
System.out.println(retval);
}


System.out.println("");
String splitStr2 = new String("www.runoob.com");
System.out.println("转义字符返回值 :");
for (String retval : splitStr2.split("\\.", 3)) {
System.out.println(retval);
}


System.out.println("");
String splitStr3 = new String("acount=? and uu =? or n=?");
System.out.println("多个分隔符返回值 :");
for (String retval : splitStr3.split("and|or")) {
System.out.println(retval);
}


// startsWith() 方法用于检测字符串是否以指定的前缀开始。
//
// 语法
// public boolean startsWith(String prefix, int toffset)
//
// 或
//
// public boolean startsWith(String prefix)
// 参数
// prefix -- 前缀。
//
// toffset -- 字符串中开始查找的位置。
//
// 返回值
// 如果字符串以指定的前缀开始,则返回 true;否则返回 false。


String startsWithStr = new String("www.runoob.com");


System.out.print("startsWithStr.startsWith(www) ,返回值 :");
System.out.println(startsWithStr.startsWith("www"));


System.out.print("startsWithStr.startsWith(runoob) ,返回值 :");
System.out.println(startsWithStr.startsWith("runoob"));


System.out.print("startsWithStr.startsWith(runoob, 4) ;返回值 :");
System.out.println(startsWithStr.startsWith("runoob", 4));


// subSequence() 方法返回一个新的字符序列,它是此序列的一个子序列。
//
// 语法
// public CharSequence subSequence(int beginIndex, int endIndex)
// 参数
// beginIndex -- 起始索引(包括)。
//
// endIndex -- 结束索引(不包括)。
//
// 返回值
// 返回一个新的字符序列,它是此序列的一个子序列。
String subSequenceStr = new String("www.runoob.com");


System.out.print("subSequenceStr.subSequence(4, 10) ,返回值 :");
System.out.println(subSequenceStr.subSequence(4, 10));


// substring() 方法返回字符串的子字符串。
//
// 语法
// public String substring(int beginIndex)
//
// 或
//
// public String substring(int beginIndex, int endIndex)
// 参数
// beginIndex -- 起始索引(包括)。
//
// endIndex -- 结束索引(不包括)。
//
// 返回值
// 子字符串。
String substringStr = new String("www.runoob.com");


System.out.print("(substringStr.substring(4) ,返回值 :");
System.out.println(substringStr.substring(4));


System.out.print("substringStr.substring(4, 10),返回值 :");
System.out.println(substringStr.substring(4, 10));


// toCharArray() 方法将字符串转换为字符数组。
//
// 语法
// public char[] toCharArray()
// 参数
// 无
//
// 返回值
// 字符数组。


String toCharArrayStr = new String("www.runoob.com");


System.out.print("toCharArrayStr.toCharArray() ,返回值 :");
char[] toChars = toCharArrayStr.toCharArray();
System.out.println(toChars);


// toLowerCase() 方法将字符串转换为小写。
//
// 语法
// public String toLowerCase()
//
// 或
//
// public String toLowerCase(Locale locale)
// 参数
// 无
//
// 返回值
// 转换为小写的字符串。


String toLowerCaseStr = new String("WWW.RUNOOB.COM");


System.out.print("toLowerCaseStr(),返回值 :");
System.out.println(toLowerCaseStr.toLowerCase());


// toString() 方法返回此对象本身(它已经是一个字符串)。
//
// 语法
// public String toString()
// 参数
// 无
//
// 返回值
// 字符串本身。


String toStringStr = new String("WWW.RUNOOB.COM");


System.out.print("toStringStr.toString() ,返回值 :");
System.out.println(toStringStr.toString());


// toUpperCase() 方法将字符串小写字符转换为大写。
//
// 语法
// public String toUpperCase()
//
// 或
//
// public String toUpperCase(Locale locale)
// 参数
// 无
//
// 返回值
// 字符转换为大写后的字符串。

String toUpperCaseStr = new String("www.runoob.com");


        System.out.print("toUpperCaseStr,返回值 :" );
        System.out.println( toUpperCaseStr.toUpperCase() );
        
// trim() 方法用于删除字符串的头尾空白符。
//
// 语法
// public String trim()
// 参数
// 无
//
// 返回值
// 删除头尾空白符的字符串。
        
        String trimStr = new String("    www.runoob.com    ");
        System.out.print("原始值 :" );
        System.out.println( trimStr );


        System.out.print(" trimStr.trim(),删除头尾空白 :" );
        System.out.println( trimStr.trim() );
        
// valueOf() 方法有以下几种不同形式:
//
// valueOf(boolean b): 返回 boolean 参数的字符串表示形式。.
//
// valueOf(char c): 返回 char 参数的字符串表示形式。
//
// valueOf(char[] data): 返回 char 数组参数的字符串表示形式。
//
// valueOf(char[] data, int offset, int count): 返回 char 数组参数的特定子数组的字符串表示形式。
//
// valueOf(double d): 返回 double 参数的字符串表示形式。
//
// valueOf(float f): 返回 float 参数的字符串表示形式。
//
// valueOf(int i): 返回 int 参数的字符串表示形式。
//
// valueOf(long l): 返回 long 参数的字符串表示形式。
//
// valueOf(Object obj): 返回 Object 参数的字符串表示形式。
//
// 语法
// static String valueOf(boolean b)
//
// 或
//
// static String valueOf(char c)
//
// 或
//
// static String valueOf(char[] data)
//
// 或
//
// static String valueOf(char[] data, int offset, int count)
//
// 或
//
// static String valueOf(double d)
//
// 或
//
// static String valueOf(float f)
//
// 或
//
// static String valueOf(int i)
//
// 或
//
// static String valueOf(long l)
//
// 或
//
// static String valueOf(Object obj)
// 参数
// 指定类型参数。
//
// 返回值
// 删除头尾空白符的字符串。
        
        double d = 1100.00;
        boolean b = true;
        long l = 1234567890;
        char[] arr = {'r', 'u', 'n', 'o', 'o', 'b' };


        System.out.println("String.valueOf(d),返回值 : " + String.valueOf(d) );
        System.out.println("String.valueOf(b),返回值 : " + String.valueOf(b) );
        System.out.println("String.valueOf(l),返回值 : " + String.valueOf(l) );
        System.out.println("String.valueOf(arr),返回值 : " + String.valueOf(arr) );
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值