2016-06-09 81 views
-1

問題是這樣的,我試圖找到並得到一個字符串的唯一出現,當我能得到一個字符串的唯一方法是通過使用關鍵字發生多次。如何在字符串中找到特定字符串的出現

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(); 
    } 
} 

感謝

+1

我很困惑你的問題。你想介紹一下你想做什麼的例子嗎?這聽起來像你正在尋找正則表達式。他們將允許您搜索字符串中的模式。你可以看看Matcher類的java。我相信有一種名爲replaceFirst的方法可能會讓你在正確的路徑上 – pythonHelpRequired

+0

使用String indexOf找到第二個「4」。找到第三個「4」。你的子串在第一個位置(第二個「4」)和第三個位置之間。作爲替代方法,在逗號+空格處拆分字符串,然後從第二部分獲取您的子字符串。 –

+0

你的詢問究竟是什麼,你能更清楚嗎? – jcool

回答

0

你的問題不是很清楚,但。您希望(1)找到您不知道的或您想要的最常出現的字符串(2)來替換您知道的出現字符串?

最天真的做法(1)是將文本按空格分割並將它們放入字符串到整數的HashMap中以計算出現頻率最高的字符串。你也可以掃描這個HashMap來查找所有的N次發生字符串

對於(2),假設你已經知道你想找到哪個鍵字符串,你可以在字符串中遞歸地應用indexOf(String str,int fromIndex)如下:

int occurenceCount = 0; 
String input = "Here is your text with key_word1, key_word2, ...etc"; 
StringBuffer output = new StringBuffer(); 
int index = input.indexOf("key_word"); 
int copiedIndex = 0; 

for(index>0) 
{ 
    output.append(input.substring(copiedIndex, index)); 
    occurenceCount++; 
    if(occurenceCount==4) //Find 4th occurrence and replaced it with "new_key_word" 
    { 
    output.append("new_key_word") 
    } 
    else 
    { 
    output.append("key_word") 
    } 
    copiedIndex = index+("key_word".length); 
    index = input.indexOf("key_word", index+("key_word".length)); 
    if(index==-1) 
    break; 
} 

編號:https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String,%20int)

不知道我是否已經回答雖然你的問題...

+0

沒有真正的幫助,但感謝您的嘗試 –

0

我通過您的代碼梳理,並試圖使用案例我做運行它從你給出的神祕信息。正如上面的人們所說,目前還不清楚你想要完成什麼。通常,如果String的內置方法不夠,則嘗試執行更復雜的字符串模式匹配時,正則表達式是您的朋友。嘗試谷歌搜索'模式匹配字符串段正則表達式java'或什麼的效果。同時,我在代碼中添加了一些註釋,這可能有助於使問題更清楚。如果我能更好地理解你想要做的事,很樂意提供幫助。請參閱下面的代碼和評論:

import java.util.ArrayList; 
import java.util.List; 
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 List<String> array = new ArrayList<String>(); 

    // Bradley D: Just some side notes here: 
    // Don't capitalize method names and don't use nouns. 
    // They're not class names or constructors. Changed Converter to convert. It'd also be good to 
    // stipulate what you are converting, i.e. convertMyString to make this a 
    // little more intuitive 
    public static void convert(String in) { 
     /* 
     * Bradley D: First, you are replacing all commas following by a space 
     * with 3 spaces. Be good to know why you doin that? 
     */ 
     in = in.replace(comm, conv); 
     for (int i = 0; i < lf.length; i++) { 
      in = in.replace(lf[i], op[i]); 
     } 
     /* 
     * Bradley D: Now you are replacing the 3 spaces with a comma and a 
     * space again?? 
     */ 
     in = in.replace(conv, comm); 

     // Bradley D: Not really sure what you are trying to iterate through 
     // here. in.indexOf(pF) is -1 
     // for the use case I've created for you with the text below (what did I 
     // miss?)... 
     // Perhaps you're trying to find the first place in your essay where pF 
     // (3 spaces) occurs.... 
     // but you've already reconverted your 3 spaces back to a comma and 
     // single space, so I'm getting even more lost here.... 
     // int rand = random.nextInt(in.indexOf(pF)); 
     for (int i = 0; i < in.indexOf(pF); i++) { 
      /* 
      * Bradley D: What is pF?? It appears to be the same as comm... 
      */ 
      /* 
      * 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 } 
      */ 

      // Bradley D: Ok, so if you can make this question clear, I'll give it a shot here 
     } 
     // Bradley D: Commenting this out since it was not included 
     // CreateGUI.output.setText(in); 
     // Sound.stopSound(); 
    } 

    public static void main(String[] args) { 
     EssayCreator ec = new EssayCreator(); 
     String essay = "Let's see if we find the desired string in here. " 
      + "Are we happy? Nope, we're not happy. Who's happy? What does happiness mean anyway? " 
      + "I'd be very happy if this question we're more clear, but let's give it a go anyway. Maybe " 
      + "we're lazy, and that's not attractive, thus rendering us unhappy and lackadasical... jk! " 
      + "So hey man... why are you replacing all of the commas with spaces? " 
      + "Can you put comments in your code? What is pF? " 
      + "Also you should not capitalize method names. They should be in camelCase and they should not " 
      + "be nouns like Converter, which makes them look like a constructor. Methods represent " 
      + "an action taken, so a verb to describe them is standard practice. " 
      + "So use convert, but what are you converting? convertString?? convertWords? " 
      + "Anyway, making your method names intuitive would be helpful to anyone trying to understand " 
      + "the code."; 
     ec.convert(essay); 
    } 
} 
相關問題