为什么Java字符串“不包含”?找到方法返回? [英] Why won't the java String "contains" method return found?

查看:128
本文介绍了为什么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之外,它什么也不会返回

When I do the below it never returns anything but -1

input.indexOf("cardnumber")

任何原因都行不通吗?当我执行indexOf( div)时,它返回正常...(帮助)

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;
        }
    }
}


推荐答案

这显然可以按照您提供的方式工作,因此只有3个选项:

This obviously works as you provide it, so there are only 3 options:


  1. 字符串;

  2. 读取输入并进行比较时会出现编码问题;

  3. 在其他地方看不到错误,因为您看不到

目前,最好的是项目3。

For the moment, the best best is item 3.

请提供更多代码。

编辑(2011-05-23 02:20):

感谢您对问题的更新。因此,由于丢失了端点返回的数据,现在我们仍不能确定,但​​是很有可能只是不完全包含< div class = cardnumber> ** *

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.

这篇关于为什么Java字符串“不包含”?找到方法返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