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?

[349 byte] By [TheNewLeadera] at [2007-11-27 4:54:05]
# 1
What should 4 return?Kaj
kajbja at 2007-7-12 10:08:40 > top of Java-index,Java Essentials,Java Programming...
# 2

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

NourElsaftya at 2007-7-12 10:08:40 > top of Java-index,Java Essentials,Java Programming...
# 3
there is no 4 it has been written wrongly ;-)
TheNewLeadera at 2007-7-12 10:08:40 > top of Java-index,Java Essentials,Java Programming...
# 4
it should beKim/Name 3 london/Place 5 33/age 10
TheNewLeadera at 2007-7-12 10:08:40 > top of Java-index,Java Essentials,Java Programming...
# 5

> 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?

TheNewLeadera at 2007-7-12 10:08:40 > top of Java-index,Java Essentials,Java Programming...
# 6

> 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 > top of Java-index,Java Essentials,Java Programming...
# 7

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

NourElsaftya at 2007-7-12 10:08:40 > top of Java-index,Java Essentials,Java Programming...
# 8
@Op. change my regexp toString regexp = "([^\\s/]+/\\S+)\\s" + lookingFor; if you want to handle all types of space characters.Kaj
kajbja at 2007-7-12 10:08:40 > top of Java-index,Java Essentials,Java Programming...
# 9
Thanks kajbj and Ahmedi'll try your suggestion nowThanks agian
TheNewLeadera at 2007-7-12 10:08:40 > top of Java-index,Java Essentials,Java Programming...