0

I have a string and I want to use some formulas, in which there is a word that is going to be searched. Sometimes that word could be in different place, depending on the user. In order to make substrings work, I have to ensure that when it happens, the code still works.

 String temp = s;//Temp is a long string text       

     String fehlerspeicher="fehlerspeicher";
    if(temp.matches(".*"+fehlerspeicher+".*")){
    // i have to find as an integer, how many LETTERS are used till this spesific word
    }//to make changes in the following code
    String temp1=temp.substring(0, 15000);//15000 is an example. it can be 5000 or 20000 sometimes. It splits the text up from 15000th letter.
    String temp2=temp.substring(15000);// It'd be useful to use this integer in these 2 formulas.
    temp2=temp2.replaceFirst("200", "20_");
    temp=temp1+temp2;

So, could it be somehow implementable? Thanks.

  • You should use a formula parser library or even better, use JavaScript as shown here: [Algebra equation parser for java](http://stackoverflow.com/q/4681959/1065197). – Luiggi Mendoza May 06 '14 at 15:28
  • 2
    Maybe `indexOf()` combined with the length of `fehlerspeicher` will help you – Alexis C. May 06 '14 at 15:29
  • 1
    I did not get the part about the 15000 can you explain it more? – mosaad May 06 '14 at 15:34
  • @mosaad 15000 is an example. it can be 5000 or 20000 sometimes. It splits the text up from 15000th letter and then I use it in substring – regardless May 06 '14 at 15:56

1 Answers1

2

Use indexOf:

public int indexOf(String str)

Returns the index within this string of the first occurrence of the specified substring.

http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#indexOf(java.lang.String)

So to get the index of "fehlerspeicher" you can use:

int index = temp.indexOf("fehlerspeicher");

So to get the position of the 'f' in 'fehlerspeicher' you can use:

String temp = "hellofehlerspecher";    //example temp string
int index = temp.indexOf("fehlerspeicher");
int fPos = index + 1; //fpos will equal 6.
Matthew Wilson
  • 2,045
  • 14
  • 27