How would I go about in replacing every character in a string which are not the following:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.#-
with -
So for example, the name Danny D'vito would become DannyD-vito
My inital thought was converting string to char[] and looping through and checking each character, then convert back to string. But my hunch is telling me there must be an easier way to do this
Regex.Replace() approach
string input = "Danny D'vito";
string result = new Regex("[^a-zA-Z0-9_.#-]").Replace(input, "-");
Related
I have a email id like below
string email=test.mail#test.com;
string myText = email.split(".");
i am not sure who split first two characters and followed by two characters after the period or dot.
myText = tema //(desired output)
Use LINQ ;)
string myText = string.Join("", email.Remove(email.IndexOf('#')).Split('.')
.Select(r =>new String(r.Take(2).ToArray())));
First Remove text after #, (including #)
Then split on .
From the returned array take first two characters from each element and convert it to array
Pass the array of characters to String constructor creating a string
using String.Join to combine returned strings element.
Another Linq solution:
string first = new string(email.Take(2).ToArray());
string second = new string(email.SkipWhile(c => c != '.').Skip(1).Take(2).ToArray());
string res = first + second;
string.Join(string.Empty, email.Substring(0, email.IndexOf("#")).Split('.').Select(x => x.Substring(0, 2)));
Lots of creative answers here, but the most important point is that Split() is the wrong tool for this job. It's much easier to use Replace():
myText = Regex.Replace(email, #"^(\w{2})[^.]*\.(\w{2})[^.]*#.+$", "$1$2");
Note that I'm making a lot of simplifying assumptions here. Most importantly, I'm assuming the original string contains the email address and nothing else (you're not searching for it), that the string is well formed (you're not trying to validate it), and that both of substrings you're interested in start with at least two word characters.
How can I concatenate the string "\u" with "a string" to get "\u0000"?
My code creates two backslashes:
string a = #"\u" + "0000"; //ends up being "\\\u0000";
The escape sequence \uXXXX is part of the language's syntax and represents a single Unicode character. By contrast, #"\u" and "0000" are two different strings, with a total of six characters. Concatenating them won't magically turn them into a single Unicode escape.
If you're trying to convert a Unicode code point into a single-character string, do this:
char.ConvertFromUtf32(strUnicodeOfMiddleChar).ToString()
BTW, don't use == true; it's redundant.
If I understand you correctly, I think you want to build a single-char string from an arbitrary Unicode value (4 hex digits). So given the string "0000", you want to convert that into the string "\u0000", i.e., a string containing a single character.
I think this is what you want:
string f = "0000"; // Or whatever
int n = int.Parse(f, NumberStyles.AllowHexSpecifier);
string s = ((char) n).ToString();
The resulting string s is "\u0000", which you can then use for your search.
(With corrections suggested by Thomas Levesque.)
the line below creates tow backslash:
string a = #"\u" + "0000"; //a ends up being "\\u0000";
No, it doesn't; the debugger shows "\" as "\", because that's how you write a backslash in C# (when you don't prefix the string with #). If you print that string, you will see \u0000, not \\u0000.
Nope, that string really has single backslash in. Print it out to the console and you'll see that.
Escape your characters correctly!!
Both:
// I am an escaped '\'.
string a = "\\u" + "0000";
And:
// I am a literal string.
string a = #"\u" + "0000";
Will work just fine. But, and I am going out on a limb here, I am guessing that you are trying to escape a Unicode Character and Hex value so, to do that, you need:
// I am an escaped Unicode Sequence with a Hex value.
char a = '\uxxxx';
My question concerns extracting an integer number from a string (in C#). There is a string which can have an integer positive number in round brackets in the end (without leading zeros), e.g. "This is a string (125)". I would like to write a code validating whether it has such a form, and if so, extracting the number and the rest from it. For example, if the string were "This is a string (125)", the results should be "This is a string" (type: string), and 125 (integer). If the string were "Another example (7)", the results should be "Another example", and 7. Would regex be useful, or should I rather write a parsing function?
If the input value always has same structure (like /type/bracket/value/bracket) then you can do it via:
1) RegEx
2) String.Split()
3) String.IndexOf()
Use the regular expression \((\d})\)$ that will much the number to the end of the string as a group.
string testString = "This is a string (125)asbcd";
string[] stringPart = testString.Split('(', ')');
Here stringPart[0] is the string part, and stringPart[1] is the numeric part.
You can try this
string firstPart = Regex.Match(inputString, #"\(([^)]*)\)").Groups[0].Value;
int number;
Int32.TryParse(Regex.Match(inputString, #"\(([^)]*)\)").Groups[1].Value, out number);
EDIT:
Obviously you can optimise this and not do the Match twice but this shows how to use it.
I'm getting a string as a parameter.
Every string should take 30 characters and after I check its length I want to add whitespaces to the end of the string.
E.g. if the passed string is 25 characters long, I want to add 5 more whitespaces.
The question is, how do I add whitespaces to a string?
You can use String.PadRight for this.
Returns a new string that left-aligns the characters in this string by padding them with spaces on the right, for a specified total length.
For example:
string paddedParam = param.PadRight(30);
You can use String.PadRight method for this;
Returns a new string of a specified length in which the end of the
current string is padded with spaces or with a specified Unicode
character.
string s = "cat".PadRight(10);
string s2 = "poodle".PadRight(10);
Console.Write(s);
Console.WriteLine("feline");
Console.Write(s2);
Console.WriteLine("canine");
Output will be;
cat feline
poodle canine
Here is a DEMO.
PadRight adds spaces to the right of strings. It makes text easier to
read or store in databases. Padding a string adds whitespace or other
characters to the beginning or end. PadRight supports any character
for padding, not just a space.
Use String.PadRight which will space out a string so it is as long as the int provided.
var str = "hello world";
var padded = str.PadRight(30);
// padded = "hello world "
you can use Padding in C#
eg
string s = "Example";
s=s.PadRight(30);
I hope It will resolve your problem.
Say I have a string such as
abc123def456
What's the best way to split the string into an array such as
["abc", "123", "def", "456"]
string input = "abc123def456";
Regex re = new Regex(#"\D+|\d+");
string[] result = re.Matches(input).OfType<Match>()
.Select(m => m.Value).ToArray();
string[] result = Regex.Split("abc123def456", "([0-9]+)");
The above will use any sequence of numbers as the delimiter, though wrapping it in () says that we still would like to keep our delimiter in our returned array.
Note: In the example snippet we will get an empty element as the last entry of our array.
The boundary you look for can be described as "A position where a digit follows a non-digit, or where a non-digit follows a digit."
So:
string[] result = Regex.Split("abc123def456", #"(?<=\D)(?=\d)|(?<=\d)(?=\D)");
Use [0-9] and [^0-9], respectively, if \d and \D are not specific enough.
Add space around digitals, then split it. So there is the solution.
Regex.Replace("abc123def456", #"(\d+)", #" \1 ").Split(' ');
I hope it works.
You could convert the string to a char array and then loop through the characters. As long as the characters are of the same type (letter or number) keep adding them to a string. When the next character no longer is of the same type (or you've reached the end of the string), add the temporary string to the array and reset the temporary string to null.