gpt4 book ai didi

java - java中两个分隔符之间的子字符串

转载 作者:搜寻专家 更新时间:2023-11-01 07:47:45 25 4
gpt4 key购买 nike

我有一个字符串:

<head>

<script type="text/javascript">window._timings = {"domLoading": Date.now()}</script>

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<script type="text/javascript">window._sharedData = {"country_code": "IR", "language_code": "en", "gatekeepers": {"cc": true, "sms": true, "ra": true}, "show_app_install": false, "static_root": "//instagramstatic-a.akamaihd.net/h1", "platform": "web"};</script>
<script src="//instagramstatic-a.akamaihd.net/h1/bundles/en_US_ProfilePage.js/88ce62d41e70.js" type="text/javascript" crossorigin="anonymous"></script>

我只需要提取:

{"country_code": "IR", "language_code": "en", "gatekeepers": {"cc": true, "sms": true, "ra": true}, "show_app_install": false, "static_root": "//instagramstatic-a.akamaihd.net/h1", "platform": "web"}

需要这方面的帮助。

最佳答案

就简单的字符串解析而言,您可以使用如下函数:

public static String between(String start, String end, String input) {
int startIndex = input.indexOf(start);
int endIndex = input.lastIndexOf(end);
if(startIndex == -1 || endIndex == -1) return input;
else return input.substring(startIndex + start.length(), endIndex + end.length()).trim();
}

在这种情况下,用法如下:

String input = "<head>\n" +
"\n" +
" <script type=\"text/javascript\">window._timings = {\"domLoading\": Date.now()}</script>\n" +
"\n" +
" <meta charset=\"utf-8\">\n" +
" <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n" +
"<script type=\"text/javascript\">window._sharedData = {\"country_code\": \"IR\", \"language_code\": \"en\", \"gatekeepers\": {\"cc\": true, \"sms\": true, \"ra\": true}, \"show_app_install\": false, \"static_root\": \"//instagramstatic-a.akamaihd.net/h1\", \"platform\": \"web\"};</script>\n" +
"<script src=\"//instagramstatic-a.akamaihd.net/h1/bundles/en_US_ProfilePage.js/88ce62d41e70.js\" type=\"text/javascript\" crossorigin=\"anonymous\"></script>";
System.out.println(between("window._sharedData =", "}", input));

结果符合预期:

{"country_code": "IR", "language_code": "en", "gatekeepers": {"cc": true, "sms": true, "ra": true}, "show_app_install": false, "static_root": "//instagramstatic-a.akamaihd.net/h1", "platform": "web"}

关于java - java中两个分隔符之间的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40405032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com