Remove substring if exists - c#

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);

Related

How can I add an integer to an existing record?

Please be kind enough to tell me how I can add an integer to an existing record which starts with a string sequence like the following;
S0000 - S00027
Kind Regards,
Indunil Sanjeewa
Try this code:
string record = "S00009";
string recordPrefix = "S";
char paddingCharacter = '0';
string recordNoPart = record.Substring(recordPrefix.Length);
int nextRecordNo = int.Parse(recordNoPart) + 1;
string nextRecord = string.Format("{0}{1}", recordPrefix, nextRecordNo.ToString().PadLeft(record.Length - recordPrefix.Length, paddingCharacter));
#kurakura88 has already given the logic. I have just provided the hardcore c# code.
The logic is:
separate the "S00009" into "S" and "00009". Use string method Substring()
Parse "00009" into integer. Use Int.Parse or Int.TryParse
Add 1 into the integer
Print back the "S" and the integer. Use string.Concat or simply string + integer
You can use following approach
string input = #"S0000";
string pattern = #"\d+";
string format = #"0000";
int addend = 1;
string result = Regex.Replace(input, pattern,
m => (int.Parse(m.Value) + addend).ToString(format));
// result = S0001
Regular expression \d+ matches all digits.
MatchEvaluator converts matched value to integer. Then adds the addend. Then converts the value to a string using the specified format.
In the end using the Replace method replaces the previous value with the new value.

c# Replace backslash in string

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;
}

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].

How to remove characters from a string to get the desired format?

I have a string representation exactly like 'ComputerName -- IPAddress'; i.e:
'samarena -- 192.168.1.97'
. I want to get only the 'ComputerName' part from the actual representation by removing other characters. I'm actually quite beginner in using string.FormatMethods() .
Please help me out.
Thanks.
This should do it:
string test = "samarena -- 192.168.1.97";
var result = test.Split(new string[] { "--" }, StringSplitOptions.None)[0].Trim();
Result will equal samarena
you could split the string on ' -- ' and then use the first part
This should do it.
var yourString = "samarena -- 192.168.1.97";
var indexOfDash = yourString.IndexOf("-");
var yourComputerName = yourString.SubString(0, indexOfDash).Trim();
But the other answers using Trim are better :)
This'd be the totally imperative way.
If You are sure there is always a substring " -- " after the part You want, You can do this
myString.Substring(0, myString.IndexOf(" -- "))
Or use a shorter part of " -- ".
Try this:
char [] chars = {'-'};
string test = "samarena -- 192.168.1.97";
//computerName array will have the Computer Name at the very first index (it is a zero based index
string[] computerName = test.Split(chars,StringSplitOptions.RemoveEmptyEntries);
//computerName[0] is your computerName

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