为什么java字符串不“包含”?方法返回找到了吗?
这是我的函数的输入
"<div class=\"xbox\">TESTING<span class=\"newbox\"><a href=\"javascript:void(0);\" id=\"btnbox\">Use a new box</a></span></div><div class=\"cardnumber\">***-1111</div></div>"
当我执行以下操作时,它不会返回除 -1 之外的
input.indexOf("cardnumber")
任何内容 这不起作用有什么原因吗?当我执行 indexOf("div") 时,它返回正常...(帮助)
谢谢大家(抱歉没有早点发布单元测试)
public Cart viewCart() {
ResponseAndCookies result = service.makeHttpRequestWithUrl("https://www.xbox.com/account/fakebox/");
String response = result.getResponse();
String availableCreditCard = "<div class=\"cardnumber\">***";
if (response.contains(availableCreditCard)) {
return parseJson.parseCartAndReturnObject(response);
}
return null;
}
public class XboxViewCartServiceTest {
@Test
public void verify_cart_works_with_valid_login() {
FakeXboxCartParseJson jsonParser = new FakeXboxCartParseJson();
XboxViewCartService sut = new XboxViewCartService(new FakeXboxViewCartHttpBase(), jsonParser);
Cart cart = sut.viewCart(null);
Assert.assertTrue(jsonParser.calledMethod);
}
class FakeXboxViewCartHttpBase extends XboxHttpService {
@Override
public ResponseAndCookies doHttpPostWithUrl(String url, ResponseAndCookies responseAndCookies, String json) {
ResponseAndCookies result = new ResponseAndCookies();
result.setResponse("<div class=\"xbox\">TESTING<span class=\"newbox\"><a href=\"javascript:void(0);\" id=\"btnbox\">Use a new box</a></span></div><div class=\"cardnumber\">***-1111</div></div>");
return result;
}
}
class FakeXboxCartParseJson extends XboxCartParseJson {
public boolean calledMethod = false;
@Override
public Cart parseCartAndReturnObject(String html) {
calledMethod = true;
return null;
}
}
}
Here is the input to my function
"<div class=\"xbox\">TESTING<span class=\"newbox\"><a href=\"javascript:void(0);\" id=\"btnbox\">Use a new box</a></span></div><div class=\"cardnumber\">***-1111</div></div>"
When I do the below it never returns anything but -1
input.indexOf("cardnumber")
Any reason this isn't working? when I do an indexOf("div") it returns fine ... (help)
thanks guys (sorry for not posting the unit test earlier)
public Cart viewCart() {
ResponseAndCookies result = service.makeHttpRequestWithUrl("https://www.xbox.com/account/fakebox/");
String response = result.getResponse();
String availableCreditCard = "<div class=\"cardnumber\">***";
if (response.contains(availableCreditCard)) {
return parseJson.parseCartAndReturnObject(response);
}
return null;
}
public class XboxViewCartServiceTest {
@Test
public void verify_cart_works_with_valid_login() {
FakeXboxCartParseJson jsonParser = new FakeXboxCartParseJson();
XboxViewCartService sut = new XboxViewCartService(new FakeXboxViewCartHttpBase(), jsonParser);
Cart cart = sut.viewCart(null);
Assert.assertTrue(jsonParser.calledMethod);
}
class FakeXboxViewCartHttpBase extends XboxHttpService {
@Override
public ResponseAndCookies doHttpPostWithUrl(String url, ResponseAndCookies responseAndCookies, String json) {
ResponseAndCookies result = new ResponseAndCookies();
result.setResponse("<div class=\"xbox\">TESTING<span class=\"newbox\"><a href=\"javascript:void(0);\" id=\"btnbox\">Use a new box</a></span></div><div class=\"cardnumber\">***-1111</div></div>");
return result;
}
}
class FakeXboxCartParseJson extends XboxCartParseJson {
public boolean calledMethod = false;
@Override
public Cart parseCartAndReturnObject(String html) {
calledMethod = true;
return null;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这显然是按照你提供的方式工作的,所以只有 3 个选项:
目前,最好的是第3项。
请提供更多代码。
编辑(2011-05-23 02:20):
感谢您更新您的问题。因此,现在我们仍然无法确定,因为我们缺少端点返回的数据,但如果只是不完全包含
请确保为您的请求指定正确的标头,并使用有效的内容类型和字符编码。另请确保您没有在源文件上使用奇怪的编码,因为您可能在不知情的情况下输入了非标准
*
字符。This obviously works as you provide it, so there are only 3 options:
For the moment, the best best is item 3.
Please provide more code.
EDIT (2011-05-23 02:20):
Thanks for the update to your question. So, now we still cannot be sure as we're missing the data returned by your endpoint, but there's a good chance if just doesn't exactly contain
<div class="cardnumber">***
.Do make sure as well that you specify the right headers for your request and that you use valid content type and character encoding. Please make also sure that you are not using a strange encoding on your source files, as you might be inputting non-standard
*
characters without knowing it.