Skip to content

Instantly share code, notes, and snippets.

@mohamedagamy
Last active October 13, 2020 18:13
Show Gist options
  • Select an option

  • Save mohamedagamy/ada3e2f8e1fb87f8c28f8e36aafa62ab to your computer and use it in GitHub Desktop.

Select an option

Save mohamedagamy/ada3e2f8e1fb87f8c28f8e36aafa62ab to your computer and use it in GitHub Desktop.
fun String.formatCreditCardNumber(): String {
val delimiter = ' '
return this.replace(".{4}(?!$)".toRegex(), "$0$delimiter")
}
fun Float.formatPriceFloat(): String {
val isEnglish = Utils.getLang()
val symbols = DecimalFormatSymbols(Locale.US)
val decimalFormat = DecimalFormat("#,###.000",symbols)
val output = if(isEnglish) decimalFormat.format(this) else
decimalFormat.format(this).convertEnglishNumbersToArabic()
return output
}
fun Float.formatPriceInt(): String {
val isEnglish = Utils.getLang()
val symbols = DecimalFormatSymbols(Locale.US)
val decimalFormat = DecimalFormat("#,###",symbols)
val output = if(isEnglish) decimalFormat.format(this.toInt()) else
decimalFormat.format(this.toInt()).convertEnglishNumbersToArabic()
return output
}
fun String.formatStringLocalized(): String {
val isEnglish = Utils.getLang()
val input = this.formatStringEnglish()
if (isEnglish) return input
else return input.convertEnglishNumbersToArabic()
}
//you need to use this workaround to avoid arabic comma in Numbers
//so you replace all english numbers with arabic numbers
fun String.convertEnglishNumbersToArabic(): String {
return this.replace("1".toRegex(), "١").replace("2".toRegex(), "٢")
.replace("3".toRegex(), "٣").replace("4".toRegex(), "٤")
.replace("5".toRegex(), "٥").replace("6".toRegex(), "٦")
.replace("7".toRegex(), "٧").replace("8".toRegex(), "٨")
.replace("9".toRegex(), "٩").replace("0".toRegex(), "٠")
}
private fun String.formatStringEnglish(): String {
val decimalFormat = NumberFormat.getNumberInstance(Locale.ENGLISH)
val result = decimalFormat.format(this.toInt())
return result
}
//*************************************************************
fun String.textOnly(): Boolean {
//not allowed number arabic/english ,not allowed special chars
return this.matches("^[^0-9\u0660-\u0669^~|<>{}¥€£@#&*+=()_\$\";.?!%]+$".toRegex())
}
fun String.digitsOnly(): Boolean {
//arabic,english numbers only
return this.matches("^[0-9\u0660-\u0669]+\$".toRegex())
}
fun String.alphaNumericOnly(): Boolean {
//arabic & english => numbers & characters also no spaces allowed
//this.matches("^(?=.*[\\u0660-\\u0669])(?=.*[\\u0621-\\u064A0]).{6,}\$".toRegex())
return this.matches("^(?=.*[0-9])(?=.*[a-zA-Z]).{6,}\$".toRegex())
}
fun String.isArabicChars(): Boolean {
//arabic characters and numbers
return this.matches("^[\\u0621-\\u064A\\u0660-\\u0669 ]+\$".toRegex())
}
fun String.encodeBase64(): String {
return Base64.encodeToString(toByteArray(), Base64.NO_WRAP)
}
fun String.decodeBase64(): String {
return Base64.decode(this.toByteArray(), Base64.NO_WRAP).toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment