我需要选择一个非常大的字符串的特定部分(从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“”; < / p>
然后,如果字符串看起来像这样,我的提取方式将不起作用:
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 = “在肉店后面”loc =“Galianwalabagh”vtc =“Alibagh”dist =“Faridabad”state =“Andhra”pc =“6754674”dob =“15/04/89”“;
正如您可能已经注意到的那样,substring()不能普遍用于所有情况,因为最后两个字符串值中缺少一些部分。因此,如果我指定提取夹在gname和co之间的特定子字符串,除了第一种情况,在最后两种情况下,它将无法找到co,因此执行代码行返回错误。 有没有更好的方法呢?
P.S。我可以从“”?
中提取所有字符串部分E.g。 “001”是字符串的一部分,我只想从中获取双引号内的001部分。
答案 0 :(得分:1)
你可以使用正则表达式,例如"\w*="([^"]*(?="))"
。这将检查以下内容:
\w*
匹配字词(如字母和数字)直到=
&#39; =&#39;到达时只匹配字符&#39; =&#39;,[^"]*
首先匹配^&#39;&#34;&#39; -character(代表开头&#39;&#34;&#39; -character)然后每个字符那不是&#39;&#34;&#39;直到(?=")
,检查下一个字符是否为&#39;&#39; -character(代表结束&#39;&#34;&#39; - 字符)
像这样使用这个正则表达式:myString.replace("\w*="([^"]*(?="))", "\1")
。这将仅用Name="myName"
替换整个myName
。
希望这有帮助
答案 1 :(得分:0)
首先,我创建了一个DataAttributes类。
DataAttributes.java
public class DataAttributes {
// declare xml attributes of QR code xml response
public static final String
DATA_TAG = "PrintLetterBarcodeData",
UID_ATTR = "uid",
NAME_ATTR = "name",
GENDER_ATTR = "gender",
GNAME_ATTR = "gname",
HOUSE_ATTR = "house",
STREET_ATTR = "street",
LM_ATTR = "lm",
LOC_ATTR = "loc",
VTC_ATTR = "vtc",
PO_ATTR = "po",
DIST_ATTR = "dist",
SUBDIST_ATTR = "subdist",
STATE_ATTR = "state",
PC_ATTR = "pc",
DOB_ATTR = "dob";
}
正在扫描QR码并将其存储在字符串中。
在您要显示扫描数据的活动中,在我的情况下是QRActivity ...所以我用单个参数创建了一个方法 - &gt; QRActivity.java中的QRActivity类中的(String scanData)
在其中编写以下代码
Log.d("QR Code",scanData);
XmlPullParserFactory pullParserFactory;
try {
// init the parserfactory
pullParserFactory = XmlPullParserFactory.newInstance();
// get the parser
XmlPullParser parser = pullParserFactory.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(new StringReader(scanData));
// parse the XML
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if(eventType == XmlPullParser.START_DOCUMENT) {
Log.d("QR Code","Start document");
} else if(eventType == XmlPullParser.START_TAG && DataAttributes.DATA_TAG.equals(parser.getName())) {
// extract data from tag
//uid
uid = parser.getAttributeValue(null,DataAttributes.UID_ATTR);
//name
name = parser.getAttributeValue(null,DataAttributes.NAME_ATTR);
//gender
gender = parser.getAttributeValue(null,DataAttributes.GENDER_ATTR);
// guardian name
gname = parser.getAttributeValue(null,DataAttributes.GNAME_ATTR);
// house number
house = parser.getAttributeValue(null,DataAttributes.HOUSE_ATTR);
// Street name
street = parser.getAttributeValue(null,DataAttributes.STREET_ATTR);
// landmark
lm = parser.getAttributeValue(null,DataAttributes.LM_ATTR);
// locality
loc = parser.getAttributeValue(null,DataAttributes.LOC_ATTR);
// village town city
vtc = parser.getAttributeValue(null,DataAttributes.VTC_ATTR);
// Post Office
po = parser.getAttributeValue(null,DataAttributes.PO_ATTR);
// district
dist = parser.getAttributeValue(null,DataAttributes.DIST_ATTR);
// sub-division
subdist = parser.getAttributeValue(null,DataAttributes.SUBDIST_ATTR);
// state
state = parser.getAttributeValue(null,DataAttributes.STATE_ATTR);
// Post Code
pc = parser.getAttributeValue(null,DataAttributes.PC_ATTR);
// Date of Birth
dob = parser.getAttributeValue(null,DataAttributes.DOB_ATTR);
} else if(eventType == XmlPullParser.END_TAG) {
Log.d("QR Code","End tag "+parser.getName());
} else if(eventType == XmlPullParser.TEXT) {
Log.d("QR Code","Text "+parser.getText());
}
// update eventType
eventType = parser.next();
}
// display the data on screen
String na = "N/A";
if (uid == null){
tv_uid.setText(na);
}else {
tv_uid.setText(uid);
}
if (name == null){
tv_name.setText(na);
}else {
tv_name.setText(name);
}
if (gender == null){
tv_gender.setText(na);
}else {
tv_gender.setText(gender);
}
if (gname == null){
tv_gName.setText(na);
}else {
tv_gName.setText(gname);
}
if (house == null){
tv_house.setText(na);
}else {
tv_house.setText(house);
}
if (street == null){
tv_street.setText(na);
}else {
tv_street.setText(street);
}
if (lm == null){
tv_lm.setText(na);
}else {
tv_lm.setText(lm);
}
if (loc == null){
tv_loc.setText(na);
}else {
tv_loc.setText(loc);
}
if (vtc == null){
tv_vtc.setText(na);
}else {
tv_vtc.setText(vtc);
}
if (po == null){
tv_po.setText(na);
}else {
tv_po.setText(po);
}
if (dist == null){
tv_dist.setText(na);
}else {
tv_dist.setText(dist);
}
if (subdist == null){
tv_subdist.setText(na);
}else {
tv_subdist.setText(subdist);
}
if (state == null){
tv_state.setText(na);
}else {
tv_state.setText(state);
}
if (pc == null){
tv_pc.setText(na);
}else {
tv_pc.setText(pc);
}
if (dob == null){
tv_dob.setText(na);
}else {
tv_dob.setText(dob);
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
上面的代码是我如何使用XMLPullParserFactory将字符串分段为适当的部分然后单独显示它们的示例。
现在,您可以在“活动”中使用适合您的方法。我只是展示了实现XMLPullParserFactory以分割字符串的方式。