Tag: 字符串

由大字符串引起的OutOfMemoryException

我使用以下方法从webapi捕获数据: public static String sendRequest(String requestURL, String data) throws IOException { URL url = new URL(requestURL + “?” + data); URLConnection conn = url.openConnection(); conn.setReadTimeout(10000); BufferedReader in = new BufferedReader(new InputStreamReader( conn.getInputStream())); String inputLine; StringBuilder answerBuilder = new StringBuilder(“”); try { while ((inputLine = in.readLine()) != null) answerBuilder.append(inputLine); in.close(); } catch (Exception e) { } […]

用Jackson Java解析JSON

我在与jackson解析JSON时遇到了问题。 我有一个POJO对象,由另一个包裹。 这是我的代码: in main: ObjectMapper mapper = new ObjectMapper(); List mpl2 = mapper.readValue(col.toString(),new TypeReference<List>() {}); my POJO class: public class ItemBean implements Serializable { private List items; @JsonProperty(“Item”) public List getItems() { return items; } public void setItems(List items) { this.items = items; } } public class Item implements Serializable{ public String field1; public […]

Java:检查String中是否有1次或更多次

我正在解析文件中的文本并试图查看时间。 我首先需要检查文本中是否有时间。 文本中唯一一致的模式是所有时间都表示为1或2位数:2位数。 我写了一些java代码,看看我是否能找到我正在看的字符串中有一次或多次。 String timePattern = “\\d\\d?:\\d\\d”; if(textWithTime.matches(timePattern)){ System.out.println(“MATCH”); } else { System.out.println(“NO MATCH”); } 然而,当我的String textWithTime等于“06:45/07:52/10:27”或“发生在06:22”时,我被告知没有匹配。 如何查看我的文本中是否有时间模式?

Java字符串编码

两者之间有什么区别 “hello world”.getBytes(“UTF-8”); 和 Charset.forName(“UTF-8”).encode(“hello world”).array(); ? 在大多数情况下,第二个代码产生一个最后有0字节的字节数组。

在应用程序购买工作,但方法不执行?

我已成功将应用内购买添加到我的应用中,并且通过罚款: 但if (purchase.getSku().equals(“android.test.purchased”))没有通过,所以我无法用高级版更新我的UI! public void buy(View v) { mHelper.launchPurchaseFlow(this, “android.test.purchased”, 10001, mPurchaseFinishedListener, “developerPayLoadString”); } IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() { public void onIabPurchaseFinished(IabResult result, Purchase purchase) { if (result.isFailure()) { Toast.makeText(MainActivity.this, “Unable to make purchase.”, Toast.LENGTH_SHORT).show(); return; } else if (purchase.getSku().equals(“android.test.purchased”)) { Toast.makeText(MainActivity.this, “Successfully bought product!”, Toast.LENGTH_SHORT).show(); } }; 即使付款成功,为什么if (purchase.getSku().equals(“android.test.purchased”))仍然没有通过? 谢谢, Ruchir

字符串比较困惑

在我们继续之前: 我知道我们应该使用.equals()来比较内容。 我现在只是谈论以下场景的实际参考是否相同…… 假设我们有以下内容: String str1 = “str”; String str2 = “string”; String str3 = “ing”; String str4 = str1 + str3; str1 = str4; String str5 = “string”; System.out.println(str1==str2);//false 我认为它应该是真的,因为在String池中,对“string”的引用应该是相同的,因为str1和str2现在都是“string”。 它应该是真的,但最终是假的 。 System.out.println(str1.intern()==str2.intern());//true 我试过这个,这次回归真实 。 然后我尝试了: System.out.println(str1==str5);//false System.out.println(str2==str5);//true 还有System.out.println(“str”+“ing”==“string”); // true 是不是应该来自String池? 有人可以帮忙解释一下吗?

如何通过保留密钥的数据类型将json序列化到另一个json?

我有我原来的JSON字符串,其中我有键和值,如下所示 – { “u”:{ “string”:”1235″ }, “p”:”2047935″, “client_id”:{ “string”:”5″ }, “origin”:null, “item_condition”:null, “country_id”:{ “int”:3 }, “timestamp”:{ “long”:1417823759555 }, “impression_id”:{ “string”:”2345HH*” }, “is_consumerid”:true, “is_pid”:false } 例如,一个键是”u” ,其值为 – { “string”:”1235″ } 同样,另一个关键是”country_id” ,其值为 – { “int”:3 } 现在我需要做的是,我需要代表键值对,如下所示。 如果任何值是字符串数据类型(如键u的值),则用双引号表示它的值,否则不用双引号表示它的值。 country_id的含义值不在String双引号中,因为它是int。 “u”: “1235” “p”: “2047935” “client_id”: “5” “origin”:null “item_condition”:null “country_id”: 3 // I don’t have double quotes […]

字符串比较结果的差异b / w ==和字符串#替换为==

可能重复: Java中的字符串比较和字符串实习 我对Java中的字符串比较有一点疑问,请考虑以下代码: if(“String”.replace(‘t’,’T’) == “String”.replace(‘t’,’T’)) { System.out.println(“true”); } else { System.out.println(“false”); } 上面的代码总是打印为false ,就像我尝试这样: if(“STring” == “STring”) { System.out.println(“true”); } else { System.out.println(“false”); } 它总是打印我true 。 是的,我知道String比较应该用String.equals() or equalsIgnoreCase()方法完成。 但这是在采访中提出的问题之一,我很困惑。 任何人都可以指导我这种行为吗? 据我所知,在代码片段1中, “String.replace(‘t’,’T’)返回对象,因此对象比较返回false。我是对的吗?

创建逗号分隔字符串作为sql“IN”子句的输入

我想要一个字符串作为SQL“IN”子句的输入,其中我想要一个用逗号分隔的字符串列表

Java – 字符串和数组引用

刚开始学习Java,发现字符串和数组都是引用类型。 我不明白以下问题: String a = “a1”; String b = “a2”; a=b; a = “rrr”; System.out.println(a); System.out.println(b); int[] arr1 = {1,2,3}; int[] arr2 = arr1; arr2[0]= 19; System.out.println(arr1[0]); 当我打印它时,我得到: “rrr” “a2” 10 当使用数组时 – 我知道它们都指向同一个对象,所以如果我改变单元格 – 我看到arr1和arr2的区别。 关于“字符串” – 根据我的理解,当我这样做时:a = b它不应该是:“让b上的同一个对象上的a点指向” – 意思是如果我改变它,它们都需要被改变? 谢谢!!