如何在方法中得到调用者的对象

oQingBing 2013-02-22 12:52:45

public class Question {
public static void main(String[] args) {
String str = "abc";
//str.replaceAll("", "/") 的结果是 /a/b/c/
//我需要的是a/b/c
//不想为这个临时字符串起名字
//如
//tmpstr = str.replaceAll("","/");
//System.out.println(tmpstr.substring(1,tmpstr.length()));

//也不想做2遍替换
//如
// System.out.println(str.replaceAll("", "/").substring(1,str.replaceAll("", "/").length()-1));

//我想用一行java语句完成从abc 到 a/b/c的过程
System.out.println(str.replaceAll("", "/").substring(1, 【这里填什么】.length()-1));

}
}


其实也就是在一个方法中如何获取调用此方法的对象
当然在本类中, 可以用this获取,在其他类的语句中如何获取呢?

菜鸟提问,谢谢你的回答和管制。
...全文
420 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zqfddqr 2013-02-23
  • 打赏
  • 举报
回复
引用 7 楼 ljc19901124 的回复:
引用 6 楼 zqfddqr 的回复:少打个斜杠...没转义 System.out.println(str.replaceAll("\\B", "/")); 大神可以解释下吗?不理解啊,谢谢
/B 匹配非首末的位置 正好适用这个 具体你可以百度一下
ceclinux 2013-02-23
  • 打赏
  • 举报
回复
str.replaceAll("", "/").substring(1, str.length()-1)只是得到了另外一个String(a/b/c)对象的引用
ZZZ5512536 2013-02-23
  • 打赏
  • 举报
回复
引用 6 楼 zqfddqr 的回复:
少打个斜杠...没转义 System.out.println(str.replaceAll("\\B", "/"));
+10086
oQingBing 2013-02-23
  • 打赏
  • 举报
回复
哇,csdn的朋友真热心, 我昨天也和同学讨论了一下这个问题, 要是不想得起名字的话, 就更复杂了。 另外请问,有没有把 abc 变成 a/b/c 的更简单的方法? 明天就可以结贴了。
哈哈小李 2013-02-22
  • 打赏
  • 举报
回复
str=str.replaceAll("", "/"); System.out.println(str.substring(1, str.length()-1)); 为什么不能这样写str.replaceAll("", "/").substring(1, str.length()-1)的原因, 你得去了解一下String 在内存的存储
爱T 2013-02-22
  • 打赏
  • 举报
回复
引用 6 楼 zqfddqr 的回复:
少打个斜杠...没转义 System.out.println(str.replaceAll("\\B", "/"));
大神可以解释下吗?不理解啊,谢谢
zqfddqr 2013-02-22
  • 打赏
  • 举报
回复
少打个斜杠...没转义 System.out.println(str.replaceAll("\\B", "/"));
zqfddqr 2013-02-22
  • 打赏
  • 举报
回复
System.out.println(str.replaceAll("\B", "/"));
赏金--猎人 2013-02-22
  • 打赏
  • 举报
回复
str=str.replaceAll("", "/"); str=str.substring(1, str.length()-1; 同意二楼! lz真是神人呀,实际开发中,不会用到你说的这种方法的。
yongger520 2013-02-22
  • 打赏
  • 举报
回复
System.out.println(str.replaceAll("", "/").substring(1)); 就你的程序,这个可以满足要求
thomasLand 2013-02-22
  • 打赏
  • 举报
回复
str=str.replaceAll("", "/"); str=str.substring(1, str.length()-1;
oZouBianBian 2013-02-22
  • 打赏
  • 举报
回复
这个问题,是无解的。因为你问的要填写的那个地方的对象其实就是str.replaceAll("", "/")的结果,而你这儿的没有将这个对象给一个引用,而要去访问一个对象时要通过引用去的。这就好比一个人去问一个无名氏叫什么名字。 另外,估计你没有去验证,这里的this是不行的。 你做2遍的那种方法,可以得到正确的结果,但是你要知道,那两个str.replaceAll("", "/")并不是指的一个对象哦

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