final email = 'abcdefghij#email.com';
final phoneNumber = '0123456789';
This email string convert to this patter like
Fox Example
email -> ab****j#email.com
phoneNumber -> (012)3****89
Please help using RegExp and 0ther technics are most welcome.
Try this way
void main() {
var result = 'nilesh.rathod#gmail.com'.replaceAll(new RegExp('(?<=.)[^#](?=[^#]*?[^#]#)'), '*');
print(result);
}
You can use the replaceRange method
final email = 'abcdefghij#email.com';
final phoneNumber = '0123456789';
final hiderPlaceholder = "****";
final censuredEmail = email.replaceRange(2, email.indexOf("#")-1, hiderPlaceholder);
final censuredPhoneNumber = "(" + phoneNumber.substring(0, 3) + ")" + phoneNumber.substring(3).replaceRange(1, phoneNumber.substring(3).length-2, hiderPlaceholder);
print (censuredEmail);
print (censuredPhoneNumber);
Or you can just go for the evergreen substring
final email = 'abcdefghij#email.com';
final phoneNumber = '0123456789';
final hiderPlaceholder = "****";
final censuredEmail = email.substring(0, 2) + hiderPlaceholder + email.substring(email.indexOf("#")-1);
final censuredPhoneNumber = "(" + phoneNumber.substring(0, 3) + ")" + phoneNumber.substring(3, 4) + hiderPlaceholder + phoneNumber.substring(phoneNumber.length-2);
print (censuredEmail);
print (censuredPhoneNumber);
P.s. obviously, add all the controls you want, e.g. for the length of the email/phone number
Related
I have a list of strings that contain names and a series of numbers, ex: "John James Hollywood 1 2 3".
I want to parse out the names using a loop so that I can scan each part of the text and store it in a new string variable. Then I tried to use a conditional to put any numbers in a different variable. I will calculate the average of the numbers and then print the name and the average of the numbers, so the output looks like this:
John James Hollywood: 2
I don't know whether to use the input string stream or what my first steps are.
public static String parseNameNumbers(String text)
{
Scanner scnr = new Scanner(text);
Scanner inSS = null;
int numSum = 0;
int countNum = 0;
String newText = new String();
String sep = "";
while(scnr.hasNext()) {
if(scnr.hasNextInt()) {
numSum = numSum + scnr.nextInt();
countNum++;
}
else {
newText = newText + sep + scnr.next();
sep = " ";
}
}
return newText + ": " + (numSum/countNum);
}
\In my csv file have two row data :
\a;b;c;d;e;
\1;2;3;4;5;
\How do I make it as below in c#:
\a=1;enter code here
\b=2;enter code here
\c=3;enter code here
\d=4;enter code here
\e=5;enter code here
tldr; you split the text.
got string from csv //e.g. string originalTxt = #"\a;b;c;d;e; \1;2;3;4;5";
split row
rearrange/edit for each word in each row
combine
string resultTxt = "";
string mycodehere ="enter code here ";
string originalTxt = ...; //TODO :: your code for get text from csv
string[2] rows = originalTxt.Split('\'); // split the \ got 2 rows
string[] row1 = rows[0].Split(';'); //split each word
string[] row2 = rows[1].Split(';'); //split each word
for(int i= 0 ; i< row1.Count ; i++){
resultTxt += #"\" + row1[i] + "=" + row2[i] + ";" + mycodehere + " " ;
}
//TODO :: use resultTxt
I want to replace an array of characters with empty space except values inside double quote in a string.
Example
"India & China" relationship & future development
In the above example, I need to replace & but thats not inside any of the double quotes(""). The expected result should be
Result
"India & China" relationship future development
Other Examples of String
relationship & future development "India & China" // Output: relationship future development "India & China"
"relationship & future development India & China // Output: reflect the same input string as result string when the double quote is unclosed.
I have so far done the below logic to replace the characters in a string.
Code
string invalidchars = "()*&;<>";
Regex rx = new Regex("[" + invalidchars + "]", RegexOptions.CultureInvariant);
string srctxtaftrep = rx.Replace(InvalidTxt, " ");
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(#"[ ]{2,}", options);
srctxtaftrep = regex.Replace(srctxtaftrep, #" ");
InvalidTxt = srctxtaftrep;
Here's a non-regex approach using a StringBuilder which should work:
string input = "\"India & China\" relationship & future development";
HashSet<char> invalidchars = new HashSet<char>("()*&;<>");
StringBuilder sb = new StringBuilder();
bool inQuotes = false;
foreach(char c in input)
{
if(c == '"') inQuotes = !inQuotes;
if ((inQuotes && c != '"') || !invalidchars.Contains(c))
sb.Append(c);
}
string output = sb.ToString();
Am newbie here and tried the search, but not quite understood it, so I am thinking to ask to the forum for help.
I want to get the result into the text box from the following code but got an error.
Confused on how to overcome it, appreciate for any help. I believe it was an error on the conversion from linqIgroup to string to be put in textboxt.Text
It's about to display the most word(s) that has been occurred in a text file.
string sentence;
string[] result = {""};
sentence = txtParagraph.Text;
char[] delimiters = new char[] { ' ', '.', '?', '!' };
string[] splitStr = sentence.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
var dic = splitStr.ToLookup(w => w.ToLowerInvariant());
var orderedDic = dic.OrderByDescending(g => g.Count(m=>m.First()).ToString()));
txtFreqWord.Text = orderedDic.ToString();
Try the following to do what you are after. I am using regular expressions aswell.
var resultsList = System.Text.RegularExpressions.Regex.Split("normal text here normal normal".ToLower(), #"\W+")
.Where(s => s.Length > 3)
.GroupBy(s => s)
.OrderByDescending(g => g.Count());
string mostFrequent = resultsList.FirstOrDefault().Key;
To get all of them with their count, do the following :
foreach (var x in resultsList) {
txtFreqWord.Text = txtFreqWord.Text + x.Key + " " + x.Count() +", ";
}
How can I generate a string of fixed length based on the input given in c#.
For example :
string s="1";
string newstring ;
I want the newstring to be "AB_000001";
// Similarly if
s="xyz";
//I want the newstring as
newstring = "AB_000xyz";
String s = "AB_000000";
String newString="xyz";
s = s.Remove(s.Length - newString.Length, newString.Length);
s = s + newString;
string basestr = "AB_000000";
string inp = "xyz";
string res = basestr.Substring(0, basestr.Length - inp.Length) + inp;