Created
May 4, 2020 19:24
-
-
Save ZahidRasheed/013d40048a1cf571fa2615709ba94ac4 to your computer and use it in GitHub Desktop.
String extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Color | |
import android.annotation.SuppressLint | |
import android.content.Context | |
import org.json.JSONArray | |
import org.json.JSONException | |
import org.json.JSONObject | |
import java.security.MessageDigest | |
import java.util.regex.Pattern | |
val String.creditCardFormatted: String | |
get() { | |
val preparedString = replace(" ", "").trim() | |
val result = StringBuilder() | |
for (i in preparedString.indices) { | |
if (i % 4 == 0 && i != 0) { | |
result.append(" ") | |
} | |
result.append(preparedString[i]) | |
} | |
return result.toString() | |
} | |
val String.jsonObject: JSONObject? | |
get() = try { | |
JSONObject(this) | |
} catch (e: JSONException) { | |
null | |
} | |
val String.jsonArray: JSONArray? | |
get() = try { | |
JSONArray(this) | |
} catch (e: JSONException) { | |
null | |
} | |
@SuppressLint("ApplySharedPref") | |
fun String.save( | |
applicationContext: Context, | |
value: Map<String, Any>, | |
clear: Boolean = false, | |
now: Boolean = false | |
) { | |
val sp = applicationContext.getSharedPreferences(this, Context.MODE_PRIVATE).edit() | |
if (clear) | |
sp.clear() | |
value.keys.forEach { key -> | |
val v = value[key] | |
if (v != null) { | |
when (v) { | |
is String -> sp.putString(key, v) | |
is Float -> sp.putFloat(key, v) | |
is Long -> sp.putLong(key, v) | |
is Int -> sp.putInt(key, v) | |
is Boolean -> sp.putBoolean(key, v) | |
} | |
} | |
} | |
if (now) | |
sp.commit() | |
else | |
sp.apply() | |
} | |
fun String.load(applicationContext: Context): Map<String, Any> { | |
val sp = applicationContext.getSharedPreferences(this, Context.MODE_PRIVATE) | |
val keys = sp.all.keys | |
val result = hashMapOf<String, Any>() | |
keys.map { key -> | |
val v = sp.all[key] | |
if (v != null) | |
result[key] = v | |
} | |
return result | |
} | |
val String.asColor: Int? | |
get() = try { | |
Color.parseColor(this) | |
} catch (e: java.lang.IllegalArgumentException) { | |
null | |
} | |
val String.lastPathComponent: String | |
get() { | |
var path = this | |
if (path.endsWith("/")) | |
path = path.substring(0, path.length - 1) | |
var index = path.lastIndexOf('/') | |
if (index < 0) { | |
if (path.endsWith("\\")) | |
path = path.substring(0, path.length - 1) | |
index = path.lastIndexOf('\\') | |
if (index < 0) | |
return path | |
} | |
return path.substring(index + 1) | |
} | |
val String.sha1: String | |
get() { | |
val bytes = MessageDigest.getInstance("SHA-1").digest(this.toByteArray()) | |
return bytes.joinToString("") { | |
"%02x".format(it) | |
} | |
} | |
val String.md5: String | |
get() { | |
val bytes = MessageDigest.getInstance("MD5").digest(this.toByteArray()) | |
return bytes.joinToString("") { | |
"%02x".format(it) | |
} | |
} | |
fun String.isEmailValid(): Boolean { | |
val expression = "^[\\w.-]+@([\\w\\-]+\\.)+[A-Z]{2,8}$" | |
val pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE) | |
val matcher = pattern.matcher(this) | |
return matcher.matches() | |
} | |
val String.containsLatinLetter: Boolean | |
get() = matches(Regex(".*[A-Za-z].*")) | |
val String.containsDigit: Boolean | |
get() = matches(Regex(".*[0-9].*")) | |
val String.isAlphanumeric: Boolean | |
get() = matches(Regex("[A-Za-z0-9]*")) | |
val String.hasLettersAndDigits: Boolean | |
get() = containsLatinLetter && containsDigit | |
val String.isIntegerNumber: Boolean | |
get() = toIntOrNull() != null | |
val String.toDecimalNumber: Boolean | |
get() = toDoubleOrNull() != null | |
/* | |
implementation 'io.michaelrocks:libphonenumber-android:8.12.2' | |
import android.content.Context | |
import io.michaelrocks.libphonenumber.android.PhoneNumberUtil | |
fun String.formatPhoneNumber(context: Context, region: String): String? { | |
val phoneNumberKit = PhoneNumberUtil.createInstance(context) | |
val number = phoneNumberKit.parse(this, region) | |
if (!phoneNumberKit.isValidNumber(number)) | |
return null | |
return phoneNumberKit.format(number, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL) | |
}*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment