How to assign StringBuilder to String value at Android? - android

I have a dynamic message Stringbuilder value in my codes.
But i want to create temp string and i want to save message value to temp value and than clean message value:
For example:
StringBuilder messages;
StringBuilder [] temp = new StringBuilder[1000];
...///other part of codes
temp[i]=message;
String text=intent.getStringExtra("theMessage");
i++;
messages.delete(0, messages.length());
A mean, when my messages come 34 i want to save temp[0] and clean message and i want to increase i with i++ and than message come 23 than temp[1]=23 and so on... How can i do this at android ?

Its as simple as following:
StringBuilder [] temp = new StringBuilder[1000];
int index = 0;
if(messages.length() == 34){
temp[index] = new StringBuilder(messages);
messages.delete(0, messages.length());
}
...
If temp[] is of type String then simply
temp[index] = new StringBuilder(messages.toString())

Related

How to convert a string variable to a string array

I am making a request from my app to a server which is returning an array. I want to put that array into a listView but the the whole array is treated as a single listItem.
As the response is recieved in a string variable i want to convert it to a string array?
any help is appretiated.. Thanks
Just replace arr with your String Array variable name:
StringBuilder builder = new StringBuilder();
for(String s : arr) {
builder.append(s);
}
String str = builder.toString();
Firstly string response data split by space[Available special character] or assign string array.
String yourdata = "A B C D E F G H";
String[] yourtDataArr = yourdata.split(" ");
ArrayList<String> list = new ArrayList<String>();
for(i =0;i<yourtDataArr.length;i++){
list.add(yourtDataArr[i]);
}
Then use your list on ListView. You can see following example.
http://www.vogella.com/tutorials/AndroidListView/article.html
https://www.tutorialspoint.com/android/android_list_view.htm
http://windrealm.org/tutorials/android/android-listview.php

Handle comma separated string

Creating an app in which i want to get string from json but i have one key and multiple value so i don't know how to handle this.
"colours": "#fff600,#000000,#ffffff,#00000,#ff9900,#333333"
And want to use this color in different class:
final ValueAnimator colorAnimation = ValueAnimator.ofObject(new android.animation.ArgbEvaluator(), Color.RED, Color.BLUE,Color.WHITE,Color.YELLOW,Color.CYAN,Color.MAGENTA,Color.GREEN,Color.GRAY);
colorAnimation.setDuration(1400);
Put the value of colours in string variable and then split the string in following way and add it to an arraylist :
String[] arr = str.split(",");
ArrayList<String> arr1 = new ArrayList<String>();
for (int i=0; i<arr.length; i++){
arr1.add(arr[i]);
}
Get value of color and use string tokenizer with ',' delima like this:
StringTokenizer stringTokenizer = new StringTokenizer(colorValueString, ",");
Also it has stringTokenizer.nextToken to get the next color in string
You can get the value of colours as a string and then split the string into parts like this:
String colours = json.getString("colours");
Log.d(TAG, colours);
String items[] = colours.split(",");
for (String item : items) {
Log.d(TAG, item);
}
If you own the json
You should use JSONArray:
"colours": ["#fff600","#000000","#ffffff","#00000","#ff9900","#333333"]
And read it like
JSONObject json = ...;
JSONArray colorsArray = json.getJSONArray("colours");
for(int i = 0; i < colorsArray.length(); i++) {
String colorString = colorsArray.getString(i);
int color = Color.parseColor(colorString);
// you should probably also catch IllegalArgumentException for wrong input
}
If you don't own the json
You can read it as string and split around commas:
JSONObject json = ...;
String colorsString = json.getString("colours");
String[] colorStrings = colorsString.split(",");
for(String string : colorStrings) {
int color = Color.parseColor(string);
// you should probably also catch IllegalArgumentException for wrong input
}

Long to hexString , wrong size

I work with my custom USB Device. I get from it bytes array. I want to display this array as hex string so I convert it firstly into Long like this :
byte[] receivedTag = connector.receive(512);
String tag = null;
if (receivedTag != null) {
long tagValue = ByteBuffer.wrap(receivedTag).getLong();
Next I want to convert it into hex String :
tag = Long.toHexString(tagValue);
however I've got problem here. Received Tag has something about 400 bytes (I've checked it on debug) , but when I convert it , tag is only 16 chars long(8 bytes, there are correct). Why is that ?
public static String bytesToHex(byte[] in) {
final StringBuilder builder = new StringBuilder();
for(byte b : in) {
builder.append(String.format("%02x", b));
}
return builder.toString();
}
// consider using this

How can I validate Android edittext for accepting string and integer

What is the validation expression for string(space)integer? I want to enter the data in the format of "month date"(eg.March 22) in database.
I think it'll help you
String abc = "March 2";
String[] split = abc.split(" ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < split.length; i++) {
sb.append(split[i]);
if (i != split.length - 1) {
sb.append(" ");
}
}
String combined = sb.toString();
At 0 position you'll get your months then get into a String and matches with your static array.
And at 1 position, you'll get your date, you can match it too.

Extract array elements and create a String from them

I have got an array through Vector addittion like
[1!,2!,3!,4!]
I have to convert it into a string like
{1!2!3!4!}
..can you please tell me the name of few methods by which i can make it? Thanks all..
String getElement = null;
for(int j = 0;j<5;j++){
getElement = dynamicViewtagNames.elementAt(j);
}
I can get elements of this array like this..then I have to convert it into a string.
I'm not sure if I understand your question correctly, but if you just want to turn this:
[1!,2!,3!,4!]
into
{1!2!3!4!}
you can for example make use of the String.replace() or String.replaceAll() method.
String str = "[1!,2!,3!,4!]";
str = str.replace("[", "{");
str = str.replace("]", "}");
str = str.replace(",", "");
If [1!,2!,3!,4!] is a Vector containing the Strings you showed us above, you could do it like this using a StringBuffer:
// letz assume this vector has the following content: [1!,2!,3!,4!]
Vector<String> dynamicViewtagNames = new Vector<String>();
StringBuffer b = new StringBuffer();
b.append("{");
for(int i = 0; i < dynamicViewtagNames.size(); i++) {
b.append(dynamicViewtagNames.get(i))
}
b.append("}");
String mystring = b.toString();
Simple Solution
String names = names.replaceAll(",","");
names = names.replaceAll("[", "{");
names = names.replaceAll("]", "}");
Use this code,
StringBuilder str = new StringBuilder();
for (int i = 0; i<dynamicViewtagNames.length;i++){
str.append(dynamicViewtagNames[i])
}
str.toString();
or you can use:
Arrays.toString(dynamicViewtagNames);
Thanks

Categories

Resources