This question already has answers here:
Java: convert List<String> to a join()d String
(23 answers)
Closed 7 years ago.
I have a situation in which I want to convert a List<String> to a single String and with commas
PS : I'm using RootTools library for command and I wanna execute a
Busybox --list command
Just iterate over your list and add elements as they come to a String.
This is one way of doing it but there are others.
List<String> myList ;
String result = "";
//...
for (String tmp : myList) {
result += tmp+","
}
Related
This question already has answers here:
How to get the last characters in a String in Java, regardless of String size
(11 answers)
Closed 4 years ago.
I have a string "Century Gothic#12,75#True#FFFFFFFF" and i would be able to retrive just the string "FFFFFFFF" so how can i use substring to get the string after the third #? or can i just start in someway to substring the String from the end?
Try this:
String s = "Century Gothic#12,75#True#FFFFFFFF";
String[] parts = s.split("#");
//parts[2] = "FFFFFFFF"
This question already has answers here:
How does a ArrayList's contains() method evaluate objects?
(10 answers)
Closed 5 years ago.
I have an app in which contain ArrayList of some String let suppose a,b,c,d and i have a String called pack. What i have to do is that i have to check whether pack is already exist in arraylist or not if not exist then i have to add it in arraylist. How do i do that
code:-
ArrayList<CBlackList> appsListDataSet;
public void setAppsDetails(String pack) {
if (appsListDataSet != null && appsListDataSet.size() > 0) {
for (int i = 0; i < appsListDataSet.size(); i++) {
CAppsModel appsModel = appsListDataSet.get(i);
if (pack.equalsIgnoreCase(appsModel.getPackageName())){
//Not to know what to do here.
}
}
}
}
Use the contains method in an arrayList. If your arrayList contains only String and you want to check for String value itself, then just do
boolean isPackPresent = appsListDataSet.contains(pack)
this will work if your arraylist has the same datatype as pack and is not a custom object.
Try Some thing like this
if(appsListDataSet.contains("your String"))
{
///Action to be occur
}
This question already has answers here:
StringBuilder to Array of Strings how to convert
(2 answers)
Closed 6 years ago.
I have a string builder in Android which is filled with data from a database, and I want to display that data using a list view. For that I want to covert string builder into an array of strings. Can somebody help me in this conversion, or suggest to me some other technique.
Here you can find Link
tempArray = sb.toString().split("ABCABC"); will split the string and return an array of strings for each line.
Try this :
String sbString = sb.ToString();
String[] ary = "abc".split("TABTAB");
This question already has answers here:
How to evaluate a math expression given in string form?
(26 answers)
Closed 7 years ago.
A part of the goal in my app is to receive a mathematical expression (e.g. 1 + 1) by string, and convert it to a BigInteger.
My goal is to have an equivalent to:
BigInteger result = new BigInteger("1+1");
// This will throw an exception of invalid BigInteger
Also, ScriptEngineManager class isn't available for Android.
I still cannot find a way to achieve my goal.
Thanks a lot for helping!
You can try :
//If you have to parse only the result
String result = "2";
BigInteger.valueOf(Long.valueOf("2");
//If you have to parse the whole operation
String result = "1+1";
//create an algo to extract each 1 and determine operation
BigInteger bigIntUn = new BigInteger("1");
BigInteger bigIntAnotherUn = new BigInteger("1");
//you have add (+), min(-), multiply(*) (etc...)
bigIntUn.add(bigIntAnotherUn);
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 8 years ago.
I have data with repeating pattern '###'.I want to extract all strings between this pattern.
The data is like -
son###can###e###nick###54###
how can i get all data between the pattern '###'.
try this:
String patternString = "###";
Pattern pattern = Pattern.compile(patternString);
String[] split = pattern.split(text);
You can use Scanner with "###" delimiter.
Scanner in = new Scanner("son###can###e###nick###54###");
in.useDelimiter("###");
while(in.hasNext)
String x = in.next();
//Do something with x;
x will hold everything between ###. You can easily store them in an Array inside the loop or use them anyway you like.
Try this:
String str = "son###can###e###nick###54###";
String newStr = str.replaceAll("[#]+", "");
or you can separate all world via
String rem = "###";
Pattern pattern = Pattern.compile(rem);
String[] splitarr = pattern.split(text);
for(int i=0;i<aplitarr.length();i++)
{
String word=aplitarr[i].ToString();
}
Hope this may help you!
try,
String[] separated = CurrentString.split("###");
separated[0]; // this will contain "son"
separated[1]; // this will contain "can"
or
StringTokenizer tokens = new StringTokenizer(CurrentString, "###");
String first = tokens.nextToken();// this will contain "son"
String second = tokens.nextToken();// this will contain "can"