c# replace each instance of a character selection in a string - c#

I've found many references to do similar to this but none seem to be exactly what I'm after, so hoping someone could help.
In simple terms, I want to take a string entered by a user (into a Winform input), and firstly strip out any blanks, then replace any of a list of 'illegal' characters with the UK currency symbol (£). The requirement is for the input to be used but the file that is generated by the process has the modified filename.
I wrote a function (based on an extension method) but it's not working quite as expected:
public static class ExtensionMethods
{
public static string Replace(this string s, char[] separators, string newVal)
{
var temp = s.Split(separators, StringSplitOptions.RemoveEmptyEntries);
return String.Join(newVal, temp);
}
}
public static string RemoveUnwantedChars(string enteredName, char[] unwanted, string rChar)
{
return enteredName.Replace(unwanted, rChar);
}
Which in my code, I've called twice:
char[] blank = { ' ' };
string ename = Utilities.RemoveUnwantedChars(this.txtTableName.Text, blank, string.Empty);
char[] unwanted = { '(', ')', '.', '%', '/', '&', '+' };
string fname = Utilities.RemoveUnwantedChars(ename, unwanted, "£");
If I enter a string that contains at least one space, all of the characters above and some other letters (for example, " (GH) F16.5% M X/Y&1+1"), I get the following results:
ename = "(GH)F16.5%MX/Y&1+1" - this is correct in that it has removed the blanks.
fname = "GH£F16£5£MX£Y£1£1" - this hasn't worked correctly in that it has not replaced the first character but removed it.
The rest of the characters have been correctly replaced. It only occurs when one of the 'illegal' characters is at the start of the string - if my string was "G(H) F16.5% M X/Y&1+1", I would correctly get "G£H£F16£5£MX£Y£1£1". It also replaces multiple 'illegal' characters with one '£', so "M()GX+.1" would become "M£GX£1" but should be "M££GX££1".

I think the problem is in your Replace extension. You are splitting in this line
var temp = s.Split(separators, StringSplitOptions.RemoveEmptyEntries);
You are removing empty entries causing the unexpected result. Use this instead:
var temp = s.Split(separators, StringSplitOptions.None);

The problem is occuring because string.Join() only puts separators between substrings - it will never put one at the start.
One possible solution is to avoid using string.Join() and write Replace() like this instead:
public static class ExtensionMethods
{
public static string Replace(this string s, char[] separators, string newVal)
{
var sb = new StringBuilder(s);
foreach (char ch in separators)
{
string target = new string(ch, 1);
sb.Replace(target, newVal);
}
return sb.ToString();
}
}

When you use split method in your Replace function you get following strings:
GH, F16, 5, MX, Y, 1, 1.
When you join them with your newVal you get:
GH + newVal + F16 + newVal + ... thus omitting first replaced character.
You would probably need some special case to check if first char is "illegal" and put newVal at start of your string.

Related

C# String.Trim() not removing characters from MailMessage.Subject

