「java替换/」java替换字符串中的某个字符串

博主:adminadmin 2023-01-25 19:09:09 52

本篇文章给大家谈谈java替换/,以及java替换字符串中的某个字符串对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java 替换文件内容

代码如下:

/***

* 方法:

* @Title: replaceContentToFile

* @Description: TODO

* @param @param path 文件

* @param @param str 开始删除的字符

* @param @param con 追加的文本

* @return void 返回类型

* @throws

*/

public static void replaceContentToFile(String path, String str ,String con){

try {

FileReader read = new FileReader(path);

BufferedReader br = new BufferedReader(read);

StringBuilder content = new StringBuilder();

while(br.ready() != false){

content.append(br.readLine());

content.append("\r\n");

}

System.out.println(content.toString());

int dex = content.indexOf(str);

if( dex != -1){

System.out.println(content.substring(dex, content.length()));

content.delete(dex, content.length());

}

content.append(con);

br.close();

read.close();

FileOutputStream fs = new FileOutputStream(path);

fs.write(content.toString().getBytes());

fs.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e){

e.printStackTrace();

}

}

java中怎么把“||”符合替换为“&”,我用.replaceAll("||", "&")替换,结果都给替换了

replaceAll()方法就是用指定的字符串来代替原有的,肯定是都替换掉了

JAVA 如何string替换指定字符

JAVA String替换指定字符有两个方法:

//返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的

public String replace(char oldChar,char newChar)

//示例

String str="Hello World";

System.out.println( str.replace( 'H','W' ) );//输出Wello World

//使用给定的 replacement 字符串替换此字符串匹配给定的正则表达式的每个子字符串。

public String replaceAll(String regex,String replacement)

示例:

String str="Hello World";

System.out.println( str.replaceAll( 'l','w' ) );//输出Hewwo Worwd

JAVA怎么替换字符串中的符号,如;:00594510=2609013500152=5801?

java中的字符串替换应该用字符串自带的方法replace或者replceAll,但这里要用replaceAll,因为要替换多个符号,只有replaceAll可以接受正则表达式

replaceAll接受两个参数,第一个是正则表示,第二个是要替换后的字符

示例代码如下:

public static void main(String args[]) {

    String s = ";:00594510=2609013500152=5801?";

    System.out.println(s.replaceAll(";|:|=|\\?",""));

}

运行结果如下

用java编写记事本程序,如何实现查找,替换

if(p.getActionCommand()=="查找 "||p.getSource()==findc) // 创建查找对话框;

{find=new Dialog(this,"查找");

JPanel p1=new JPanel();

JPanel p2=new JPanel();

findtxt=new JTextField(7);

p1.add(new JLabel("输入要查找字符:"));

p1.add(findtxt);

p2.add(findenter);

find.add("Center",p1);

find.add("South",p2);

find.setSize(200,100);

find.show();

}

if(p.getSource()==findenter) ///点击查找对话框的确定按钮后的事件处理

{

if(findtxt.getText().equals(""))

{find.dispose();}

else if(!findtxt.getText().equals(""))

{ find.dispose();

str=txt1.getText();

startp=str.indexOf(findtxt.getText());

endp=startp+findtxt.getText().length();

txt1.select(startp,endp);

m26.setEnabled(true);

newendp=endp;////////获取这次查找的终点

notfindmethod();

}

}

//////////////////////////////////////////////////////////

if(p.getActionCommand()=="查找下一个 ")

{

nexttemp=newendp; /////获取上次查找的终点做为未查找字符串的起点

String strall=txt1.getText();

txt1.select(nexttemp,strall.length()); /////选中所有未查找的字符串

strnext=txt1.getSelectedText();

newstartp=strnext.indexOf(findtxt.getText())+nexttemp;/////在未查找的字符串里搜索对应字符的在TXT1中的位置

newendp=newstartp+findtxt.getText().length();

txt1.select(newstartp,newendp); ////找到相应文本,并选择

notfindmethod();

}

if(p.getActionCommand()=="替换 ")

{m271.setEnabled(true);

replace=new Dialog(this,"替换"); // 创建替换对话框;

JPanel p1=new JPanel();

JPanel p2=new JPanel();

JPanel p3=new JPanel();

findtxt=new JTextField(7);

repltxt=new JTextField(7);

p1.add(new JLabel("输入原始字符:"));

p1.add(findtxt);

p2.add(new JLabel("输入替换字符:"));

p2.add(repltxt);

p3.add(replb);

replace.add("North",p1);

replace.add("Center",p2);

replace.add("South",p3);

replace.setSize(200,150);

replace.show();

}

if(p.getSource()==replb) ///点击替换对话框的确定按钮后的事件处理

{

replace.dispose();

str=txt1.getText();

startp=str.indexOf(findtxt.getText());

endp=startp+findtxt.getText().length();

txt1.replaceRange(repltxt.getText(),startp,endp);

newendp=endp; ///获取这次替换的终点

}

if(p.getActionCommand()=="替换下一个")

{nexttemp=newendp; /////获取上次查找的终点做为未查找字符串的起点

String strall=txt1.getText();

txt1.select(nexttemp,strall.length()); /////选中所有未查找的字符串

strnext=txt1.getSelectedText();

newstartp=strnext.indexOf(findtxt.getText())+nexttemp;/////在未查找的字符串里搜索对应字符的在TXT1中的位置

newendp=newstartp+findtxt.getText().length();

txt1.select(newstartp,newendp);

notfindmethod();

txt1.replaceRange(repltxt.getText(),newstartp,newendp);//替换字符

}

if(p.getActionCommand()=="全选 ")

{txt1.selectAll(); }

要看全部代码,我空间里有,自己看

jsp如何替换转义字符"\"

jsp中使用#47替换转义字符"\".实例如下:

html

head

title我的第一个 HTML 页面/title

/head

body

pbody 这是测试转义字符的例子,#47。/p

ptitle 这是测试转义字符的例子,#47。/p

/body

/html

运行结果如下:

关于java替换/和java替换字符串中的某个字符串的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2023-01-25,除非注明,否则均为首码项目网原创文章,转载请注明出处。