String strTemp ="C:\test\test1";替换为C:\\test\\test1
谢先了!
17 个解决方案
#1
strTemp = strTemp.replaceAll("\","\\");
#2
String strTemp ="C:\\test\\test1";
StringTokenizer stn = new StringTokenizer(dtrTemp,"\\");
String strTemp2 = "";
while(stn.hasMoreTokens())
{
strTemp2 += stn.nextToken() + "\\\\";
}
strTemp2 = strTemp2.substring(0,strTemp2.length() - 2);
StringTokenizer stn = new StringTokenizer(dtrTemp,"\\");
String strTemp2 = "";
while(stn.hasMoreTokens())
{
strTemp2 += stn.nextToken() + "\\\\";
}
strTemp2 = strTemp2.substring(0,strTemp2.length() - 2);
#3
public static String StrReplace(String rStr, String rFix, String rRep) {
int l = 0;
gRtnStr = rStr;
do
{
l = rStr.indexOf(rFix,l);
if(l == -1) break;
gRtnStr = rStr.substring(0,l) + rRep + rStr.substring(l + 1);
l += rRep.length();
rStr = gRtnStr;
}while(true);
return gRtnStr.substring(0, gRtnStr.length());
}
int l = 0;
gRtnStr = rStr;
do
{
l = rStr.indexOf(rFix,l);
if(l == -1) break;
gRtnStr = rStr.substring(0,l) + rRep + rStr.substring(l + 1);
l += rRep.length();
rStr = gRtnStr;
}while(true);
return gRtnStr.substring(0, gRtnStr.length());
}
#4
class convertString
{
public static void main(String[] args)
{
String s = "C:\\test\\test1";
StringBuffer sb = new StringBuffer( s );
String searchfor = "\\";
String replacefor = "\\\\";
int pos;
pos = s.indexOf(searchfor, 0);
while (pos>=0){
try {
sb.replace( pos, pos + 1, replacefor);
}
catch (Exception e) {
System.out.println("Exception: " + e.toString());
return;
}
pos += 2;
s = sb.toString();
pos = s.indexOf(searchfor, pos);
}
System.out.println(s);
}
}
{
public static void main(String[] args)
{
String s = "C:\\test\\test1";
StringBuffer sb = new StringBuffer( s );
String searchfor = "\\";
String replacefor = "\\\\";
int pos;
pos = s.indexOf(searchfor, 0);
while (pos>=0){
try {
sb.replace( pos, pos + 1, replacefor);
}
catch (Exception e) {
System.out.println("Exception: " + e.toString());
return;
}
pos += 2;
s = sb.toString();
pos = s.indexOf(searchfor, pos);
}
System.out.println(s);
}
}
#5
都试过么?在我这里怎么不能用呢?
#6
[JScript]
str=(str.split("\\")).join("\\\\");
str=(str.split("\\")).join("\\\\");
#7
再给你一个,赶快给分啊。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ConvertString
{
String convert(final String src){
String s = src;
StringBuffer sb = new StringBuffer( s );
String searchfor = "\\";
String replacefor = "\\\\";
int pos;
pos = s.indexOf(searchfor, 0);
while (pos>=0){
try {
sb.replace( pos, pos + 1, replacefor);
}
catch (Exception e) {
System.out.println("Exception: " + e.toString());
return "";
}
pos += 2;
s = sb.toString();
pos = s.indexOf(searchfor, pos);
}
return s;
}
Component createPane() {
final JTextField field = new JTextField(25);
//final JLabel label = new JLabel("");
JButton button = new JButton("Convert");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String tmp;
tmp = convert(field.getText());
field.setText(tmp);
}
});
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(
10,
10,
5,
10
));
panel.setLayout(new GridLayout(0,1));
panel.add(field);
panel.add(button);
//panel.add(label);
return panel;
}
public static void main(String[] args)
{
ConvertString app = new ConvertString();
Component component = app.createPane();
JFrame J = new JFrame();
J.getContentPane().add(component, BorderLayout.CENTER);
J.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
J.pack();
J.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ConvertString
{
String convert(final String src){
String s = src;
StringBuffer sb = new StringBuffer( s );
String searchfor = "\\";
String replacefor = "\\\\";
int pos;
pos = s.indexOf(searchfor, 0);
while (pos>=0){
try {
sb.replace( pos, pos + 1, replacefor);
}
catch (Exception e) {
System.out.println("Exception: " + e.toString());
return "";
}
pos += 2;
s = sb.toString();
pos = s.indexOf(searchfor, pos);
}
return s;
}
Component createPane() {
final JTextField field = new JTextField(25);
//final JLabel label = new JLabel("");
JButton button = new JButton("Convert");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String tmp;
tmp = convert(field.getText());
field.setText(tmp);
}
});
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(
10,
10,
5,
10
));
panel.setLayout(new GridLayout(0,1));
panel.add(field);
panel.add(button);
//panel.add(label);
return panel;
}
public static void main(String[] args)
{
ConvertString app = new ConvertString();
Component component = app.createPane();
JFrame J = new JFrame();
J.getContentPane().add(component, BorderLayout.CENTER);
J.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
J.pack();
J.setVisible(true);
}
}
#8
[JScript]
str=(str.split("\\")).join("\\\\");
str=(str.split("\\")).join("\\\\");
#9
oicu(阿猫)的办法简单,不过前面要加上“import java.util.*;”
#10
public class Test
{
public static void main(String args[])
{
String strTemp = "c:\test\test1";
strTemp =strTemp.replace("\","\\");
System.out.println("strTemp:"+strTemp);
}
}
我要的结果是:strTemp = c:\\test\\test1
{
public static void main(String args[])
{
String strTemp = "c:\test\test1";
strTemp =strTemp.replace("\","\\");
System.out.println("strTemp:"+strTemp);
}
}
我要的结果是:strTemp = c:\\test\\test1
#11
倒!编译通不过。
你用的是什么版本?
你用的是什么版本?
#12
我用的是1.3
#13
定义虚线
float dash1[] = {10.0f};
BasicStroke bs = new BasicStroke(5.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
g2.setStroke(bs);
Line2D line = new Line2D.Float(20.0f, 10.0f, 100.0f, 10.0f);
g2.draw(line);
//定义虚线
float[] dash2 = {6.0f, 4.0f, 2.0f, 4.0f, 2.0f, 4.0f};
bs = new BasicStroke(5.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, dash2, 0.0f);
g2.setStroke(bs);
g2.draw(line);
float dash1[] = {10.0f};
BasicStroke bs = new BasicStroke(5.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
g2.setStroke(bs);
Line2D line = new Line2D.Float(20.0f, 10.0f, 100.0f, 10.0f);
g2.draw(line);
//定义虚线
float[] dash2 = {6.0f, 4.0f, 2.0f, 4.0f, 2.0f, 4.0f};
bs = new BasicStroke(5.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, dash2, 0.0f);
g2.setStroke(bs);
g2.draw(line);
#14
import java.util.regex.*;
import java.lang.StringBuffer;
p = Pattern.compile("[^a");
m = p.matcher("whatya");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while(result)
{
deletedIllegalChars = true;
m.appendReplacement(sb, "b");
result = m.find();
}
// Add the last segment of input to the new String
m.appendTail(sb);
变成whbtyb,学过PERL吗?
import java.lang.StringBuffer;
p = Pattern.compile("[^a");
m = p.matcher("whatya");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while(result)
{
deletedIllegalChars = true;
m.appendReplacement(sb, "b");
result = m.find();
}
// Add the last segment of input to the new String
m.appendTail(sb);
变成whbtyb,学过PERL吗?
#15
import java.util.regex.*;
import java.lang.StringBuffer;
Pattern p = Pattern.compile("a");
Matcher m = p.matcher("whatya");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while(result)
{
deletedIllegalChars = true;
m.appendReplacement(sb, "b");
result = m.find();
}
// Add the last segment of input to the new String
m.appendTail(sb);
变成whbtyb,学过PERL吗?
import java.lang.StringBuffer;
Pattern p = Pattern.compile("a");
Matcher m = p.matcher("whatya");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while(result)
{
deletedIllegalChars = true;
m.appendReplacement(sb, "b");
result = m.find();
}
// Add the last segment of input to the new String
m.appendTail(sb);
变成whbtyb,学过PERL吗?
#16
import java.util.regex.*;
import java.lang.StringBuffer;
Pattern p = Pattern.compile("\\");
Matcher m = p.matcher("C:\test\test1");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while(result)
{
deletedIllegalChars = true;
m.appendReplacement(sb, "\\\\");
result = m.find();
}
// Add the last segment of input to the new String
m.appendTail(sb);
变成whbtyb,学过PERL吗?
import java.lang.StringBuffer;
Pattern p = Pattern.compile("\\");
Matcher m = p.matcher("C:\test\test1");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while(result)
{
deletedIllegalChars = true;
m.appendReplacement(sb, "\\\\");
result = m.find();
}
// Add the last segment of input to the new String
m.appendTail(sb);
变成whbtyb,学过PERL吗?
#17
奇怪,怎么没有我昨天晚上写的回复?
你遇到的问题是这样的,“\”是转义符,比如字符串"\n"表示回车,而"\\"只表示一个"\"。所以虽然表面上变量的赋值为"C:\\d\\e",实际的值却是"C:\d\e"。运行我写的第二个程序,在text field中输入"c:\d\e\f",按convert按钮,就可以得到"c:\\d\\e\\f"。
你遇到的问题是这样的,“\”是转义符,比如字符串"\n"表示回车,而"\\"只表示一个"\"。所以虽然表面上变量的赋值为"C:\\d\\e",实际的值却是"C:\d\e"。运行我写的第二个程序,在text field中输入"c:\d\e\f",按convert按钮,就可以得到"c:\\d\\e\\f"。
#1
strTemp = strTemp.replaceAll("\","\\");
#2
String strTemp ="C:\\test\\test1";
StringTokenizer stn = new StringTokenizer(dtrTemp,"\\");
String strTemp2 = "";
while(stn.hasMoreTokens())
{
strTemp2 += stn.nextToken() + "\\\\";
}
strTemp2 = strTemp2.substring(0,strTemp2.length() - 2);
StringTokenizer stn = new StringTokenizer(dtrTemp,"\\");
String strTemp2 = "";
while(stn.hasMoreTokens())
{
strTemp2 += stn.nextToken() + "\\\\";
}
strTemp2 = strTemp2.substring(0,strTemp2.length() - 2);
#3
public static String StrReplace(String rStr, String rFix, String rRep) {
int l = 0;
gRtnStr = rStr;
do
{
l = rStr.indexOf(rFix,l);
if(l == -1) break;
gRtnStr = rStr.substring(0,l) + rRep + rStr.substring(l + 1);
l += rRep.length();
rStr = gRtnStr;
}while(true);
return gRtnStr.substring(0, gRtnStr.length());
}
int l = 0;
gRtnStr = rStr;
do
{
l = rStr.indexOf(rFix,l);
if(l == -1) break;
gRtnStr = rStr.substring(0,l) + rRep + rStr.substring(l + 1);
l += rRep.length();
rStr = gRtnStr;
}while(true);
return gRtnStr.substring(0, gRtnStr.length());
}
#4
class convertString
{
public static void main(String[] args)
{
String s = "C:\\test\\test1";
StringBuffer sb = new StringBuffer( s );
String searchfor = "\\";
String replacefor = "\\\\";
int pos;
pos = s.indexOf(searchfor, 0);
while (pos>=0){
try {
sb.replace( pos, pos + 1, replacefor);
}
catch (Exception e) {
System.out.println("Exception: " + e.toString());
return;
}
pos += 2;
s = sb.toString();
pos = s.indexOf(searchfor, pos);
}
System.out.println(s);
}
}
{
public static void main(String[] args)
{
String s = "C:\\test\\test1";
StringBuffer sb = new StringBuffer( s );
String searchfor = "\\";
String replacefor = "\\\\";
int pos;
pos = s.indexOf(searchfor, 0);
while (pos>=0){
try {
sb.replace( pos, pos + 1, replacefor);
}
catch (Exception e) {
System.out.println("Exception: " + e.toString());
return;
}
pos += 2;
s = sb.toString();
pos = s.indexOf(searchfor, pos);
}
System.out.println(s);
}
}
#5
都试过么?在我这里怎么不能用呢?
#6
[JScript]
str=(str.split("\\")).join("\\\\");
str=(str.split("\\")).join("\\\\");
#7
再给你一个,赶快给分啊。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ConvertString
{
String convert(final String src){
String s = src;
StringBuffer sb = new StringBuffer( s );
String searchfor = "\\";
String replacefor = "\\\\";
int pos;
pos = s.indexOf(searchfor, 0);
while (pos>=0){
try {
sb.replace( pos, pos + 1, replacefor);
}
catch (Exception e) {
System.out.println("Exception: " + e.toString());
return "";
}
pos += 2;
s = sb.toString();
pos = s.indexOf(searchfor, pos);
}
return s;
}
Component createPane() {
final JTextField field = new JTextField(25);
//final JLabel label = new JLabel("");
JButton button = new JButton("Convert");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String tmp;
tmp = convert(field.getText());
field.setText(tmp);
}
});
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(
10,
10,
5,
10
));
panel.setLayout(new GridLayout(0,1));
panel.add(field);
panel.add(button);
//panel.add(label);
return panel;
}
public static void main(String[] args)
{
ConvertString app = new ConvertString();
Component component = app.createPane();
JFrame J = new JFrame();
J.getContentPane().add(component, BorderLayout.CENTER);
J.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
J.pack();
J.setVisible(true);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ConvertString
{
String convert(final String src){
String s = src;
StringBuffer sb = new StringBuffer( s );
String searchfor = "\\";
String replacefor = "\\\\";
int pos;
pos = s.indexOf(searchfor, 0);
while (pos>=0){
try {
sb.replace( pos, pos + 1, replacefor);
}
catch (Exception e) {
System.out.println("Exception: " + e.toString());
return "";
}
pos += 2;
s = sb.toString();
pos = s.indexOf(searchfor, pos);
}
return s;
}
Component createPane() {
final JTextField field = new JTextField(25);
//final JLabel label = new JLabel("");
JButton button = new JButton("Convert");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String tmp;
tmp = convert(field.getText());
field.setText(tmp);
}
});
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(
10,
10,
5,
10
));
panel.setLayout(new GridLayout(0,1));
panel.add(field);
panel.add(button);
//panel.add(label);
return panel;
}
public static void main(String[] args)
{
ConvertString app = new ConvertString();
Component component = app.createPane();
JFrame J = new JFrame();
J.getContentPane().add(component, BorderLayout.CENTER);
J.addWindowListener( new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
J.pack();
J.setVisible(true);
}
}
#8
[JScript]
str=(str.split("\\")).join("\\\\");
str=(str.split("\\")).join("\\\\");
#9
oicu(阿猫)的办法简单,不过前面要加上“import java.util.*;”
#10
public class Test
{
public static void main(String args[])
{
String strTemp = "c:\test\test1";
strTemp =strTemp.replace("\","\\");
System.out.println("strTemp:"+strTemp);
}
}
我要的结果是:strTemp = c:\\test\\test1
{
public static void main(String args[])
{
String strTemp = "c:\test\test1";
strTemp =strTemp.replace("\","\\");
System.out.println("strTemp:"+strTemp);
}
}
我要的结果是:strTemp = c:\\test\\test1
#11
倒!编译通不过。
你用的是什么版本?
你用的是什么版本?
#12
我用的是1.3
#13
定义虚线
float dash1[] = {10.0f};
BasicStroke bs = new BasicStroke(5.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
g2.setStroke(bs);
Line2D line = new Line2D.Float(20.0f, 10.0f, 100.0f, 10.0f);
g2.draw(line);
//定义虚线
float[] dash2 = {6.0f, 4.0f, 2.0f, 4.0f, 2.0f, 4.0f};
bs = new BasicStroke(5.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, dash2, 0.0f);
g2.setStroke(bs);
g2.draw(line);
float dash1[] = {10.0f};
BasicStroke bs = new BasicStroke(5.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, dash1, 0.0f);
g2.setStroke(bs);
Line2D line = new Line2D.Float(20.0f, 10.0f, 100.0f, 10.0f);
g2.draw(line);
//定义虚线
float[] dash2 = {6.0f, 4.0f, 2.0f, 4.0f, 2.0f, 4.0f};
bs = new BasicStroke(5.0f, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_MITER, 10.0f, dash2, 0.0f);
g2.setStroke(bs);
g2.draw(line);
#14
import java.util.regex.*;
import java.lang.StringBuffer;
p = Pattern.compile("[^a");
m = p.matcher("whatya");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while(result)
{
deletedIllegalChars = true;
m.appendReplacement(sb, "b");
result = m.find();
}
// Add the last segment of input to the new String
m.appendTail(sb);
变成whbtyb,学过PERL吗?
import java.lang.StringBuffer;
p = Pattern.compile("[^a");
m = p.matcher("whatya");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while(result)
{
deletedIllegalChars = true;
m.appendReplacement(sb, "b");
result = m.find();
}
// Add the last segment of input to the new String
m.appendTail(sb);
变成whbtyb,学过PERL吗?
#15
import java.util.regex.*;
import java.lang.StringBuffer;
Pattern p = Pattern.compile("a");
Matcher m = p.matcher("whatya");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while(result)
{
deletedIllegalChars = true;
m.appendReplacement(sb, "b");
result = m.find();
}
// Add the last segment of input to the new String
m.appendTail(sb);
变成whbtyb,学过PERL吗?
import java.lang.StringBuffer;
Pattern p = Pattern.compile("a");
Matcher m = p.matcher("whatya");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while(result)
{
deletedIllegalChars = true;
m.appendReplacement(sb, "b");
result = m.find();
}
// Add the last segment of input to the new String
m.appendTail(sb);
变成whbtyb,学过PERL吗?
#16
import java.util.regex.*;
import java.lang.StringBuffer;
Pattern p = Pattern.compile("\\");
Matcher m = p.matcher("C:\test\test1");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while(result)
{
deletedIllegalChars = true;
m.appendReplacement(sb, "\\\\");
result = m.find();
}
// Add the last segment of input to the new String
m.appendTail(sb);
变成whbtyb,学过PERL吗?
import java.lang.StringBuffer;
Pattern p = Pattern.compile("\\");
Matcher m = p.matcher("C:\test\test1");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;
while(result)
{
deletedIllegalChars = true;
m.appendReplacement(sb, "\\\\");
result = m.find();
}
// Add the last segment of input to the new String
m.appendTail(sb);
变成whbtyb,学过PERL吗?
#17
奇怪,怎么没有我昨天晚上写的回复?
你遇到的问题是这样的,“\”是转义符,比如字符串"\n"表示回车,而"\\"只表示一个"\"。所以虽然表面上变量的赋值为"C:\\d\\e",实际的值却是"C:\d\e"。运行我写的第二个程序,在text field中输入"c:\d\e\f",按convert按钮,就可以得到"c:\\d\\e\\f"。
你遇到的问题是这样的,“\”是转义符,比如字符串"\n"表示回车,而"\\"只表示一个"\"。所以虽然表面上变量的赋值为"C:\\d\\e",实际的值却是"C:\d\e"。运行我写的第二个程序,在text field中输入"c:\d\e\f",按convert按钮,就可以得到"c:\\d\\e\\f"。