Is this what you're after?
def padString(id: Int, chars: String, length: Int): String =
chars * length + id.toString
padString(123, "0", 10)
res4: String = 00123
Mike
On Thu, Aug 25, 2016 at 12:39 PM, Mich Talebzadeh wrote:
> Thanks Mike.
>
> Can one turn the first example into
Thanks Mike.
Can one turn the first example into a generic UDF similar to the output
from below where 10 "0" are padded to the left of 123
def padString(id: Int, chars: String, length: Int): String =
(0 until length).map(_ =>
chars(Random.nextInt(chars.length))).mkString + id.toString
sca
Are you trying to always add x numbers of digits / characters or are you
trying to pad to a specific length? If the latter, try using format
strings:
// Pad to 10 0 characters
val c = 123
f"$c%010d"
// Result is 000123
// Pad to 10 total characters with 0's
val c = 123.87
f"$c%010.2f"
//
Ok I tried this
def padString(s: String, chars: String, length: Int): String =
| (0 until length).map(_ =>
chars(Random.nextInt(chars.length))).mkString + s
padString: (s: String, chars: String, length: Int)String
And use it like below:
Example left pad the figure 12345.87 with 10 "0"s
Hi,
This UDF on substring works
scala> val SubstrUDF = udf { (s: String, start: Int, end: Int) =>
s.substring(start, end) }
SubstrUDF: org.apache.spark.sql.expressions.UserDefinedFunction =
UserDefinedFunction(,StringType,Some(List(StringType,
IntegerType, IntegerType)))
I want something similar