看String的源码
看到
public String substring(int beginIndex, int endIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
if (endIndex > count) {
throw new StringIndexOutOfBoundsException(endIndex);
}
if (beginIndex > endIndex) {
throw new StringIndexOutOfBoundsException(endIndex - beginIndex);
}
return ((beginIndex == 0) && (endIndex == count)) ? this :
new String(offset + beginIndex, endIndex - beginIndex, value);
}
当时就想到2个问题,第一个是endIndex可以等于count,因为他的判定条件是Index > count,第二个就是他return的是
new String(offset + beginIndex, endIndex - beginIndex, value)
再看看这个构造方法
String(int offset, int count, char value[]) {
this.value = value;
this.offset = offset;
this.count = count;
}
也就是char value[]是共享的,只是调整offset和count的值来完成截取的。
不明白这样做的目的是什么,有什么好处,新截出来的字符串不是有浪费空间的嫌疑?
然后查阅网上其他人的关于substring的文章
参考博客:http://blog.csdn.net/bzwm/article/details/5844045
发现这个substring方法还是要小心点用
分享到:
相关推荐
软件版权源码整理工具(著作权整理工具)仅适合(*.cpp;*.h;*.java)项目源码_Deom可以改造成志气其他开发语言开发的源码项目; 软件著作权申请时,需要准备前30页后30... lexts.Add(tmpstr.Substring(2).ToLower());
ebook 源码 public class AndroidUtils { public static DisplayMetrics getDisplayMetrics(Context cx) { DisplayMetrics dm = cx.getApplicationContext().getResources().getDisplayMetrics(); return dm; }...
java字符转换类代码,可以... getstr = getstr.substring(0,getstr.indexOf(oldString)) newString getstr.substring(getstr.indexOf(oldString) oldString.length(),getstr.length()); } return getstr; }
void sendMail(String sender,String password,String addressee,String subject,String text,Map<String,File> enclosures,Map<String,RecipientType> copyToSends) throws Exception; /** * sendMail 发送...
hour = dateString.substring(11, 13); return hour; } /** * 得到现在分钟 * * @return */ public static String getTime() { Date currentTime = new Date(); SimpleDateFormat formatter = new ...
Des加密解密C#源码,很实用 byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); byte[] rgbIV = Encoding.ASCII.GetBytes(kesVector); byte[] inputByteArray = Encoding.UTF8.GetBytes...
//处理源码筛选字符串,只获取含有问题标题的内容 // System.out.println(queStr);//测试标题部分字符串 String[] queArr = queStr.split("(||)"); for(int i=0;i;i++){ String msg = queArr[i]; msg = msg....
更多请看源码。 //Get file info from content. string[] contentArray = m_content.Split(';'); string m_temp = contentArray[0]; this.m_contenttype = m_temp.Substring(m_temp.IndexOf(":")+1)....
String fileName = value.substring(start, start + 1); request.setAttribute(name, fileName); OutputStream os = new FileOutputStream(new File(fileName)); InputStream in = item....
String t_name = path.substring(path.lastIndexOf("\\") + 1); System.out.println(t_name); // 得到文件的扩展名(无扩展名时将得到全名) String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1); // ...
string b = mediaElement.Position.ToString().Substring(0, 8); 当前转为计数器 string[] videotime = b.Split(':'); int totime = int.Parse(videotime[0]) * 3600 + int.Parse(videotime[1]) * 60 + int....
public void Ccreateconnect(String ip) { try { socket=new Socket(ip,9999);//创建套接字 } catch(Exception e) { System.out.print("创立连接失败!"); } } //管道通信 //通信内容 //1.文件接发...
字符串的相加,检索,替换,截取,插入,计算长度 private void button1_Click(object sender, EventArgs e) { string a = textBox1.Text.ToString();... textBox3.Text = a.Substring(b, c); }
return CallInfo.Substring(0,140); } } else { int error = Marshal.GetLastWin32Error(); return ""; } } catch (Exception ep) { //MessageBox.Show(ep.ToString()); return ""; } finally { } ...
byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); byte[] rgbIV = Keys; byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString); DESCryptoServiceProvider dCSP = new ...
2004.03.09 完成对<函数类>的查询功能,包括 代码调用,中文注释,所需的DLL库,与C#中函数对应关系 2004.03.10 0:48:52 完成了用StringBuilder数组对原ComboBox的替换,可以使程序不用从新读取数据库就可以刷新...
String path=entryName.substring(0, index); log("new Dir:"+rootDir+file.separator+path); new File(rootDir+file.separator+path).mkdirs(); log("entry:"+entryName.substring(0, index)); ...
public static String getChinese(String number, int depth) { if (depth ) { depth = 0; } String chs = ""; String src = number; if (src.length() > 4) { chs = getChinese(src.substring(0,...
实例90 如何使用substring()方法截取子串 118 实例91 分解字符串 119 实例92 字母大小写转换 120 实例93 去除多余的空白 120 实例94 原始数组类型的String形式 121 实例95 Java合法标识符 122 实例96 显示一周各星期...