aspose file tools*
The moose likes Beginning Java and the fly likes substring problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "substring problem" Watch "substring problem" New topic
Author

substring problem

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,

I got the output of my program as follows:

Event: Newstate
Privilege: call,all
Channel: SIP/NBE-09541428
State: Ringing
CallerID: 9842412678
CallerIDName: <unknown>
Uniqueid: 1244020336.101


I need to take the Channel,CallerID,Uniqueid from the above.I have tried using substring to separate this.but i didnt get the output what i need.please help me to seggregate this.

Thanks
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

How you are printing these values ?
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
Instead of using substring, you could use String.split() for this situation.
If the entire string [including the line breaks] as you mentioned in the question is the input to your program, then you might initially want to do a split based on line-breaks, and then follow it up with a split with colon [:].
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518

Thanks.I am reading the line from the console and printing the output as follows


String event=br.readLine();
System.out.println(event);
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
then immediately after reading it off the console, split it....
Birla Murugesan
Ranch Hand

Joined: Nov 25, 2008
Posts: 66

use split method of string to do your process.

String[] temp; = str.split("\n");
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518


String[] temp = event.split(":");
System.out.print(temp);


But i didnt get the proper out put.please correct me where am i wrong?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Use StringBuilder to append the string (separated by some identifier example , or| etc) first

then you do your logic using String[] temp = sb.split(":"); // now time to build your logic
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
thanks for your response.could you please show me in code?

Thanks
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

preethi Ayyappan wrote:could you please show me in code?


Well today i can code for you. But think about tomorrow .. try to do it
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
There is no reason for it not to work:

If your input string is "hello:there" [ofcourse, without the double quotes], then :

String event = "hello:there";
String temp[] = event.split(":");

System.out.println(temp[0]); // will give you 'hello'
System.out.println(temp[1]); // will give you 'there'

does it make sense?
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thank you all.I will try that.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

preethi Ayyappan wrote:Thank you all.I will try that.


I believe you can
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks.It is working now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: substring problem