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.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.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吗?
StringTokenizer stn = new StringTokenizer(dtrTemp,"\\");
String strTemp2 = "";
while(stn.hasMoreTokens())
{
strTemp2 += stn.nextToken() + "\\\\";
}
strTemp2 = strTemp2.substring(0,strTemp2.length() - 2);
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());
}
{
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); }}
str=(str.split("\\")).join("\\\\");
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);
}
}
str=(str.split("\\")).join("\\\\");
{
public static void main(String args[])
{
String strTemp = "c:\test\test1";
strTemp =strTemp.replace("\","\\");
System.out.println("strTemp:"+strTemp);
}
}我要的结果是:strTemp = c:\\test\\test1
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.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; 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("\\");
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吗?