I'm trying to eliminate certain symbols from the Subject property of a MailMessage object. What I'm experiencing is that it does nothing. Even after assigning Subject to a string, and trimming that, the final Subject still has the symbols in it. (not showed in example)
MailMessage mailMessage = new MailMessage
{
From = new MailAddress(mail.SenderEmailAddress),
SubjectEncoding = System.Text.Encoding.UTF8,
Subject = mail.Subject.Trim(new char[] {}), //symbol list, like ":", "~", ">"
Body = mail.Body
};
String path = #"C:\Users\" + Environment.UserName + #"\Documents\EML\";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = #"C:\Users\" + Environment.UserName + #"\Documents\EML\"
+ mailMessage.Subject + ".eml";
MessageBox.Show(path);
The message box is just to see whether the symbol gets removed or not at the moment, path will be put into a method later.
mail has subject RE: dog, .Trim tries to remove :,
MessageBox shows C:\Users\user\Documents\EML\RE: dog.eml.
The String.Trim(Char[]) method, as per official MSDN documentation, removes all leading and trailing occurrences of a set of characters specified in an array from the current string object. If you want to remove all the occurrences of a specified list of characters from the string, even when they don't appear at the beginning or at the end of it, you may want to use a different approach.
Given the following example string and the following replacements:
String text = "This is: the~ mail sub~ject!";
Char[] replacements = new Char[] { ':', '~' };
you can perform this operation using various approaches. Here is a list containing a few of them:
1) Using String.Split and String.Join
text = String.Join(String.Empty, text.Split(replacements));
2) Using LINQ
text = new String
(
(from c in text
where !replacements.Contains(c)
select c).ToArray()
);
or:
text = new String(text.Where(c => !replacements.Contains(c)).ToArray());
3) Using Regular Expressions
text = Regex.Replace(text, "[:~]", String.Empty);
4) Using a Loop and String.Replace
foreach (Char c in replacements)
text = text.Replace(c.ToString(), String.Empty);
5) Using an Extension Method
public static String RemoveChars(this String input, params Char[] chars)
{
StringBuilder builder = new StringBuilder();
for (Int32 i = 0; i < input.Length; ++i)
{
if (!chars.Contains(input[i]))
builder.Append(input[i]);
}
return builder.ToString();
}
text = text.RemoveChars(replacements);
The final output is always the same:
This is the mail subject!
From MSDN:
String.Trim Method () - Removes all leading and trailing white-space characters from the current String object.
So, Trim isn't going to remove characters from the middle of a String. Commenters suggested using Replace instead, but there isn't a signature that takes an array of characters like you are using. An easy way around that is Extension methods.
class Program
{
static void Main(string[] args)
{
string text = "This:is~a>test";
string subject = text.ReplaceFromCollection(new char[] { ':', '~', '>'}); //symbol list, like ":", "~", ">"
Console.WriteLine($"{text}\n{subject}");
Console.ReadLine();
}
}
static class Extensions
{
public static String ReplaceFromCollection(this string text, IEnumerable<char> characters)
{
foreach (var chr in characters)
{
text = text.Replace(chr.ToString(), String.Empty);
}
return text;
}
}
Using this, each character in your string that matches a character in the array is replaced with the empty String one by one. The result is then passed back.
More reading on Extension Methods.

Get everything before dot or comma c#

how can I get a substring of everything before dot or comma?
For example:
string input = "2.1";
int charLocation = text.IndexOf(".", StringComparison.Ordinal);
string test = input.Substring(0, charLocation );
but what if I have an input = "2,1" ?
I would like to do it in one method, not using twice a substring (once for dot and once for comma)?
string test = input.Split(new Char[] { ',', '.' })[0];
This will split the string for either comma or period...
input.Split(',','.');
Use the IndexOfAny function. It allows you to specify a list of characters to look for, rather than just a single character. You could then make a substring up to the return value of that function.
e.g.
char[] chars = { '.', ',' }
String out = s.Substring(0,s.IndexOfAny(chars));

Fastest way to remove the leading special characters in string in c#

I am using c# and i have a string like
-Xyz
--Xyz
---Xyz
-Xyz-Abc
--Xyz-Abc
i simply want to remove any leading special character until alphabet comes , Note: Special characters in the middle of string will remain same . What is the fastest way to do this?
You could use string.TrimStart and pass in the characters you want to remove:
var result = yourString.TrimStart('-', '_');
However, this is only a good idea if the number of special characters you want to remove is well-known and small.
If that's not the case, you can use regular expressions:
var result = Regex.Replace(yourString, "^[^A-Za-z0-9]*", "");
I prefer this two methods:
List<string> strings = new List<string>()
{
"-Xyz",
"--Xyz",
"---Xyz",
"-Xyz-Abc",
"--Xyz-Abc"
};
foreach (var s in strings)
{
string temp;
// String.Trim Method
char[] charsToTrim = { '*', ' ', '\'', '-', '_' }; // Add more
temp = s.TrimStart(charsToTrim);
Console.WriteLine(temp);
// Enumerable.SkipWhile Method
// Char.IsPunctuation Method (se also Char.IsLetter, Char.IsLetterOrDigit, etc.)
temp = new String(s.SkipWhile(x => Char.IsPunctuation(x)).ToArray());
Console.WriteLine(temp);
}

Get a file/directory path from a string that contains other data

