how to take all the prevoius until space or tab
suppose i have the following
Kim/Name 34 london/Place 5 33/age 10
suppose i ask the user to enter a number then i'll retreive all the data before that number for example
if the user enter 3 i'll return K im/Name
if the user enter 10 i'll return 33/age
Where there is 1space between the number and the words?
you can use String class methods indexOf() and substring()
String s = "Kim/Name 3 4 london/Place 5 33/age 10";
String result = s.substring(0, s.indexOf(<your-string>));
or you can use Regular Expressions to search your string and decide the place content of your searched Pattern.
Good Luck.
Ahmad Elsafty
> you can use String class methods indexOf() and
> substring()
>
> String s = "Kim/Name 3 4 london/Place 5 33/age
> 10";
> String result = s.substring(0,
> s.indexOf(<your-string>));
>
> or you can use Regular Expressions to search
> your string and decide the place content of your
> searched Pattern.
> Good Luck.
> Ahmad Elsafty
should'nt the indexOf be for the integer that the user insert
how can regular Expression be used?
> it should be
> Kim/Name 3 london/Place 5 33/age 10
class Test {
public static void main(String[] args) {
String data = "Kim/Name 3 london/Place 5 33/age 10";
String lookingFor = "5";
String regexp = "([^ /]+/[^ ]+) " + lookingFor;
Matcher matcher = Pattern.compile(regexp).matcher(data);
if (matcher.find()) {
System.out.println(matcher.group(1));
} else {
System.out.println("No match");
}
}
}
Kaj
kajbja at 2007-7-12 10:08:40 >

String source = // your source string..........
String str = // what your user inputs......
Pattern p = Pattern.compile(str);
Matcher m = p.matcher(source);
then you can use many methods in Matcher class like find(), group(), and so........
Pattern docs page:
http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html
Matcher docs page:
http://java.sun.com/javase/6/docs/api/java/util/regex/Matcher.html
Good Luck
Ahmad Elsafty