在第一个逗号之后和第二个逗号之前获取字符串

时间:2017-12-08 14:05:50

标签: java android

我有一个这样的字符串 - @Service public class MyBusinessClient { @Autowired private RestTemplate template; public ResponseEntity<ProductsResponse> send(Req req) { //sends request to external webservice api return template.postForEntity(host, req, ProductsResponse.class); } } @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class Test { @Test public void test() { String xml = loadFromFile("productsResponse.xml"); //TODO how can I tell RestTemplate to assume that the external webserver responded with the value in xml variable? } }

我想从中仅提取"5,password,6099000,tree,city"。这意味着在第一个逗号之后和第二个逗号之前的任何内容。

我该怎么做?

4 个答案:

答案 0 :(得分:3)

最简单的解决方案可能是将字符串拆分为逗号:

String input = "5,password,6099000,tree,city";
String[] parts = input.split(",");
String password = parts[1];

答案 1 :(得分:0)

使用Split(),

功能
time                        sumValue    obs  
2013-07-03 07:50:00 UTC     0           0    
2013-07-03 07:55:00 UTC     7           2    
2013-07-03 08:00:00 UTC     7           2    
2013-07-03 08:05:00 UTC     3           1    
2013-07-03 08:10:00 UTC     3           1    

另见: How to split a string in Java

答案 2 :(得分:0)

你去

  

这将起作用

 String str="5,password,6099000,tree,city"
  String parts[]=str.split(",");
    String part1 = parts[0]; // 5
    String part2 = parts[1]; //Password

答案 3 :(得分:0)

String string = "5,password,6099000,tree,city";
String subString = string.substring(2,10);

我假设您是Java初学者,您可能需要检查

  

字符串操作