How to get substring

Say i have a string as follows:

property typename ttl=10 cval=30 test myprogram

Now i am trying to get ttl value i.e 10. I am just confused how to use substring() method or if it eases with regex (i dont know how to use it for this case)

It would be helpful if someone can hint me to solve this problem.

[328 byte] By [ArpanaKa] at [2007-11-27 10:11:02]
# 1

you can read this:

http://mindprod.com/jgloss/substring.html

Yannixa at 2007-7-28 15:10:24 > top of Java-index,Java Essentials,Java Programming...
# 2

Without regex, you can find the position of the value with the help of the String.indexOf(String) and String.indexOf(String, int) methods (in case where property typename is of variable length.)

Then, you can in fact extract the value with String.substring(int, int).

- http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

With regex, you might use an expression that matches the non-whitespace characters that follow "ttl="

You could also use an expression that allows you to extract all the key=value pairs.

- http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html

- http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Matcher.html

TimTheEnchantora at 2007-7-28 15:10:24 > top of Java-index,Java Essentials,Java Programming...
# 3

Hi,

Whether the particular text is a pattern (i.e., it will remain same as it is expect the values) or one-time string. Please let me know..

Regards,

Loga

Loga_07a at 2007-7-28 15:10:24 > top of Java-index,Java Essentials,Java Programming...
# 4

> you can read this:

>

> http://mindprod.com/jgloss/substring.html

I know how to use substring. But i am confused how to retrieve the value 10 from that string.

say, my string variable is str,

str.substring(str.indexOf("ttl"), ?) // what have to come in ? place

ArpanaKa at 2007-7-28 15:10:24 > top of Java-index,Java Essentials,Java Programming...
# 5

String str = "property typename ttl=10 cval=30 test myprogram";

System.out.println( str.substring(str.indexOf("10"),str.indexOf("c")).trim() );

display 10.

Yannixa at 2007-7-28 15:10:24 > top of Java-index,Java Essentials,Java Programming...
# 6

> String str = "property typename ttl=10 cval=30 test myprogram";

>

> System.out.println(str.substring(str.indexOf("10"),str.indexOf("c")).trim() );

> display 10.

So do:System.out.println("10");

Now, without knowing the value in advance, you could try something like :int startIndex = input.indexOf("ttl=")+4; // index of the char that follows "ttl=" (i.e. the first char of the value)

int endIndex = input.indexOf(" ", startIndex); // index of the whitespace following the value

String value = input.substring(startIndex, endIndex);

TimTheEnchantora at 2007-7-28 15:10:24 > top of Java-index,Java Essentials,Java Programming...
# 7

s/he only ask to display 10... ^_^

s/he could also try to use StringTokenizer to separate ttl = 10.

Yannixa at 2007-7-28 15:10:24 > top of Java-index,Java Essentials,Java Programming...
# 8

> s/he only ask to display 10... ^_^

So my (first) solution still applies...

> s/he could also try to use StringTokenizer to

> separate ttl = 10.

I would probably go with a regex anyway:Matcher m = Pattern.compile("(?<=ttl=)(\\S+)").matcher(input);

if(m.find()) {

System.out.println("'"+m.group()+"'");

}

orMatcher m = Pattern.compile("(\\S+)=(\\S+)").matcher(input);

while(m.find()) {

System.out.println(m.group(1) + " = '" + m.group(2) + "'");

}

TimTheEnchantora at 2007-7-28 15:10:24 > top of Java-index,Java Essentials,Java Programming...
# 9

Hi,

The following program will extract the value (it will work for both +ve as well as -ve numbers). I used the regex patterns.

