当前位置: 技术问答>java相关
如何从字符串中的某个字符替换为另一字符?
来源: 互联网 发布时间:2015-07-22
本文导语: 如何从字符串中的某个字符替换为另一字符?例如: String strTemp ="C:testtest1";替换为C:\test\test1 谢先了! | 奇怪,怎么没有我昨天晚上写的回复? 你遇到的问题是这样的,“”是转义符,比如字符串"...
如何从字符串中的某个字符替换为另一字符?例如:
String strTemp ="C:testtest1";替换为C:\test\test1
谢先了!
String strTemp ="C:testtest1";替换为C:\test\test1
谢先了!
|
奇怪,怎么没有我昨天晚上写的回复?
你遇到的问题是这样的,“”是转义符,比如字符串"n"表示回车,而"\"只表示一个""。所以虽然表面上变量的赋值为"C:\d\e",实际的值却是"C:de"。运行我写的第二个程序,在text field中输入"c:def",按convert按钮,就可以得到"c:\d\e\f"。
你遇到的问题是这样的,“”是转义符,比如字符串"n"表示回车,而"\"只表示一个""。所以虽然表面上变量的赋值为"C:\d\e",实际的值却是"C:de"。运行我写的第二个程序,在text field中输入"c:def",按convert按钮,就可以得到"c:\d\e\f"。
|
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);
|
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());
}
|
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);
}
}
|
再给你一个,赶快给分啊。
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);
}
}
|
定义虚线
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);
|
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吗?
|
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吗?
|
import java.util.regex.*;
import java.lang.StringBuffer;
Pattern p = Pattern.compile("\");
Matcher m = p.matcher("C:testtest1");
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:testtest1");
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吗?