Java获取字符串长度获取

Java 以字符串为源进行流读取 -
- ITeye博客
这个其实很简单,以前以见过,今天要用,一下子还是想不起来,就记下来,以后参照:
需求:现在有一个字符串(存在一个String变量中),想以一行行读取的方式(readLine)获取其中的内容。
解析:有人说用String.split("\n")的方式,要知道换行符对不同的平台是不一样的,大体三种情况:"\n", "\r", "\r\n"
我们可以手工处理这三种情况的换行,但是有一个读取字符流已经都实现好了(BufferedReader.readLine()),那我们就拿来用吧。
String s = "A\rB\nC\r\nD";
//封装ByteArrayInputStream--&InputStreamReader--&BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(s.getBytes(Charset.forName("utf8"))), Charset.forName("utf8")));
while ( (line = br.readLine()) != null ) {
System.out.println(line);
结果输出:
浏览: 462149 次
来自: 上海
如若_晴 写道急求答案,楼主你好,我用springcloud
急求答案,楼主你好,我用springcloud 上传文件时,按 ...
[qote=&xiaoliji
看了各位答案,我整理一下,供大家参考1.&if test ...java字符串编码类型获取
- ITeye博客
Encoding.java
package org.loon.test.
* Title: LoonFramework
* Description:编码基本类型集合
* Copyright: Copyright (c) 2008
* Company: LoonFramework
* License: http://www.apache.org/licenses/LICENSE-2.0
* @author chenpeng
* @email:.cn
* @version 0.1
public class Encoding ...{
// 支持的字符格式
public static int GB2312 = 0;
public static int GBK = 1;
public static int BIG5 = 2;
public static int UTF8 = 3;
public static int UNICODE = 4;
public static int EUC_KR = 5;
public static int SJIS = 6;
public static int EUC_JP = 7;
public static int ASCII = 8;
public static int UNKNOWN = 9;
public static int TOTALT = 10;
public final static int SIMP = 0;
public final static int TRAD = 1;
// 解析名称用
public static String[]
public static String[]
// 应用于html中的字符集
public static String[]
public Encoding() ...{
javaname = new String[TOTALT];
nicename = new String[TOTALT];
htmlname = new String[TOTALT];
javaname[GB2312] = "GB2312";
javaname[GBK] = "GBK";
javaname[BIG5] = "BIG5";
javaname[UTF8] = "UTF8";
javaname[UNICODE] = "Unicode";
javaname[EUC_KR] = "EUC_KR";
javaname[SJIS] = "SJIS";
javaname[EUC_JP] = "EUC_JP";
javaname[ASCII] = "ASCII";
javaname[UNKNOWN] = "ISO8859_1";
// 分配编码名称
htmlname[GB2312] = "GB2312";
htmlname[GBK] = "GBK";
htmlname[BIG5] = "BIG5";
htmlname[UTF8] = "UTF-8";
htmlname[UNICODE] = "UTF-16";
htmlname[EUC_KR] = "EUC-KR";
htmlname[SJIS] = "Shift_JIS";
htmlname[EUC_JP] = "EUC-JP";
htmlname[ASCII] = "ASCII";
htmlname[UNKNOWN] = "ISO8859-1";
// 分配可读名称
nicename[GB2312] = "GB-2312";
nicename[GBK] = "GBK";
nicename[BIG5] = "Big5";
nicename[UTF8] = "UTF-8";
nicename[UNICODE] = "Unicode";
nicename[EUC_KR] = "EUC-KR";
nicename[SJIS] = "Shift-JIS";
nicename[EUC_JP] = "EUC-JP";
nicename[ASCII] = "ASCII";
nicename[UNKNOWN] = "UNKNOWN";
public String toEncoding(final int type) ...{
return (javaname[type] + "," + nicename[type] + "," + htmlname[type])
.intern();
Encode,java(省略,见源码)
ParseEncoding.java
package org.loon.test.
import java.io.ByteArrayOutputS
import java.io.F
import java.io.FileInputS
import java.io.FileNotFoundE
import java.io.IOE
import java.io.InputS
import java.net.MalformedURLE
import java.net.URL;
* Title: LoonFramework
* Description:
* Copyright: Copyright (c) 2008
* Company: LoonFramework
* License: http://www.apache.org/licenses/LICENSE-2.0
* @author chenpeng
* @email:.cn
* @version 0.1
public class ParseEncoding extends Encode ...{
public ParseEncoding() ...{
GB2312format = new int[94][94];
GBKformat = new int[126][191];
Big5format = new int[94][158];
EUC_KRformat = new int[94][94];
JPformat = new int[94][94];
// 初始化编码格式
public String getEncoding(final String path) ...{
return check(getEncodeValue(path));
public String getEncoding(final InputStream in) ...{
return check(getEncodeValue(in));
public String getEncoding(final byte[] buffer) ...{
return check(getEncodeValue(buffer));
public String getEncoding(final URL url) ...{
return check(getEncodeValue(url));
private String check(final int result) ...{
if (result == -1) ...{
return nicename[UNKNOWN];
return nicename[result];
* 解析指定字符串路径编码所用格式
* @param path
private int getEncodeValue(String path) ...{
int express = UNKNOWN;
if (path.startsWith("http://")) ...{
express = getEncodeValue(new URL(path));
} catch (MalformedURLException e) ...{
express = -1;
} else ...{
express = getEncodeValue(new File(path));
* 解析指定InputStream所用编码,返回或然率最高的编码类型数值
* @param in
public int getEncodeValue(InputStream in) ...{
byte[] rawtext = new byte[8192];
int bytesread = 0, byteoffset = 0;
int express = UNKNOWN;
InputStream stream =
while ((bytesread = stream.read(rawtext, byteoffset, rawtext.length
- byteoffset)) & 0) ...{
byteoffset +=
stream.close();
express = getEncodeValue(rawtext);
} catch (Exception e) ...{
express = -1;
* 解析指定url下数据所用编码,返回或然率最高的编码类型数值
* @param url
public int getEncodeValue(URL url) ...{
stream = url.openStream();
} catch (IOException e) ...{
return getEncodeValue(stream);
* 解析指定file所用编码,返回或然率最高的编码类型数值
* @param file
public int getEncodeValue(File file) ...{
buffer = read(new FileInputStream(file));
} catch (FileNotFoundException e) ...{
return getEncodeValue(buffer);
* 将inputstream转为byte[]
* @param inputStream
private final byte[] read(final InputStream inputStream) ...{
byte[] arrayByte =
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[8192];
bytes = new byte[inputStream.available()];
while ((read = inputStream.read(bytes)) &= 0) ...{
byteArrayOutputStream.write(bytes, 0, read);
arrayByte = byteArrayOutputStream.toByteArray();
} catch (IOException e) ...{
return arrayB
* 解析指定byte[]所用编码,返回或然率最高的数值类型
* @param content
public int getEncodeValue(byte[] content) ...{
if (content == null)
return -1;
int index, maxscore = 0;
int encoding = UNKNOWN;
scores = new int[TOTALT];
// 分配或然率
scores[GB2312] = gb2312probability(content);
scores[GBK] = gbkprobability(content);
scores[BIG5] = big5probability(content);
scores[UTF8] = utf8probability(content);
scores[UNICODE] = utf16probability(content);
scores[EUC_KR] = euc_krprobability(content);
scores[ASCII] = asciiprobability(content);
scores[SJIS] = sjisprobability(content);
scores[EUC_JP] = euc_jpprobability(content);
scores[UNKNOWN] = 0;
// 概率比较
for (index = 0; index & TOTALT; index++) ...{
if (scores[index] & maxscore) ...{
encoding =
// 最大几率
maxscore = scores[index];
// 返回或然率大于50%的数据
if (maxscore &= 50) ...{
encoding = UNKNOWN;
* gb2312数据或然率计算
* @param content
private int gb2312probability(byte[] content) ...{
int i, rawtextlen = 0;
int dbchars = 1, gbchars = 1;
long gbformat = 0, totalformat = 1;
float rangeval = 0, formatval = 0;
// 检查是否在亚洲汉字范围内
rawtextlen = content.
for (i = 0; i & rawtextlen - 1; i++) ...{
if (content[i] &= 0) ...{
} else ...{
dbchars++;
// 汉字GB码由两个字节组成,每个字节的范围是0xA1 ~ 0xFE
if ((byte) 0xA1 &= content[i] && content[i] &= (byte) 0xF7
&& (byte) 0xA1 &= content[i + 1]
&& content[i + 1] &= (byte) 0xFE) ...{
gbchars++;
totalformat += 500;
row = content[i] + 256 - 0xA1;
column = content[i + 1] + 256 - 0xA1;
if (GB2312format[row][column] != 0) ...{
gbformat += GB2312format[row][column];
} else if (15 &= row && row & 55) ...{
// 在gb编码范围
gbformat += 200;
rangeval = 50 * ((float) gbchars / (float) dbchars);
formatval = 50 * ((float) gbformat / (float) totalformat);
return (int) (rangeval + formatval);
* gb2312或然率计算
* @param content
private int gbkprobability(byte[] content) ...{
int i, rawtextlen = 0;
int dbchars = 1, gbchars = 1;
long gbformat = 0, totalformat = 1;
float rangeval = 0, formatval = 0;
rawtextlen = content.
for (i = 0; i & rawtextlen - 1; i++) ...{
if (content[i] &= 0) ...{
} else ...{
dbchars++;
if ((byte) 0xA1 &= content[i] && content[i] &= (byte) 0xF7
&& // gb范围
(byte) 0xA1 &= content[i + 1]
&& content[i + 1] &= (byte) 0xFE) ...{
gbchars++;
totalformat += 500;
row = content[i] + 256 - 0xA1;
column = content[i + 1] + 256 - 0xA1;
if (GB2312format[row][column] != 0) ...{
gbformat += GB2312format[row][column];
} else if (15 &= row && row & 55) ...{
gbformat += 200;
} else if ((byte) 0x81 &= content[i]
&& content[i] &= (byte) 0xFE && // gb扩展区域
(((byte) 0x80 &= content[i + 1] && content[i + 1] &= (byte) 0xFE) || ((byte) 0x40 &= content[i + 1] && content[i + 1] &= (byte) 0x7E))) ...{
gbchars++;
totalformat += 500;
row = content[i] + 256 - 0x81;
if (0x40 &= content[i + 1] && content[i + 1] &= 0x7E) ...{
column = content[i + 1] - 0x40;
} else ...{
column = content[i + 1] + 256 - 0x40;
if (GBKformat[row][column] != 0) ...{
gbformat += GBKformat[row][column];
rangeval = 50 * ((float) gbchars / (float) dbchars);
formatval = 50 * ((float) gbformat / (float) totalformat);
return (int) (rangeval + formatval) - 1;
* 解析为big5的或然率
* @param content
private int big5probability(byte[] content) ...{
int i, rawtextlen = 0;
int dbchars = 1, bfchars = 1;
float rangeval = 0, formatval = 0;
long bfformat = 0, totalformat = 1;
rawtextlen = content.
for (i = 0; i & rawtextlen - 1; i++) ...{
if (content[i] &= 0) ...{
} else ...{
dbchars++;
if ((byte) 0xA1 &= content[i]
&& content[i] &= (byte) 0xF9
&& (((byte) 0x40 &= content[i + 1] && content[i + 1] &= (byte) 0x7E) || ((byte) 0xA1 &= content[i + 1] && content[i + 1] &= (byte) 0xFE))) ...{
bfchars++;
totalformat += 500;
row = content[i] + 256 - 0xA1;
if (0x40 &= content[i + 1] && content[i + 1] &= 0x7E) ...{
column = content[i + 1] - 0x40;
} else ...{
column = content[i + 1] + 256 - 0x61;
if (Big5format[row][column] != 0) ...{
bfformat += Big5format[row][column];
} else if (3 &= row && row &= 37) ...{
bfformat += 200;
rangeval = 50 * ((float) bfchars / (float) dbchars);
formatval = 50 * ((float) bfformat / (float) totalformat);
return (int) (rangeval + formatval);
* 在utf-8中的或然率
* @param content
private int utf8probability(byte[] content) ...{
int score = 0;
int i, rawtextlen = 0;
int goodbytes = 0, asciibytes = 0;
// 检查是否为汉字可接受范围
rawtextlen = content.
for (i = 0; i & i++) ...{
if ((content[i] & (byte) 0x7F) == content[i]) ...{
asciibytes++;
} else if (-64 &= content[i] && content[i] &= -33
&& i + 1 & rawtextlen && -128 &= content[i + 1]
&& content[i + 1] &= -65) ...{
goodbytes += 2;
} else if (-32 &= content[i] && content[i] &= -17
&& i + 2 & rawtextlen && -128 &= content[i + 1]
&& content[i + 1] &= -65 && -128 &= content[i + 2]
&& content[i + 2] &= -65) ...{
goodbytes += 3;
if (asciibytes == rawtextlen) ...{
score = (int) (100 * ((float) goodbytes / (float) (rawtextlen - asciibytes)));
// 如果不高于98则减少到零
if (score & 98) ...{
} else if (score & 95 && goodbytes & 30) ...{
} else ...{
* 检查为utf-16的或然率
* @param content
private int utf16probability(byte[] content) ...{
if (content.length & 1
&& ((byte) 0xFE == content[0] && (byte) 0xFF == content[1])
|| ((byte) 0xFF == content[0] && (byte) 0xFE == content[1])) ...{
return 100;
* 检查为ascii的或然率
* @param content
private int asciiprobability(byte[] content) ...{
int score = 75;
rawtextlen = content.
for (i = 0; i & i++) ...{
if (content[i] & 0) ...{
score = score - 5;
} else if (content[i] == (byte) 0x1B) ...{ // ESC (used by ISO 2022)
score = score - 5;
if (score &= 0) ...{
* 检查为euc_kr的或然率
* @param content
private int euc_krprobability(byte[] content) ...{
int i, rawtextlen = 0;
int dbchars = 1, krchars = 1;
long krformat = 0, totalformat = 1;
float rangeval = 0, formatval = 0;
rawtextlen = content.
for (i = 0; i & rawtextlen - 1; i++) ...{
if (content[i] &= 0) ...{
} else ...{
dbchars++;
if ((byte) 0xA1 &= content[i] && content[i] &= (byte) 0xFE
&& (byte) 0xA1 &= content[i + 1]
&& content[i + 1] &= (byte) 0xFE) ...{
krchars++;
totalformat += 500;
row = content[i] + 256 - 0xA1;
column = content[i + 1] + 256 - 0xA1;
if (EUC_KRformat[row][column] != 0) ...{
krformat += EUC_KRformat[row][column];
} else if (15 &= row && row & 55) ...{
krformat += 0;
rangeval = 50 * ((float) krchars / (float) dbchars);
formatval = 50 * ((float) krformat / (float) totalformat);
return (int) (rangeval + formatval);
private int euc_jpprobability(byte[] content) ...{
int i, rawtextlen = 0;
int dbchars = 1, jpchars = 1;
long jpformat = 0, totalformat = 1;
float rangeval = 0, formatval = 0;
rawtextlen = content.
for (i = 0; i & rawtextlen - 1; i++) ...{
if (content[i] &= 0) ...{
} else ...{
dbchars++;
if ((byte) 0xA1 &= content[i] && content[i] &= (byte) 0xFE
&& (byte) 0xA1 &= content[i + 1]
&& content[i + 1] &= (byte) 0xFE) ...{
jpchars++;
totalformat += 500;
row = content[i] + 256 - 0xA1;
column = content[i + 1] + 256 - 0xA1;
if (JPformat[row][column] != 0) ...{
jpformat += JPformat[row][column];
} else if (15 &= row && row & 55) ...{
jpformat += 0;
rangeval = 50 * ((float) jpchars / (float) dbchars);
formatval = 50 * ((float) jpformat / (float) totalformat);
return (int) (rangeval + formatval);
private int sjisprobability(byte[] content) ...{
int i, rawtextlen = 0;
int dbchars = 1, jpchars = 1;
long jpformat = 0, totalformat = 1;
float rangeval = 0, formatval = 0;
int row, column,
rawtextlen = content.
for (i = 0; i & rawtextlen - 1; i++) ...{
if (content[i] &= 0) ...{
} else ...{
dbchars++;
if (i + 1 & content.length
&& (((byte) 0x81 &= content[i] && content[i] &= (byte) 0x9F) || ((byte) 0xE0 &= content[i] && content[i] &= (byte) 0xEF))
&& (((byte) 0x40 &= content[i + 1] && content[i + 1] &= (byte) 0x7E) || ((byte) 0x80 &= content[i + 1] && content[i + 1] &= (byte) 0xFC))) ...{
jpchars++;
totalformat += 500;
row = content[i] + 256;
column = content[i + 1] + 256;
if (column & 0x9f) ...{
adjust = 1;
if (column & 0x7f) ...{
column -= 0x20;
} else ...{
column -= 0x19;
} else ...{
adjust = 0;
column -= 0x7e;
if (row & 0xa0) ...{
row = ((row - 0x70) && 1) -
} else ...{
row = ((row - 0xb0) && 1) -
row -= 0x20;
column = 0x20;
if (row & JPformat.length && column & JPformat[row].length
&& JPformat[row][column] != 0) ...{
jpformat += JPformat[row][column];
} else if ((byte) 0xA1 &= content[i]
&& content[i] &= (byte) 0xDF) ...{
rangeval = 50 * ((float) jpchars / (float) dbchars);
formatval = 50 * ((float) jpformat / (float) totalformat);
return (int) (rangeval + formatval) - 1;
EncodingTest.java
package org.loon.test.
* &p&Title: LoonFramework&/p&
* &p&Description:&/p&
* &p&Copyright: Copyright (c) 2008&/p&
* &p&Company: LoonFramework&/p&
* &p&License: http://www.apache.org/licenses/LICENSE-2.0&/p&
* @author chenpeng
* @email:.cn
* @version 0.1
public class EncodingTest ...{
public static void main(String argc[]) ...{
parse = new ParseEncoding();
System.out.println("中国大陆:");
System.out.println("测试字符串,编码格式="+parse.getEncoding("百度".getBytes()));
System.out.println("测试站点,编码格式="+parse.getEncoding(""));
System.out.println();
System.out.println("中国台湾:");
System.out.println("测试字符串,编码格式="+parse.getEncoding("い地チ瓣".getBytes()));
System.out.println("测试站点,编码格式="+parse.getEncoding("/"));
System.out.println("测试站点(繁体字,UTF编码),编码格式="+parse.getEncoding(".tw/jute"));
System.out.println();
System.out.println("日本:");
System.out.println("测试字符串,编码格式="+parse.getEncoding("その機能".getBytes()));
System.out.println("测试站点,编码格式="+parse.getEncoding("http://www.4gamer.net"));
System.out.println();
System.out.println("自称蚩尤后代那群……:");
System.out.println("测试站点,编码格式="+parse.getEncoding("http://www.easyjava.co.kr/"));
浏览: 128726 次
来自: 济南
pageoffice生成excel就不用使用poi。pageo ...
正好我用来转义微博信息
这是primefaces 的么?
用$.pdialog.closeCurrent();关闭当前页 ...
前台如何接收?在Java中按字节获得字符串长度的两种方法
我的图书馆
在Java中按字节获得字符串长度的两种方法
&&& 由于Java是基于Unicode编码的,因此,一个汉字的长度为1,而不是2。但有时需要以字节单位获得字符串的长度。例如,“123abc长城”按字节长度计算是10,而按Unicode计算长度是8。为了获得10,需要从头扫描根据字符的Ascii来获得具体的长度。如果是标准的字符,Ascii的范围是0至255,如果是汉字或其他全角字符,Ascii会大于255。因此,可以编写如下的方法来获得以字节为单位的字符串长度。
&&&&public&int&getWordCount(String&s)&&&&{&&&&&&&&int&length&=&<SPAN style="COLOR: #;&&&&&&&&for(int&i&=&<SPAN style="COLOR: #;&i&&&s.length();&i++)&&&&&&&&{&&&&&&&&&&&&int&ascii&=&Character.codePointAt(s,&i);&&&&&&&&&&&&if(ascii&&=&<SPAN style="COLOR: #&&&&ascii&&=<SPAN style="COLOR: #5)&&&&&&&&&&&&&&&&length++;&&&&&&&&&&&&else&&&&&&&&&&&&&&&&length&+=&<SPAN style="COLOR: #;&&&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&return&&&&&&&&&&&&&}&&& 当然,也可以采用正则表达式来简化上面的方法,代码如下:
&&&&public& int&getWordCount(String&s)&&&&{&&&&&&&&s&=&s.replaceAll("[^\\x00-\\xff]",&"**");&&&&&&&&int&length&=&s.length();&&&&&&&&return&&&&&}
TA的最新馆藏[转]&
喜欢该文的人也喜欢3779人阅读
& String类下的split方法,可以按照指定的定界字符串,对某一字符串进行分割,将待分割字符串中参考字符串前后的字符串提取出来,作为返回&#20540;。返回类型为String中,维度由分割完成的结果决定,但内容中会直接去掉定界字符串。
& 定界字符串查找不到时返回结果为该字符串本身。
& 需要注意的是定界字符串本质上是正则表达式,如果参考字符串中包含有特殊含义的符号,需要进行转义。
& 看例子:
String a = &abcdefgabcdefgabcdefg&;
String b[] = a.split(&a&);
for(inti = 0; i&b. i++)
System.out.println(b[i]);
& 运行结果为:&
& 注意第一行是个空行。
String a = &abcdefgabcdefgabcdefg&;
String b[] = a.split(&a&,2);
for(int i = 0; i&b. i++)
System.out.println(b[i]);
& 运行结果为:
bcdefgabcdefgabcdefg
& 注意第一行仍是空行。
& 即split第二个参数用来限制分割完成的字符串段数,分割由左到右进行,缺省则代表分割段数不限,直至分割完成为止。
& String类下的lastIndexOf方法,用于在当前字符串中查找指定字符串。
& 1、int lastIndexOf(String arg0) 和 int lastIndexOf(int arg0)
& & 用于查找当前字符串中指定字符串或字符(int arg0表字符的ascii码&#20540;)最后出现的位置,返回&#20540;为子串在原串中的相对位置,若找不到,则返回-1。&
& & 看例子:
String s = &abcdefgabcdefg&;
int i = s.lastIndexOf(&cd&);
System.out.println(i);
运行结果为:
& & 需要注意的是字符串s中共有两个”cd”子串,本函数返回的是最后一个子串的位置。
& 2、int lastIndexOf(String arg0, int arg1) 和int lastIndexOf(int arg0, int arg1)
& & 在当前字符串中小于arg1的范围中,查找指定字符串或字符。返回&#20540;同样为子串在原串中最后一次出现的相对位置,只不过查找有范围限制,超出范围的部分即便仍有子串,也无法找到。
& & 看例子:
String s = &abcdefgabcdefg&;
int i = s.lastIndexOf(&cd&,8);
System.out.println(i);
运行结果为:
& & 注意与上一个例子做比较。
& & 但是当范围当中包含了子串的前面的某一位或某几位时:
String s = &abcdefgabcdefg&;
int i = s.lastIndexOf(&cd&,9);
System.out.println(i);
运行结果为:
& & 原因很简单,字符串比较时约束的范围仅限制住了首地址而没有约束长度。
& 与lastIndexOf方法相对的indexOf方法。
& 1、Int indexOf(String arg0)和Int indexOf(int arg0)
& & 这两个方法返回&#20540;为源字符串中子串最先出现的位置(参数int arg0同样指字符的ascii码&#20540;),若找不到,则返回-1。
& & 与上文lastIndexOf对比:
String s = &abcdefgabcdefg&;
int i = s.indexOf(&cd&);
System.out.println(i);
运行结果为:
&2、 int indexOf(String arg0, int arg1) 和int indexOf(int arg0, int arg1)&
& & 这两个方法返回&#20540;为从指定位置起查找,子串最先出现的位置,若找不到,则返回-1。与lastIndexOf相区别的是indexOf的限制arg1参数限定的是由此位起始搜索,
lastIndexOf限定的是搜索到此位为止。
String s = &abcdefgabcdefg&;
int i = s.indexOf(&cd&,8);
System.out.println(i);
运行结果为:
& String substring(int arg0)与String substring(intarg0, int arg1)
& 这两个函数用来在源字符串中根据指定位置取出子串。前者返回源字符串中从参数arg0指定的位置开始至字符串结尾的子串;后者返回源字符串中位置arg0到位置arg1指定的
& substring可与上文中的查找方法一起使用,用于提取指定字符串。
String s = &abcdefgabcdefg&;
String cd = &cd&;
String s1 = s.substring(s.indexOf(cd));
String s2 = s.substring(s.indexOf(cd), s.indexOf(cd)+cd.length());
System.out.println(s1);
System.out.println(s2);
运行结果为:
cdefgabcdefg
注意当参数越界时会抛出java.lang.StringIndexOutOfBoundsException异常。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:30578次
排名:千里之外
原创:34篇
评论:25条
(1)(2)(2)(2)(2)(4)(4)(3)(2)(1)(2)(4)(5)

我要回帖

更多关于 获取字符串长度 的文章

 

随机推荐