How would I get the directory and filename from the following string using C#:
string test = "test#test.com, snap555.jpg, c:\users\test\desktop\snap555.jpg";
I only want to be able to get the "c:\users\test\desktop\snap555.jpg" from the string and turn it into another string.
The characters before the "," will always be different and different lengths such as: "bob#bob.com, x.txt, c:\users\test\desktop\x.txt"
What is the easiest way to do this in c#?
Thanks.
t
If the comma delimiter does not appear in the first part, you can use:
string pathName = test.Split(',')[2];
If that space after the comma is a problem and assuming that the first part never has spaces, you can use:
char[] delims = new char[] { ',', ' '};
string pathName = test.Split(delims, StringSplitOptions.RemoveEmptyEntries)[2];
This example assumes you always want the third item, as mentioned in your question. Otherwise, change the 2 in the examples above to the correct index of the item you want. For example, if it should always be the last item, you can do:
char[] delims = new char[] { ',', ' '};
string[] items = test.Split(delims, StringSplitOptions.RemoveEmptyEntries);
string pathName = items[items.Length-1];
If there's always going to be a comma there you can use
string test = "snap555.jpg, c:\users\test\desktop\snap555.jpg";
string[] parts = test.Split(',');
Then get whatever you need from the parts array.

String.Replace(char, char) method in C#

How do I replace \n with empty space?
I get an empty literal error if I do this:
string temp = mystring.Replace('\n', '');
String.Replace('\n', '') doesn't work because '' is not a valid character literal.
If you use the String.Replace(string, string) override, it should work.
string temp = mystring.Replace("\n", "");
As replacing "\n" with "" doesn't give you the result that you want, that means that what you should replace is actually not "\n", but some other character combination.
One possibility is that what you should replace is the "\r\n" character combination, which is the newline code in a Windows system. If you replace only the "\n" (line feed) character it will leave the "\r" (carriage return) character, which still may be interpreted as a line break, depending on how you display the string.
If the source of the string is system specific you should use that specific string, otherwise you should use Environment.NewLine to get the newline character combination for the current system.
string temp = mystring.Replace("\r\n", string.Empty);
or:
string temp = mystring.Replace(Environment.NewLine, string.Empty);
This should work.
string temp = mystring.Replace("\n", "");
Are you sure there are actual \n new lines in your original string?
string temp = mystring.Replace("\n", string.Empty).Replace("\r", string.Empty);
Obviously, this removes both '\n' and '\r' and is as simple as I know how to do it.
If you use
string temp = mystring.Replace("\r\n", "").Replace("\n", "");
then you won't have to worry about where your string is coming from.
One caveat: in .NET the linefeed is "\r\n". So if you're loading your text from a file, you might have to use that instead of just "\n"
edit> as samuel pointed out in the comments, "\r\n" is not .NET specific, but is windows specific.
What about creating an Extension Method like this....
public static string ReplaceTHAT(this string s)
{
return s.Replace("\n\r", "");
}
And then when you want to replace that wherever you want you can do this.
s.ReplaceTHAT();
Best Regards!
Here is your exact answer...
const char LineFeed = '\n'; // #10
string temp = new System.Text.RegularExpressions.Regex(
LineFeed
).Replace(mystring, string.Empty);
But this one is much better... Specially if you are trying to split the lines (you may also use it with Split)
const char CarriageReturn = '\r'; // #13
const char LineFeed = '\n'; // #10
string temp = new System.Text.RegularExpressions.Regex(
string.Format("{0}?{1}", CarriageReturn, LineFeed)
).Replace(mystring, string.Empty);
string temp = mystring.Replace("\n", " ");
#gnomixa - What do you mean in your comment about not achieving anything? The following works for me in VS2005.
If your goal is to remove the newline characters, thereby shortening the string, look at this:
string originalStringWithNewline = "12\n345"; // length is 6
System.Diagnostics.Debug.Assert(originalStringWithNewline.Length == 6);
string newStringWithoutNewline = originalStringWithNewline.Replace("\n", ""); // new length is 5
System.Diagnostics.Debug.Assert(newStringWithoutNewline.Length == 5);
If your goal is to replace the newline characters with a space character, leaving the string length the same, look at this example:
string originalStringWithNewline = "12\n345"; // length is 6
System.Diagnostics.Debug.Assert(originalStringWithNewline.Length == 6);
string newStringWithoutNewline = originalStringWithNewline.Replace("\n", " "); // new length is still 6
System.Diagnostics.Debug.Assert(newStringWithoutNewline.Length == 6);
And you have to replace single-character strings instead of characters because '' is not a valid character to be passed to Replace(string,char)
I know this is an old post but I'd like to add my method.
public static string Replace(string text, string[] toReplace, string replaceWith)
{
foreach (string str in toReplace)
text = text.Replace(str, replaceWith);
return text;
}
Example usage:
string newText = Replace("This is an \r\n \n an example.", new string[] { "\r\n", "\n" }, "");
Found on Bytes.com:
string temp = mystring.Replace('\n', '\0');// '\0' represents an empty char

Categories