Code

import java.util.ArrayList; import java.util.List; import org.asteriskjava.manager.ManagerConnection; import org.asteriskjava.manager.ManagerConnectionFactory; import org.asteriskjava.manager.action.CommandAction; import org.asteriskjava.manager.response.CommandResponse; public class Manager { private ManagerConnection c; public Manager() throws Exception { ManagerConnectionFactory factory = new ManagerConnectionFactory( "ip", "admin", "pass"); c = factory.createManagerConnection(); } public void run() throws Exception { c.login(); CommandAction action; CommandResponse response; List<String> list = new ArrayList<String>(); action = new CommandAction(); action.setCommand(" sip show peers"); response = (CommandResponse) c.sendAction(action); list = response.getResult(); int i = 0; while ( i <list.size()) { System.out.println(list.get(i)); i++; } c.logoff(); } public static void main(String[] args) throws Exception { new Manager().run(); } } 

Result of performance:

 Name/username Host Dyn Forcerport ACL Port Status Description 4975/4975 (Unspecified) D a 0 UNKNOWN 4986/4986 (Unspecified) D a 0 UNKNOWN 5001/5001 (Unspecified) D a 0 UNKNOWN 5002/5002 (Unspecified) D a 0 UNKNOWN 6000/6000 (Unspecified) D a 0 UNKNOWN 7777/7777 (Unspecified) D a 0 UNKNOWN 9011/9011 (Unspecified) D a 0 UNKNOWN 9012/9012 (Unspecified) D a 0 UNKNOWN 

Tell me, how can you display a string that contains for example 4975?

  • 2
    Until they came with a full answer, leave a link to the usual SO. In short, in 1.7 you need to check each element through .contains("4975") , in 1.8 use removeIf() . stackoverflow.com/a/21368638/2908793 - etki
  • @Etki: Well, you just remove right away :) - VladD
  • @VladD yes, I didn’t think - etki
  • one
    @Etki In this formulation of the problem, it is better then not to removeIf, but to filter and then output. By type list.stream().filter(s -> s.contains("4975")).forEach(System.out::println); - zzashpaupat

1 answer 1

Firstly, in your case it is more expedient to use a for-each loop. Secondly, it is possible to check whether the string contains another string using the contains method. Here is an example:

 for (String s : list) { if (s.contains("4975")) { System.out.println(s) } } 
  • Thanks for the advice, your example works as needed. - santer