gpt4 book ai didi

java - 如何从 QR 解码字符串中提取非常特定的字符串部分集?

转载 作者:行者123 更新时间:2023-12-02 13:22:01 28 4
gpt4 key购买 nike

我需要选择一个非常大的字符串(从 QR 代码中解码)的特定部分,以将其设置为 Android 中 TextView 元素中的文本。

//*these are the parts of the string whose corresponding values I need as a text for my TextViews.*

String uid, name, gname, gender, house, street, ps, po, dist, subdist, state, pin, address, dob;

//*this 'string' object holds the decoded qr string*

String string = "uid="001" name="Akbar Shah" gender="M" yob="1989" gname="Jahangir Shah" co="S/O: Jahangir Shah" house="45/5" street="Huzurey Ala Street" vtc="Alibagh" po="Bagnan" dist="Faridabad" subdist="Alamgarh" state="Andhra" pc="6754674" dob="15/04/89"";

//*this is where I am using substring() to get a specific part of the string value*

uid = string.substring(string.indexOf("uid=")+5, string.indexOf("name=")-2);
name = string.substring(string.indexOf("name=")+6, string.indexOf("gender=")-2);
gname = string.substring(string.indexOf("gname=")+7, string.indexOf("co=")-2);
gender = string.substring(string.indexOf("gender=")+8, string.indexOf("yob=")-2);

house = string.substring(string.indexOf("house=")+7, string.indexOf("street=")-2);
street = string.substring(string.indexOf("street=")+8, string.indexOf("vtc=")-2);
ps = string.substring(string.indexOf("vtc=")+5, string.indexOf("po=")-2);
po = string.substring(string.indexOf("po=")+4, string.indexOf("dist=")-2);
dist = string.substring(string.indexOf("dist=")+6, string.indexOf("subdist=")-2);
subdist = string.substring(string.indexOf("subdist=")+9, string.indexOf("state=")-2);
state = string.substring(string.indexOf("state=")+7, string.indexOf("pc=")-2);
pin = string.substring(string.indexOf("pc=")+4, string.indexOf("dob=")-2);
dob = string.substring(string.indexOf("dob=")+5, string.indexOf("/>")-1);

//*this is where I have concatenated the whole address parts into one*
address = house+", "+street+", \nPS - "+ps+", \nPO - "+po+", \nDistrict - "+dist+", \nSub-Division - "+subdist+", \nState - "+state+", \nPin Code - "+pin;

TextView tv_uid, tv_name, tv_gName, tv_gender, tv_address, tv_dob;

//*this is where I then setText those substrings for appropriate TextViews*

tv_uid.setText(uid);
tv_name.setText(name);
tv_gName.setText(gname);
tv_gender.setText(gender);
tv_address.setText(address);
tv_dob.setText(dob);

只有解码后的 QR 字符串格式保持不变,即如果现在解码的字符串是这样的,我的方法才有效:

String string = "uid="001" name="Akbar Shah" gender="M" yob="1989" gname="Jahangir Shah" co="S/O: Jahangir Shah" house="45/5" street="Huzurey Ala Street" vtc="Alibagh" po="Bagnan" dist="Faridabad" subdist="Alamgarh" state="Andhra" pc="6754674" dob="15/04/89"";

然后,如果字符串看起来像这样,我的提取方式将不起作用:

String string = "uid="002" name="Amar Tripathi" gender="M" yob="1990" vtc="Alibagh" po="Bagnan" dist="Faridabad" state="Andhra" pc="6754674" dob="15/04/89"";

或者,像这样:

String string = "uid="003" name="Anthony Gonzalis" gender="M" yob="1985" gname="Jahangir Shah" house="45/5" street="Huzurey Ala Street" lm="Behind the Meat shop" loc="Galianwalabagh" vtc="Alibagh" dist="Faridabad" state="Andhra" pc="6754674" dob="15/04/89"";

您可能已经注意到,substring() 不能普遍用于所有情况,因为最后两个字符串值缺少某些部分。因此,如果我指定提取夹在 gname 和 co 之间的特定子字符串,除了第一种情况外,在最后两种情况下它将无法找到 co ,因此代码行的执行会返回错误。有更好的方法吗?

附注我可以从“”中提取所有字符串部分吗?

例如“001”是字符串的一部分,我只想从中获取双引号内的 001 部分。

最佳答案

您可以使用正则表达式,例如 "\w*="([^"]*(?="))"。这会检查以下内容:

  • \w* 匹配单词字符(如字母和数字),直到
  • = 到达“=”,它简单地匹配字符“=”,
  • [^"]* 首先匹配 ^'"' 字符(代表开头的 '"' 字符),然后匹配不是 '"' 的每个字符,直到
  • (?=") 到达,检查下一个字符是否是 '"' 字符(代表结束的 '"' 字符)

像这样使用这个正则表达式:myString.replace("\w*="([^"]*(?="))", "\1")。这将替换整个 Name="myName" 与简单的 myName

希望这有帮助

关于java - 如何从 QR 解码字符串中提取非常特定的字符串部分集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43527809/

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