请教哪位哥哥能帮我看看,我想将字符串数组里的"\"前加个转义字符"\"放到新的字符串数组里!但是遇到了一些问题在线等作答!妹子不能感激~~~

lady000 2012-03-31 09:19:30
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package t;
/**
*
* @author Administrator
*/
public class T {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String str;
str="d:\\数\\b\\c";
int m=str.length();
int n;
String strF[]=new String[m];
for(int i=0;i<str.length();i++)
{
strF[i]=str.substring(i, i+1);
}

for(int k=0;k<m;k++)
{

if(strF[k].equalsIgnoreCase("\\"))
{
n=m;
m+=1;


for(int j=n;j>k;j--)
{

strF[j]=strF[j-1];

}
k+=1;
}

}
for(int i=0;i<strF.length;i++){
System.out.print(strF[i]);
}
}
}
...全文
146 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
liutieniu87 2012-03-31
  • 打赏
  • 举报
回复
不好意思刚才在测试你代码的时候多写了点东西,请以下面代码我为参考

package com.inspur.date;

import java.util.ArrayList;
import java.util.List;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
public class T {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String str;
str="d:\\数\\b\\c";
int m=str.length();
int n;
List<String> strF = new ArrayList<String>();
for(int i=0;i<str.length();i++)
{
strF.add(str.substring(i, i+1));
}
for(int k=m-1;k>=0;k--)
{
if(strF.get(k).equalsIgnoreCase("\\"))
{
strF.add(k, "\\");
}
}
for(int i=0;i<strF.size();i++){
System.out.print(strF.get(i));
}
}
}
噢噢噢噢 2012-03-31
  • 打赏
  • 举报
回复
简单的问题复杂化,现在用手机明天回。
liutieniu87 2012-03-31
  • 打赏
  • 举报
回复
兄弟我大致明白你的意思
在回答你问题之前提两点建议(你犯的两点错误在上面代码中):
1:在java里面数组的长度是在以后是不可变的(确切的说是:通过 new 分配出的内存空间在以后的操作中是不可变的)
2:数组的下标是最大下标也就是你代码中的 m-1,不是 你写的j。

我在你的代码基础上做了修改,仅供参考

package com.inspur.date;

import java.util.ArrayList;
import java.util.List;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
public class T {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String str;
str="d:\\数\\b\\c";
int m=str.length();
int n;
List<String> strF = new ArrayList<String>();
for(int i=0;i<str.length();i++)
{
strF.add(str.substring(i, i+1));
}
for (int i = 0; i < strF.size(); i++) {
System.out.print(strF.get(i));
}
for(int k=m-1;k>=0;k--)
{
if(strF.get(k).equalsIgnoreCase("\\"))
{
strF.add(k, "\\");
}
}
for(int i=0;i<strF.size();i++){
System.out.print(strF.get(i));
}
}
}
lady000 2012-03-31
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

代码太乱,看不下去。 你直接说你想干嘛 ?
[/Quote]就是比如说我的字符串str得到了一个路径d:\a\b\c想让它转换成d:\\a\\b\\c再放过这个字符串str里
lady000 2012-03-31
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

public static void main(String[] args) {
StringBuffer []sb= new StringBuffer[2];
sb[0] =new StringBuffer("d:\\数\\b\\c");
sb[1] =new StringBuffer("");
sb[1]=sb[0].insert(sb[0].indexOf("\\"), "/"……
[/Quote]你做出的答案是d:/\数\b\c而我想让原来的数组修改成d:\\数\\b\\c
噢噢噢噢 2012-03-31
  • 打赏
  • 举报
回复
代码太乱,看不下去。 你直接说你想干嘛 ?
hjsdhsjhdjkhjheer 2012-03-31
  • 打赏
  • 举报
回复
public static void main(String[] args) {
StringBuffer []sb= new StringBuffer[2];
sb[0] =new StringBuffer("d:\\数\\b\\c");
sb[1] =new StringBuffer("");
sb[1]=sb[0].insert(sb[0].indexOf("\\"), "/");
System.out.println(sb[1]);
}

这个是增加的!。。
lady000 2012-03-31
  • 打赏
  • 举报
回复
妹子不想让它沉了~~~~~~
lady000 2012-03-31
  • 打赏
  • 举报
回复
是不是和数组的下标越界有关哦,就是在第3个for循环里
lady000 2012-03-31
  • 打赏
  • 举报
回复
自己先顶一下!!!比较急哦!!!

23,407

社区成员

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

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