java实现查找替换功能
作者:cat_pp 发布时间:2021-12-15 00:46:50
标签:java,查找,替换
本文实例为大家分享了java实现查找替换功能的具体代码,供大家参考,具体内容如下
查找
if(searchTxt.getText().equals("")){
JOptionPane.showMessageDialog(null, "查找内容不能为空!");
}else if(!searchTxt.getText().equals("")){
//searchDialog.dispose();
if(fileChoose.focus == 1){
fileChoose.tp1.requestFocus(true);
str = fileChoose.tp1.getText();
startPosition = str.indexOf(searchTxt.getText());
endPosition = startPosition + searchTxt.getText().length();
fileChoose.tp1.select(startPosition, endPosition);
newEndPosition = endPosition;
}else if(fileChoose.focus == 2){
fileChoose.tp2.requestFocus(true);
str = fileChoose.tp2.getText();
startPosition = str.indexOf(searchTxt.getText());
endPosition = startPosition + searchTxt.getText().length();
fileChoose.tp2.select(startPosition, endPosition);
newEndPosition = endPosition;
}
}
查找下一处
nextTemp = newEndPosition;
if(fileChoose.focus == 1){
strAll = fileChoose.tp1.getText();
fileChoose.tp1.select(nextTemp, strAll.length());//选中所有未查找的字符串
nextStr = fileChoose.tp1.getSelectedText();
newStartPosition = nextStr.indexOf(searchTxt.getText()) + nextTemp;//在未查找的字符串中查找对应字符在tp1中的位置
newEndPosition = newStartPosition + searchTxt.getText().length();
fileChoose.tp1.select(newStartPosition, newEndPosition);//找到文本,选择文本
}else if(fileChoose.focus == 2){
strAll = fileChoose.tp2.getText();
fileChoose.tp2.select(nextTemp, strAll.length());//选中所有未查找的字符串
nextStr = fileChoose.tp2.getSelectedText();
newStartPosition = nextStr.indexOf(searchTxt.getText()) + nextTemp;//在未查找的字符串中查找对应字符在tp1中的位置
newEndPosition = newStartPosition + searchTxt.getText().length();
fileChoose.tp2.select(newStartPosition, newEndPosition);//找到文本,选择文本
}
替换
if(fileChoose.focus == 1){
fileChoose.tp1.requestFocus(true);
str = fileChoose.tp1.getText();
startPosition = str.indexOf(searchTxt.getText());
endPosition = startPosition + searchTxt.getText().length();
fileChoose.tp1.replaceSelection(replaceTxt.getText());
newEndPosition = endPosition;
}else if(fileChoose.focus == 2){
fileChoose.tp2.requestFocus(true);
str = fileChoose.tp2.getText();
startPosition = str.indexOf(searchTxt.getText());
endPosition = startPosition + searchTxt.getText().length();
fileChoose.tp2.replaceSelection(replaceTxt.getText());
newEndPosition = endPosition;
}
替换下一处
if(fileChoose.focus == 1){
fileChoose.tp1.requestFocus(true);
nextTemp = newEndPosition;
strAll = fileChoose.tp1.getText();////////////////////////////////
fileChoose.tp1.select(nextTemp, strAll.length());/////////////////////////////
nextStr = fileChoose.tp1.getSelectedText();
newStartPosition = nextStr.indexOf(searchTxt.getText()) + nextTemp;
newEndPosition = newStartPosition + searchTxt.getText().length();
fileChoose.tp1.select(newStartPosition, newEndPosition);
fileChoose.tp1.replaceSelection(replaceTxt.getText());
}else if(fileChoose.focus == 2){
fileChoose.tp2.requestFocus(true);
nextTemp = newEndPosition;
strAll = fileChoose.tp2.getText();
fileChoose.tp2.select(nextTemp,strAll.length());
nextStr = fileChoose.tp2.getSelectedText();
newStartPosition = nextStr.indexOf(searchTxt.getText()) + nextTemp;
newEndPosition =newStartPosition + searchTxt.getText().length();
fileChoose.tp2.select(newStartPosition, newEndPosition);
fileChoose.tp2.replaceSelection(replaceTxt.getText());
}
来源:https://blog.csdn.net/cat_pp/article/details/50963397


猜你喜欢
- 谈到多线程就不得不谈到Synchronized,重要性不言而喻,今天主要谈谈Synchronized的实现原理。Synchronizedsy
- 转成 Base64 形式的 System.String:string a = "base64字符串与普通字符串互转";
- 本文实例讲述了Android中Bitmap用法。分享给大家供大家参考,具体如下:一般在android程序中把图片文件放在res/drawab
- 今天一直在绞尽脑汁的寻找解决两个字符之间的内容如何输出的问题,刚开始就使用了万能的正则表达式;但是不知哪里的原因自己的数据一直出不来,觉得应
- 哈希表(HashMap)hash查询的时间复杂度是O(1)按值传递Character,Short,Integer,Long, Float,D
- 本文实例讲述了android编程实现悬浮窗体的方法。分享给大家供大家参考,具体如下:突然对悬浮窗体感兴趣,查资料做了个小Demo,效果是点击
- 简介本文用示例介绍java的Period的用法。Duration和Period说明Duration类通过秒和纳秒相结合来描述一个时间量,最高
- 在项目开发中,经常碰到map转实体对象或者对象转map的场景,工作中,很多时候我们可能比较喜欢使用第三方jar包的API对他们进行转化,而且
- 从主线程发送消息到子线程(准确地说应该是非UI线程)package com.zhuozhuo;import android.app.Acti
- 在实际应用中,很可能我们希望自己的app在按下返回键的时候并不退出,而是像按home键一样仅仅返回桌面,而程序仍然在后台运行着。要怎么实现这
- 本文研究的主要是Java中hashCode的正确求值方法的相关内容,具体如下。散列表有一项优化,可以将对象的散列码(hashCode)缓存起
- 目录MeasureSpecLayoutParamViewViewGroupTextColumn使用总结
- 一、题目描述题目实现:运行客户端,连接服务器。二、解题思路首先需要启动上题的ServerSocketFrame服务,这样客户端运行时,才能连
- 一)Document介绍API来源:在JDK中javax.xml.*包下使用场景:1、需要知道XML文档所有结构2、需要把文档一些元素排序3
- Input源码解读——从"Show tabs"开始本文基于Android T版本
- 泛型在继承方面的体现类A是类B的父类,G<A>和G<B>二者不具有子父类关系,二者是并列关系@Test &
- 本文纯干货,贴上PDF文档操作类C#代码,需要添加iTextSharp.dll引用才可以正常通过编译。废话不多说了,直接给大家贴代码了。代码
- 前言:图片加载涉及到图片的缓存、图片的处理、图片的显示等。四种常用的图片加载框架,分别是Fresco、ImageLoader、 Picass
- 生命太短暂,不要去做一些根本没有人想要的东西。本文已被 https://www.yourbatman.cn 收录,里面一并有Spring技术
- 本文实例讲述了C#实现控制Windows系统关机、重启和注销的方法。分享给大家供大家参考。具体分析如下:使用.NET和C#.NET,我们可以