我有一个看起来像这样的字符串
protected SSLSocketFactory getCertificateSocketFactory() throws Exception {
InputStream certStrm = context.getResources().openRawResource(R.raw.cert_file)
CertificateFactory cf = CertificateFactory.getInstance("X.509");
Certificate ca = cf.generateCertificate(certStrm);
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);
return sslContext.getSocketFactory();
}
我想在我的数据表中存储“2015-1-1”“2017-1-1”“Active” - 这是怎么做到的?
完成该过程后,我想将我存储的数据与原始数据表进行比较,看看日期和状态是否匹配(如果他们不创建报告,提醒用户有错误的日期)
编辑:我有大约300个这些字符串,所以我不确定处理这些字符串的最佳方法是什么。
答案 0 :(得分:0)
您可以尝试这样的事情:
var parts = s.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
var startDate = DateTime.Parse(parts.SkipWhile(p => p != "Start Date:").Skip(1).First().Trim());
var endDate = DateTime.Parse(parts.SkipWhile(p => p != "EndDate:").Skip(1).First().Trim());
var status = parts.SkipWhile(p => p != "Status:").Skip(1).First();
然后,拥有这些值,您可以将它们插入数据表
答案 1 :(得分:0)
这是正则表达式的完美解决方案。
编辑:由于OP更新了他的输入字符串,我更新了我的代码和正则表达式模式以匹配。
string var1 =
@"Start Date:
2015 - 1 - 1
End Date:
2017 - 1 - 1
Warranty Type:
XXX
Status:
Active
Serial Number / IMEI:
XXXXXXXX
Description:
This product has a three year limited warranty and is entitled to parts, labor and on-site repair service. Service is available Monday - Friday, except holidays, with a next business day response objective. Many parts can also be delivered to you using the Customer Replaceable Unit (CRU)method.";
// Remove all spaces and newlines
var1 = var1.Replace(" ", "").Replace("\r\n", "");
// our regex pattern now that there ar eno spaces or new lines
var regex = new Regex(@"StartDate:(.*)EndDate:(.*)W.*Status:(.*)\/");
//assign them
string strStartDate = regex.Match(var1).Groups[1].ToString();
string strEndDate = regex.Match(var1).Groups[2].ToString();
string Status = regex.Match(var1).Groups[3].ToString().Substring(0, 6);
//convert to DateTime from our string versions
DateTime StartDate = DateTime.Parse(strStartDate);
DateTime EndDate = DateTime.Parse(strEndDate);