問題是這樣的,我試圖找到並得到一個字符串的唯一出現,當我能得到一個字符串的唯一方法是通過使用關鍵字發生多次。如何在字符串中找到特定字符串的出現
Ex。 4土豆,4(字符串我希望),4家,4車
如何只得到我想要的字符串,當我不能鍵入該字符串可能包含的關鍵字。
想象一下,它試圖從一篇短文中只取出一段。
我試過了stringy.replaceAll(Str1,Str2);變量,但無濟於事。所發生的一切是我替換所有的字符串(去圖一個名稱,如全部更換)
package com.donovan.cunningham;
import java.util.ArrayList;
import java.util.Random;
public class EssayCreator {
//Creating varz
private static String[] lf = {"happy", "sad", "unhappy", "atractive",
"fast", "lazy"};
private static String[] op = {"estatic", "melhencohly", "depressed",
"alluring", "swift", "lackadaisical"};
private static String pF = " ";
private static String temp[];
private static String conv = " ";
private static String comm = ", ";
private static Random random = new Random();
private ArrayList<String> array = new ArrayList<String>();
public static void Converter(String in) {
in = in.replace(comm, conv);
for (int i = 0; i < lf.length; i++){
in = in.replace(lf[i], op[i]);
}
in = in.replace(conv, comm);
//int rand = random.nextInt(in.indexOf(pF));
for (int i = 0; i < in.indexOf(pF); i++){
/*
Where I want to get an exact string of an essay
I'd convert pF to conv, and then remove the paragraph to
change the order
} */
}
CreateGUI.output.setText(in);
Sound.stopSound();
}
}
感謝
我很困惑你的問題。你想介紹一下你想做什麼的例子嗎?這聽起來像你正在尋找正則表達式。他們將允許您搜索字符串中的模式。你可以看看Matcher類的java。我相信有一種名爲replaceFirst的方法可能會讓你在正確的路徑上 – pythonHelpRequired
使用String indexOf找到第二個「4」。找到第三個「4」。你的子串在第一個位置(第二個「4」)和第三個位置之間。作爲替代方法,在逗號+空格處拆分字符串,然後從第二部分獲取您的子字符串。 –
你的詢問究竟是什麼,你能更清楚嗎? – jcool