Formatting the String, Object C versus Java [duplicate] - android

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Inserting a Java string in another string without concatenation?
In Object C , the string formatting is like this
url = [NSString stringWithFormat:#"%#devices.json?user=%#&pswd=%#",server_name, user,password];
What about in Java for Android?
How does it handle that?

You can concatenate the strings or use String.format(String format, Object... args)
Concatenate
String url = server_name + "devices.json?user=" + user + "&pswd=" + password
Format
String url = String.format("%sdevices.json?user=%spswd=%s",server_name,user,password);
It is a matter of preference for which one you prefer.
Look up java.util.Formatter for a table that demonstrates other conversion types. %s is for string, %d for int ...

Related

How can i get sub string that is after three #? [duplicate]

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"

how do I save a part of a variable string? [duplicate]

This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 6 years ago.
I get a response but it's different for every user.
I want to extract the UserID & Name. How can I do this?
The response String:
{
"sessionkey":"f4b54dedlfkjhgdlfkjghdfslkjgh1a255242bf71597f4b35ac882f0702",
"user" : {
"userID" : "1",
"name":"Test"
}
}
I want to save 1 = (String userid)
and save Test = (String name)
Thanks in advance
This is just a simple JSON blob, so use a JSON Parser to extract the data.
My lib of choice is org.json.json.
String blob = "{ origin string }";
// create a new json object from the main blob
JSONObject root = new JSONObject(blob);
// extract the session key
String sessionKey = root.getString("sessionkey");
// extract the user object
JSONObject user = root.getJSONObject("user");
// extract the user id
String userId = user.getString("userID");

How do I convert string builder to string array in Android? [duplicate]

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");

How to convert a string mathematical expression into a BigInteger? [duplicate]

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);

Comparing strings that use the substring method [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I hope somebody could give me an explaination why the below code wont work:
//Why doesnt this work
String l = myString.substring(cut, lengthLastBtn-1);
String c = myString.substring(cut, lengthLastBtn-1);
if(l==c){
Log.i(TAG, "Correct");
}
//End
//This work!
String l = "hi";
String c = "hi";
if(l==c){
Log.i(TAG, "Correct");
}
// End
// Or if i want the Vars as in the first code i have to use the if statement like this
if(l.contains(c)){
Log.i(TAG, "Correct");
}
//End
So, why cant a compare a string when i have used the substring method on it. I even see in the log for the strings that they are the same, or have the same text at least.
When you use the “==“ operator with String`s, it means a comparison between objects, not the value that objects hold.
In order to compare Strings values , you should use the built-in method equals. The result is true if the String object represents the same sequence of characters.
if(string1.equals(string2)) {
//Match
}

Categories

Resources