Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

substring problem

 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How you are printing these values ?
 
Dawn Charangat
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then immediately after reading it off the console, split it....
 
Birla Murugesan
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

use split method of string to do your process.

String[] temp; = str.split("\n");
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for your response.could you please show me in code?

Thanks
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all.I will try that.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
preethi Ayyappan wrote:Thank you all.I will try that.


I believe you can
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.It is working now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic