Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string like this:
["477","com.dummybilling","android.test.purchased","inapp:com.dummybilling:android.test.purchased","779"]
How to have a String[] with these 5 element?
Does anyone know a regex for .split() method?
Thank you very much, regular expressions make me crazy! :(
Process it as JSON. Two immediate benifits would be that it would take care of any embedded commas in your data automatically and the other that you would get a String[] with unquoted strings.
String input = "[\"477\",\"com.dummybilling\",\"android.test.purchased\",\"inapp:com.dummybilling:android.test.purchased\",\"779\"]";
JSONArray jsonArray = new JSONArray(input);
String[] strArr = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
strArr[i] = jsonArray.getString(i);
}
System.out.println(Arrays.toString(strArr));
Output :
[477, com.dummybilling, android.test.purchased, inapp:com.dummybilling:android.test.purchased, 779]
You can split your string by separator [" (the beginning) or "," or "] (the ending) like this:
final String[] tokens = yourString.split("\",\"|\\[\"|\"\\]");
Please note that this will only work for your string. It's not a general solution (for example, it does not take care of any escaped quotes). If your string is in JSON format, you should use a JSON parser as proposed by #Ravi Thapliyal .
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
String jsonString="{"name":"event_level_count","elements":[{"serial_number":"xxx12315","manufacturer_name":"xxx","count_level1":2004,"count_level2":1798,"count_level3":7},{"serial_number":"yyx01444","manufacturer_name":"xxx","count_level1":15,"count_level2":11,"count_level3":3}]}" ;
JSONObject output = new JSONObject(jsonString);
JSONArray docs = output.getJSONArray("elements");
System.out.println("Docs: "+docs);
Output Docs:
{"name":"event_level_count","elements":[{"count_level3":7,"count_level2":1798,"count_level1":2004,"manufacturer_name":"xxx","serial_number":"xxx12315"},{"count_level3":3,"count_level2":11,"count_level1":15,"manufacturer_name":"xxx","serial_number":"yyx01444"}]}
File file=new File("path \\ExportAsExcelfromJSON.csv");
String csv = CDL.toString(docs);
I need to reverse its as given string - jsonString.
JSON is simply a way to save or transfer data -- if you care about how it looks (e.g. how it's sorted), you are probably doing something wrong. If you want the array to be sorted in any way, do it once you have parsed the JSON back into actual data. Most programming languages (Java, for instance) come with easy-to-use tools to sort arrays.
As per Nizil's comment and this page, you can use the reverse(List<?> list) function from the Collections class in Java.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I want to convert integer type array that contain only 0 and 1 into string that should be simple sequence of array value not in any other form for example if array is
arr[] = {0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1}
than string should be
str = "0110100001100101"
than i want to divide it into two substrings like
str1 = 01101000(this is bit value of h as i know) and str2= 01100101 (this is bit value of e) and want to convert these substrings into character h from str1 and e from str2.
please help me .
You can use Arrays.toString()
Integer arr[] = {0,1,1,0,1,0,0,0,0,1,1,0,0,1,0,1};
String str= Arrays.toString(arr).replaceAll(",|\\[|\\]","").trim();
System.out.println(str);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do I add two numbers within one string?
I have:
String a = "(x+x)";
String lb = "2";
String b = a.replace("x", lb);
int c = ?
it outputs 2+2, how do I get it to add them together correctly into an integer?
While you can use a Java library to achieve this goal as mentioned in the comments, there is something built into Java since version 6 which may be useful (but maybe a little overkill). I suggest this not because I believe it's particularly efficient but rather as an alternative to using a library.
Use JavaScript:
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
ScriptEngine jsRuntime = new ScriptEngineManager().getEngineByName("javascript");
String expression = "2+2"; //The expression you would to evaluate.
double result = (Double) jsRuntime.eval(expression);
Use the Integer.parseInt method to parse a String into an int, then add the values. Adding strings just concatenates them.
You can use parseInt() method.
It seems your question could be summarised as
How do I convert a String such as "2+2" to the number 4
Here's a solution that uses only the standard Java libraries:
String sumExpression = "2+2";
String[] numbers = sumExpression.split("\\+");
int total = 0;
for (String number: numbers) {
total += Integer.parseInt(number.trim());
}
// check our result
assert total == 4;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Whenever I want to parse the string "0" in Java to 0 (int) it throws a InvalidInt-Error.
However strings like "1", "2" etc. work.
UPDATE: Other numbers don't work as well.
I'm fetching the HTML source code of a PHP-File from my web page and this web page only displays one number.
Code:
String[] result = sourceCode.trim().split("<br>");
for (int i = 0; i < result.length; i++)
{
result[i] = result[i].trim().replace("\n", "").replace("\r", "");
}
if (Integer.parseInt(result[0]) > 0)
{
//Do Something
}
With the information provided in the comments below the question, it turns out that you have the Unicode Character 'ZERO WIDTH NO-BREAK SPACE' (U+FEFF) repeatedly at the beginning of your String. You can do the following to remove it:
final String ZERO_WIDTH_NO_BREAK_SPACE = "\uFEFF";
String good = result[0].replace(ZERO_WIDTH_NO_BREAK_SPACE, "");
Also, there is one TAB hidden inbetween all those characters. Get rid of it with
good = good.replaceAll("\\s+", "");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to java need help
the main string i have is
eg.
"String1/string2/string3/string4/all_free.sdx"
"file1/file2/string3/string4/all_free.sdx"
the end result i need is to be able to isolate and get string3
i can indexof but not able to achieve it in few steps need brainy people help as i am new to JAVA
If you know it's the third item that you want to get, one simple approach could be using split method, like this:
String myString = "String1/string2/string3/string4/all_free.sdx";
String string3 = myString.split("/")[2];
The call of split("/") produces an array of strings like this:
{"String1", "string2", "string3", "string4", "all_free.sdx"}
Now you can apply the subscript operator to grab the element that you want.