PDA

View Full Version : How teh heck do I do between(S1,s2)....



ZaSz
11-17-2009, 05:19 AM
In java...

I want a procedure that would return text between the 2 strings like this between("<col=800000>","<col=000080>");

noidea
11-17-2009, 05:34 AM
I know, I've been looking, and ended up just using for loops :<

I will sex you if some one knows.

Nava2
11-17-2009, 05:35 AM
Check out this.. it might help..
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

E: added the link :<

edit again..

public String between(String s1, String s2) {
int sPos = indexOf(s1);
int ePos = indexOf(s2, sPos);
return substring(sPos, ePos);
}

I didn't add any error handling, you might want to work with that.

ZaSz
11-17-2009, 05:47 AM
err... Dang, i did my own ;(

also, yours doesn't use the main string to get the substring, so no worky.

pretty sure this works...


public String between(String main, String s1, String s2) {
if(main.contains(s1)&& main.contains(s2)) {
int string1lastchar,string2firstchar;
string1lastchar = main.lastIndexOf(s1);
string2firstchar = main.indexOf(s2);
return main.substring(string1lastchar,string2firstchar);
}
return null;
}

Sir R. M8gic1an
11-18-2009, 01:25 AM
you could use String.split with a limit and then use it a second time for the second substring.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#split%28java.lang.String,%20int%29


pseudo code
- Array = String.split("first thingy", 2);
- Result = Array[1].split("second thingy", 2);
- return Result[0];

~RM

arash
11-18-2009, 02:23 AM
Correct Code because some people are idiots



public String between(String main,String s1, String s2) {

int length=s1.length();
int position1=main.indexOf(s1)+length;

return main.substring(position1,main.indexOf(s2,position1 ));

}





you could use String.split with a limit and then use it a second time for the second substring.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#split%28java.lang.String,%20int%29


pseudo code
- Array = String.split("first thingy", 2);
- Result = Array[1].split("second thingy", 2);
- return Result[0];

~RM

Sir R. M8gic1an
11-18-2009, 02:34 AM
Your suggestion teaches bad programming habits Rasta Magician, split is by no means used for the situation here. It is wholly inefficient, uses more system resources, and has a much slower run time.

Heh, I by no means consider myself a Java Programmer as I just started learning it, but if i were cornered to do it, that's the way I instinctively found :p

~RM

arash
11-18-2009, 02:45 AM
Instinctively yes, but if you just tear out methods out of the API because their the easiest, that is bad programming style period regardless of language. Programming teaches you to disregard instinct and think more in terms of logic and efficiency.


Heh, I by no means consider myself a Java Programmer as I just started learning it, but if i were cornered to do it, that's the way I instinctively found :p

~RM

Sir R. M8gic1an
11-18-2009, 02:49 AM
Instinctively yes, but if you just tear out methods out of the API because their the easiest, that is bad programming style period regardless of language. Programming teaches you to disregard instinct and think more in terms of logic and efficiency.

Ouch, that took a knife to my pride :(

~RM

arash
11-18-2009, 02:55 AM
Sorry :sasmokin:


Ouch, that took a knife to my pride :(

~RM

noidea
11-18-2009, 03:58 AM
I think arash is hard core man :S