SRL Archive - Java Help and Tutorials

How teh heck do I do between(S1,s2)....


ZaSz

2009-11-17 00:19:22

In java…

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

noidea

2009-11-17 00:34:50

I know, I've been looking, and ended up just using for loops :<

I will sex you if some one knows.

Nava2

2009-11-17 00:35:47

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

2009-11-17 00:47:21

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

2009-11-17 20:25:50

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

2009-11-17 21:23:48

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

}



From: Rasta Magician
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

2009-11-17 21:34:32

From: arash

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

2009-11-17 21:45:04

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.

From: Rasta Magician
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

2009-11-17 21:49:57

From: arash
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

2009-11-17 21:55:13

Sorry :sasmokin:

From: Rasta Magician
Ouch, that took a knife to my pride :(

~RM

noidea

2009-11-17 22:58:58

I think arash is hard core man :S