public class SplitStringTest {

private static final String STRING_SPLIT = "property typename ttl=10 cval=30 test myprogram";

/**

* @param args

* loganathank

* void

*

*/

public static void main(String[] args) {

String ttlRegExPattern = ".*ttl\\p{Blank}*=\\p{Blank}*-*\\p{Digit}+.*";

if(STRING_SPLIT.matches(ttlRegExPattern)) {

System.out.println("ttl found in the given string");

//ttl is found in the given string

//now extract the ttl.

String ttlExtractRegExPattern = ".*ttl\\p{Blank}*=\\p{Blank}*";

String[] splitString = STRING_SPLIT.split(ttlExtractRegExPattern);

//The first String will be blank

//So directly fetch the second string

String excludeTTL = splitString[1];

//After getting the second string, split the string based on blank spaces

//get the first element, that will give the value

String ttlValueExtractRegExPattern = "\\p{Blank}";

String[] splitString1 = excludeTTL.split(ttlValueExtractRegExPattern);

System.out.println(" The Value of ttl in the given String => " + splitString1[0]);

}

}

}

Please have a look into it.

Regards,

Loga

Loga_07a at 2007-7-28 15:10:24 > top of Java-index,Java Essentials,Java Programming...
# 10

> Please have a look into it.

I'm not sure I should comment on your approach, but.. nah... nevermind.

TimTheEnchantora at 2007-7-28 15:10:24 > top of Java-index,Java Essentials,Java Programming...
# 11

No probs !!! you can comment on my approach.

Regards,

Loga

Loga_07a at 2007-7-28 15:10:24 > top of Java-index,Java Essentials,Java Programming...
# 12

> No probs !!! you can comment on my approach.

Basically, I would say it is kind of overkill.

- First you check the format of the input String in order to determine whether your process will work.

- Then, you split the input in order to extract the right-hand part of "ttl=".

- Then you split this part in order to keep the substring until first whitespace.

The Matcher class is aimed at finding occurences of patterns in a character sequence (e.g. a String.)

Here, we are trying to find the value (i.e. a sequence of non-whitespace characters) that follows the sequence "ttl=".

This can be translated into the regex: (?<=ttl=)\\S+

A single call to Matcher.find() will find it if it exists.

TimTheEnchantora at 2007-7-28 15:10:24 > top of Java-index,Java Essentials,Java Programming...
# 13

the one who post this topic should comment on both your approach... ^_^

Yannixa at 2007-7-28 15:10:24 > top of Java-index,Java Essentials,Java Programming...
# 14

> the one who post this topic should comment on both

> your approach... ^_^

The three step approach should be promoted whenever possible. The three step approach is the most simple and easily learned and is the one where everything can be done naturally. Dixit Maine State Candlepin Bowling Association

Any comment ?

TimTheEnchantora at 2007-7-28 15:10:24 > top of Java-index,Java Essentials,Java Programming...
# 15

Thanks to everyone.

My special thanks to Tim who explained in detail

ArpanaKa at 2007-7-28 15:10:30 > top of Java-index,Java Essentials,Java Programming...
# 16

I have another doubt. As mentioned in my first post in this thread, from the following string:

property typename ttl=10 cval=30 test myprogram

How to get string "test". I tried following but no use. say the above string is stored in a variable named "line"

line = line.substring(line.lastIndexOf("\\s")+1, line.length());

but i dont get any output. can you please help me.

ArpanaKa at 2007-7-28 15:10:30 > top of Java-index,Java Essentials,Java Programming...
# 17

The parameter of the lastIndexOf method is treated as a litteral, not a regex.

Therefore "\\s" doesn't mean whitespace, but simply backslash followed by 's'.

Note that it returns -1 if there is no occurence of the given substring.

The parameters to the substring methods are :

beginIndex - the beginning index, inclusive.

endIndex - the ending index, exclusive.

Knowing that, you can determine what the following statement does (Hint: not much) :line = line.substring(line.lastIndexOf("\\s")+1, line.length());

You have to determine the criteria that make "test" findable.

Assuming that it is the last but one token of your input String considering whitespace as token separator, you might use the String.split method (which splits the string around matches of the given regex):String[] tokens = input.split("\\s");

String lastButOne = tokens[tokens.length-2];

System.out.println("'" + lastButOne + "'");

Tim - Loga, looks like we finally have to split the string... ;-)

TimTheEnchantora at 2007-7-28 15:10:31 > top of Java-index,Java Essentials,Java Programming...
# 18

Oh! so kind of you. It finally worked perfectly.

Thanks alot

ArpanaKa at 2007-7-28 15:10:31 > top of Java-index,Java Essentials,Java Programming...