c# Replace backslash in string - c#

I am reading the contents of an XLS file. Some older versions of excel add what I assume are escape sequences within the cell that causes me problems upon attempting to create a SQL column of that name.
Example string I have to work with is passed as
\0\0K\0\b\0Job
I would like to return this string as "Job". None of the replace sequences in the following job make a change and it returns as
\0\0K\0\b\0Job
C# does not allow that string to be created manually without escaping it with "\\", which defeats the purpose of what I need.
Any suggestions?
public static string CleanupHeaders(string _Col)
{
// Replace your #"\" parameter with double-escaped: #"\\"
_Col = _Col.Replace(#"\\0K", "");
_Col = _Col.Replace(#"\\b", "");
_Col = _Col.Replace(#"\\0", "");
return _Col;
}

Any suggestions?
I have one, since the string comes in as "\0\0K\0\b\0Job" your string replace methods _Col.Replace shouldn't need to use the literal syntax. In C# the \ is an escape character and the following characters create an escape sequence.
The issue is the verbatim string with the # syntax. Remove that and you get "Job". See this working .NET Fiddle - it has several demonstrations that will easily show you what happens. Take a look # MSDN's reference on string literals for more details.
public static string CleanupHeaders(string _Col)
{
_Col = _Col.Replace("\0K", "");
_Col = _Col.Replace("\b", "");
_Col = _Col.Replace("\0", "");
return _Col;
}
There should be no reason to use them in this situation.
Updated
The OP altered the question a little, so here's an update .NET Fiddle.

It's possible that the thing that's reporting the content of the string to you is reporting actual zero characters as \0 for human readability.
Try removing the #s from your replacement strings there.

The code that you provided should be working exactly as expected as seen in this example.
It's All In How It's Called
Since you mentioned that your initial string isn't being changed, it's worth noting that your replace function actually returns a string and doesn't perform an inline replacement, so you'll need to call it through :
// This will set your original string to the result of your CleanupHeaders() method
yourColumnVariable = CleanupHeaders(yourColumnVariable);
If you do want to perform an inline replacement of your existing string, simply update your CleanupHeaders() method to use a ref parameter to perform an update to the actual reference that was passed in :
// Notice the ref keyword
public static void CleanupHeaders(ref string _Col)
{
_Col = _Col.Replace(#"\0K", "");
_Col = _Col.Replace(#"\b", "");
_Col = _Col.Replace(#"\0", "");
}
and then you could simply call it using :
// This will update the yourColumnVariable to reflect any changes made in the method
CleanupHeaders(ref yourColumnVariable);
You can see an updated example that uses the ref approach here.

If you have a debugger, I would first of all suggest seeing what value the _Col string is when it's passed into your CleanupHeaders() method.
Depending where the string:
"\0\0K\0\b\0Job"
came from; I would imagine it is already escaped when it reaches your code.
The string would look something like this: "\\0\\0K\\0\\b\\0Job" when it reaches your method.
I would suggest 1 of 2 things:
1: Replace all your \ in the string with - or any character of your choice:
_Col = _Col.Replace(#"\\", "-");
And then replace your matching strings that now have the - prefix:
// Replace all your escaped backslashes
_Col = _Col.Replace(#"\\", "-");
// Now replace your new prefix + values you are looking to replace
_Col = _Col.Replace("-0K", "");
_Col = _Col.Replace("-0", "");
_Col = _Col.Replace("-b", "");
return _Col;
Or
2: \0 Is recognised as a character as it's escaped; so it's not being printed/output. Try changing your .Replace(#"\0", ""); with .Replace(#"\\0", ""); like so:
public static string CleanupHeaders(string _Col)
{
_Col = _Col.Replace(#"\\0K", ""); // Replaces the exact already escaped \\ value.
_Col = _Col.Replace(#"\\b", "");
_Col = _Col.Replace(#"\\0", "");
return _Col;
}

Related

Remove substring if exists

I have 3 possible input cases
string input = ""; // expected result: ""
string input = "bar-foo"; // expected result: "foo"
string input = "foo"; // expected result: "foo"
And I have to remove everyting including the first separator char - if exists.
Working approach:
string output = input.Split('-').LastOrDefault();
I want to solve this without Split() - my NOT working approach:
string output = input.Substring(input.IndexOf('-') );
How can I handle the IndexOutOfRangeException / make this code work?
Try to add 1:
string output = input.Substring(input.LastIndexOf('-') + 1);
If there's no - in the input, LastIndexOf returns -1 and so you'll have the entire string.
I've assumed that your are looking for input's suffix, that's why I've put LastIndexOf:
"123-456-789" -> "789"
If you want to cut off the prefix:
"123-456-789" -> "456-789"
please, change LastIndexOf into IndexOf
i think you should use Contains Method to identify - is available or not.
string a = "";
if (a.Contains("-"))
{
string output = input.Substring(input.LastIndexOf('-') + 1);
}
Why not just remove it from the string without checking:
input = input.Replace("-foo", string.Empty);

Trying to remove a hyphen from string . The replace method is not working

I have a string myStringTitle= "Insurance Subsequent Action - Removing Coverage";
I use the method
myStringTitle.replace("-", "");
and also
Regex.Replace(myStringTitle, #"[^-]", ""); <br>
none of them seems to work any reason why?. thanks
Strings are immutable. Replace() will create a new string and you must assign the result to a variable. It does not modify the string in-place.
So, change your code as:
myStringTitle = myStringTitle.Replace("-", "");

Replacing characters in a string with another string

So what I am trying to do is as follows :
example of a string is A4PC
I am trying to replace for example any occurance of "A" with "[A4]" so I would get and similar any occurance of "4" with "[A4]"
"[A4][A4]PC"
I tried doing a normal Replace on the string but found out I got
"[A[A4]]PC"
string badWordAllVariants =
restriction.Value.Replace("A", "[A4]").Replace("4", "[A4]")
since I have two A's in a row causing an issue.
So I was thinking it would be better rather than the replace on the string I need to do it on a character per character basis and then build up a string again.
Is there anyway in Linq or so to do something like this ?
You don't need any LINQ here - String.Replace works just fine:
string input = "AAPC";
string result = input.Replace("A", "[A4]"); // "[A4][A4]PC"
UPDATE: For your updated requirements I suggest to use regular expression replace
string input = "A4PC";
var result = Regex.Replace(input, "A|4", "[A4]"); // "[A4][A4]PC"
This works well for me:
string x = "AAPC";
string replace = x.Replace("A", "[A4]");
EDIT:
Based on the updated question, the issue is the second replacement. In order to replace multiple strings you will want to do this sequentially:
var original = "AAPC";
// add arbitrary room to allow for more new characters
StringBuilder resultString = new StringBuilder(original.Length + 10);
foreach (char currentChar in original.ToCharArray())
{
if (currentChar == 'A') resultString.Append("[A4]");
else if (currentChar == '4') resultString.Append("[A4]");
else resultString.Append(currentChar);
}
string result = resultString.ToString();
You can run this routine with any replacements you want to make (in this case the letters 'A' and '4' and it should work. If you would want to replace strings the code would be similar in structure but you would need to "look ahead" and probably use a for loop. Hopefully this helps!
By the way - you want to use a string builder here and not strings because strings are static which means space gets allocated every time you loop. (Not good!)
I think this should do the trick
string str = "AA4PC";
string result = Regex.Replace(str, #"(?<Before>[^A4]?)(?<Value>A|4)(?<After>[^A4]?)", (m) =>
{
string before = m.Groups["Before"].Value;
string after = m.Groups["After"].Value;
string value = m.Groups["Value"].Value;
if (before != "[" || after != "]")
{
return "[A4]";
}
return m.ToString();
});
It is going to replace A and 4 that hasn't been replaced yet for [A4].

Changing an "." to an "_" in a C# string

I'm developing a piece of software in C# and the end result is an Excel spreadsheet. The title of the spreadsheet is created using several variables to explain exactly what the spreadsheet is. One of the variables is a string which contains data like this:
'1.1.1'
I need to convert it at the point of creating the spreadsheet to be:
'1_1_1'
I have tried using the String.Replace method but it just seems to ignore it. Any ideas?
Best Regards
I bet you doing this:
myString.Replace(".","_");
When you should be doing this:
myString = myString.Replace(".","_");
Remember, in .Net strings are immutable, so any changes result in a new string.
Chances are you're ignoring the result of string.Replace. You need:
text = text.Replace('.', '_');
Just calling Replace doesn't change the existing string - it creates a new string and returns it. Strings are immutable in .NET - they never change after creation.
When you use string.Replace are you remembering that you have to assign it?
yourString.Replace(".", "_");
Will do nothing.
string newString = yourString.Replace(".", "_");
will return the string with the dots replaced with underscores.
If I had to guess, you're not capturing the value returned by String.Replace. Strings are immutable, so String.Replace returns a new string, which you need to store a reference to.
string foo = "1.1.1";
foo = foo.Replace('.', '_');
String input = "1.1.1";
input = input.Replace(".", "_");
strings are immutable, so make sure you're using it like this:
string myString = "1.1.1";
myString = myString.Replace('.', '_');
String.Replace is the right way to do this:
private void button1_Click(object sender, RoutedEventArgs e) {
String myNumbers = "1.1.1";
Console.WriteLine("after replace: " + myNumbers);
myNumbers = myNumbers.Replace(".", "_");
Console.WriteLine("after replace: " + myNumbers);
}
will produce:
after replace: 1.1.1
after replace: 1_1_1

Remove characters after specific character in string, then remove substring?

I feel kind of dumb posting this when this seems kind of simple and there are tons of questions on strings/characters/regex, but I couldn't find quite what I needed (except in another language: Remove All Text After Certain Point).
I've got the following code:
[Test]
public void stringManipulation()
{
String filename = "testpage.aspx";
String currentFullUrl = "http://localhost:2000/somefolder/myrep/test.aspx?q=qvalue";
String fullUrlWithoutQueryString = currentFullUrl.Replace("?.*", "");
String urlWithoutPageName = fullUrlWithoutQueryString.Remove(fullUrlWithoutQueryString.Length - filename.Length);
String expected = "http://localhost:2000/somefolder/myrep/";
String actual = urlWithoutPageName;
Assert.AreEqual(expected, actual);
}
I tried the solution in the question above (hoping the syntax would be the same!) but nope. I want to first remove the queryString which could be any variable length, then remove the page name, which again could be any length.
How can I get the remove the query string from the full URL such that this test passes?
For string manipulation, if you just want to kill everything after the ?, you can do this
string input = "http://www.somesite.com/somepage.aspx?whatever";
int index = input.IndexOf("?");
if (index >= 0)
input = input.Substring(0, index);
Edit: If everything after the last slash, do something like
string input = "http://www.somesite.com/somepage.aspx?whatever";
int index = input.LastIndexOf("/");
if (index >= 0)
input = input.Substring(0, index); // or index + 1 to keep slash
Alternately, since you're working with a URL, you can do something with it like this code
System.Uri uri = new Uri("http://www.somesite.com/what/test.aspx?hello=1");
string fixedUri = uri.AbsoluteUri.Replace(uri.Query, string.Empty);
To remove everything before the first /
input = input.Substring(input.IndexOf("/"));
To remove everything after the first /
input = input.Substring(0, input.IndexOf("/") + 1);
To remove everything before the last /
input = input.Substring(input.LastIndexOf("/"));
To remove everything after the last /
input = input.Substring(0, input.LastIndexOf("/") + 1);
An even more simpler solution for removing characters after a specified char is to use the String.Remove() method as follows:
To remove everything after the first /
input = input.Remove(input.IndexOf("/") + 1);
To remove everything after the last /
input = input.Remove(input.LastIndexOf("/") + 1);
Here's another simple solution. The following code will return everything before the '|' character:
if (path.Contains('|'))
path = path.Split('|')[0];
In fact, you could have as many separators as you want, but assuming you only have one separation character, here is how you would get everything after the '|':
if (path.Contains('|'))
path = path.Split('|')[1];
(All I changed in the second piece of code was the index of the array.)
The Uri class is generally your best bet for manipulating Urls.
To remove everything before a specific char, use below.
string1 = string1.Substring(string1.IndexOf('$') + 1);
What this does is, takes everything before the $ char and removes it. Now if you want to remove the items after a character, just change the +1 to a -1 and you are set!
But for a URL, I would use the built in .NET class to take of that.
Request.QueryString helps you to get the parameters and values included within the URL
example
string http = "http://dave.com/customers.aspx?customername=dave"
string customername = Request.QueryString["customername"].ToString();
so the customername variable should be equal to dave
regards
I second Hightechrider: there is a specialized Url class already built for you.
I must also point out, however, that the PHP's replaceAll uses regular expressions for search pattern, which you can do in .NET as well - look at the RegEx class.
you can use .NET's built in method to remove the QueryString.
i.e., Request.QueryString.Remove["whatever"];
here whatever in the [ ] is name of the querystring which you want to
remove.
Try this...
I hope this will help.
You can use this extension method to remove query parameters (everything after the ?) in a string
public static string RemoveQueryParameters(this string str)
{
int index = str.IndexOf("?");
return index >= 0 ? str.Substring(0, index) : str;
}

Categories

Resources