0

I'm trying to reformat a string in java and remove the final decimal out of an android version number, so 4.2.2 would be formatted to 4.2.

Would I use REGEX to get the desired format for the android version number?

Here's my code for finding the android version number:

String osRelease = Build.VERSION.RELEASE;

https://i.stack.imgur.com/5wIeI.jpg [Error Log File From Eclipse]

pHorseSpec
  • 1,246
  • 5
  • 19
  • 48

1 Answers1

0

Just use some String manipulation:

String i = "4.2.2";
String s = i.substring(0, Math.min(s.length(), 3));
System.out.println(s);

This will give you the first 3 characters (ie. "4.2") of your input String).

If you have your value as a Double or similar, use String.valueOf(myDouble) to convert to String.


Math.min() trick is from here.

Community
  • 1
  • 1
Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
  • I don't get any errors in my code when I run this, but my app won't start with this code included. – pHorseSpec May 01 '15 at 20:28
  • @pHorseSpec, Do you see anything in LogCat when you try to start app ? Try printing out the version without modifying it. – Jonas Czech May 01 '15 at 20:42
  • based on the value returned, a certain picture is displayed. Here's the code for the if statement: `if (MainActivity.osReleaseDouble>4.3){mPhotoPool.add(R.drawable.download);}else{mPh‌​otoPool.add(R.drawable.negative);}` My error log file is in the above question. – pHorseSpec May 02 '15 at 02:26
  • @pHorseSpec, why do you even use a double ? Just get the a api level via `Build.SDK_INT` and use that. That's what you're supposed to do. – Jonas Czech May 02 '15 at 06:41