gpt4 book ai didi

java - 在 word 中突出显示子字符串

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:27:40 24 4
gpt4 key购买 nike

我想使用 java 代码在 word 文档中突出显示字符串模式。我已经确定了 java 中的模式,现在我想在 word 文档中突出显示该模式。我想在word文件中写入字符串并突出显示字符串中的模式。

我想要这样的输出。

enter image description here

最佳答案

莎拉,下面应该做的是获取你的字符串,获取我们想要着色的单词的索引点(像以前一样),然后它将创建一个新的 ArrayList,其中将包含你的字符串的字母加上我们的单词想上色。例如,如果您的字符串是“abi​​gdog”,并且您想要将颜色变大,我制作了一个 ArrayList,如下所示:

element1: a
element2: big
element3: d
element4: o
element5: g

在创建角色运行时,我们遍历 ArrayList,根据需要为相关元素/单词着色。

public class SO4 {
public static void main(String[] args) {

String words = "adaahhhccadcdaccaaddadddcchah"; //String from doc
String[] wordsToColour = {"ahhh", "acca", "adda", "hah" }; //Words we want to find
ArrayList<Integer> points = new ArrayList<Integer>(); //Hold indexes of words

//Get the points of the words to colour first and add them to points arrayList
int j, k =0, count = 0;
String checker = "";
while(count < wordsToColour.length){ //For each search word
j = 0; //start of search word
k = wordsToColour[count].length(); //end of current search word
while(k < words.length() + 1){ //we will search whole string
checker = words.substring(j, k); //String equal to a substring the same length as the word we are searching for
if(checker.equals(wordsToColour[count])){ //If we find our word
points.add(Integer.valueOf(j)); //Store co-ordinates in arrayList, co-ordinate being the index at which the word is found
points.add(Integer.valueOf(k)); //Store co-ordinates in arrayList co-ordinate being the index at which the word is found
}
j++;
k++;
}//Inner loop
count++;
}//Outer loop

//Checking the arrayList to see if it contains our words.
/* k=0; j=1;
while(j < points.size()){
System.out.println(points.get(k) + " " + points.get(j) + " Word: " + words.substring(points.get(k), points.get(j)));
k = (k+2);
j = (j + 2);
} */


//This will hold a reference to either a letter or a word to colour
ArrayList<String> wordsByElement = new ArrayList<String>(words.length());
k = 0; j = 0;
String storage; //Will hold reference to current substring
while(j < words.length()){
storage = words.substring(j, j+1); //Will iterate over each letter
if(j == points.get(k)){ //If we hit a point stored in points array
//Use points in array to add substring word to wordsByElement ArrayList
wordsByElement.add(words.substring(points.get(k), points.get(k+1))); //Adding
j = points.get(k + 1) -1; //Set j to continue at the point after the word we just extracted
k = (k + 2); //raise k to start point of next word we want
}/*Inner if */ else { //If we didn't get a match
wordsByElement.add(storage); //add the letter
}
j++;
}//While loop end


//This shows what is in the ArrayList and should show what I have done to this point
for(String s : wordsByElement){
System.out.println(s); //Checking what is in our ArrayList
}

XWPFDocument newDoc = new XWPFDocument(); //Doc to write new doc to
XWPFParagraph para = newDoc.createParagraph(); //Paragraph
XWPFRun run = para.createRun(); //Where the text will be written from
k=0;

while(k < wordsByElement.size()){
run = para.createRun();
switch (wordsByElement.get(k)) {
case "ahhh": //If word is ahhh
run.setColor("ff5400"); //Set Color
break;
case "acca": //If word is acca
run.setColor("a7bf42"); //set color
break;
case "adda": //So on
run.setColor("7b8896"); //so forth
break;
case "hah":
run.setColor("00adee");
break;
default: //If word is not one of the above
run.setColor("000000"); //Color is black
break;
}

run.setText((wordsByElement.get(k)));
k++;
}

try {
newDoc.write(new FileOutputStream(new File("D:\\Users\\user277005\\Desktop\\testerFinished.docx")));

//View our document
if(Desktop.isDesktopSupported()){
Desktop.getDesktop().open(new File("D:\\Users\\user277005\\Desktop\\testerFinished.docx"));
}

} catch (IOException e) {
e.printStackTrace();
} //save

}
}

祝你好运!

关于java - 在 word 中突出显示子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19357322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com