I need to mask a string field for example phone number "0144567890" into "014XXXX890". Only first three and last three character need to remained, others turn it into "X".Also I wish to make it dynamically which can mask any lengths of string.Thanks.
If you know that the maximum length of the string is say 10,
use this 'XXXXXXXXXX' as a pattern from which to extract the Xs like this:
select
substr(col, 1, 3) ||
substr('XXXXXXXXXX', 1, length(col) - 6) ||
substr(col, length(col) - 2)
from tablename
col is the name of the column.
Related
"\n".join([string[i:i+max_width] for i in range(0, len(string), max_width)])
here,
String - Input String
max_width = a integer value
Ex - input- abcdefghij
to
output -
ab
cd
ef
gh
ij
Ok, so:
range(0, len(string), max_width)
this returns an iterable of integers starting from 0, ending at len(string) - 1, with a stride of max_width, so for max_width = 3, it would give us 0, 3, 6, 9, ...
string[a:b]
is a slicing syntax, so
'abcdefgh'[2:5] = 'cde'
Finally, the join function inserts a newline character between all of those arrays.
Given a String this Code consists of different parts:
string[i:i+max_width] accesses several characters within the string (depending on max_width) and returns them. Join joins these returns together with a newline inbetween. This happens for range(0, len(string), max_width) meaning, that we range from 0 to len(string)-1 with steps of size max_width, which is 2 in this example. This means for your input we take every two chars of the string and seperate them by newline.
For example, If my string was 'HelloWorld'
I want the output to be ######orld
My Code:
myString = 'ThisIsAString'
hashedString = string.replace(string[:-4], '#')
print(hashedString)
Output >> #ring
I expected the output to have just one # symbol since it is replacing argument 1 with argument 2.
Can anyone help me with this?
You could multiply # by the word length - 4 and then use the string slicing.
myString = 'HelloWorld'
print('#' * (len(myString) - 4) + myString[-4:])
myString = 'ThisIsAString'
print('#' * (len(myString) - 4) + myString[-4:])
string.replace(old, new) replaces all instances of old with new. So the code you provided is actually replacing the entire beginning of the string with a single pound sign.
You will also notice that input like abcdabcd will give the output ##, since you are replacing all 'abcd' substrings.
Using replace, you could do
hashes = '#' * len(string[:-4])
hashedString = string.replace(string[:-4], hashes, 1)
Note the string multiplication to get the right number of pound symbols, and the 1 passed to replace, which tells it only to replace the first case it finds.
A better method would be to not use replace at all:
hashes = '#' * (len(string) - 4)
leftover = string[-4:]
hashedString = hashes + leftover
This time we do the same work with getting the pound sign string, but instead of replacing we just take the last 4 characters and add them after the pound signs.
I am trying to find out how to find the amount of unique letters in a string.
I know how to find the amount of unique characters in a string by using the code below.
But what if I want to find the amount of unique letters, not characters, excluding punctuation,in the string?
import string
s = 'AabC'
s = s.lower()
print(sum(1 for c in string.ascii_lowercase if s.count(c) == 1))
First, you can filter out all non-letter characters, then you can convert it into a set and check the length.
s = 'AabC123qwer!!>>??'
unique = set(filter(str.isalpha, s.lower()))
print(len(unique))
7
I want to count a long string contain how many substring, how to do it in python?
"12212"
contains 2x "12"
how to get the count number?
It must allow for overlaping substrings; for instance "1111" contains 3 "11" substrings.
"12121" contains 2 "121" substrings.
"1111".count("11")
will return 2. It does not count any overlaps.
Strings have a method count
You can do
s = '12212'
s.count('12') # this equals 2
Edited for the changing question, the answer below was posted as a comment by tobias_k
To count with overlap,
count_all = lambda string, sub: sum(string[i:i+len(sub)] == sub for i in range(len(string) - len(sub) + 1))
This can be called with,
count_all('1111', '11') # this returns 3
I'm struggling to combine two expression into one so that I can remove trailing 'mm' chars of a varchar column which holds values like 3.214mm and use those chars as numeric values.
The problem I have is that there can be also null or empty string values and I can't find a way to combine both expressions below.
Example: SQLfiddle
DECLARE #string varchar(128)
SET #string = '4.123mm'
SELECT ISNULL(NULLIF(#string,''),NULL) As MyString ;
DECLARE #createNumber varchar(128)
SET #createNumber = '4.123mm'
select LEFT(#createNumber, NULLIF (LEN(#createNumber) - 2, - 1))As MyNumber
DECLARE #createNumber varchar(128)
SET #createNumber = ''
select reverse(stuff(reverse(#createNumber), 1,2, ''))
This will return null if createnumber is shorter than 2 characters.
One way to handle mm/''/null;
select cast(isnull(replace('0' + #createNumber, 'mm', ''), 0) as decimal(6,3))