I am trying to implement a search function that allows the user to search by product tag. The problem is some of the tags are in different formats, like some have spaces and others don't.
At the moment I'm using WHERE modelNumber LIKE '" + '%' + number + '%' + "';"; This will get me the tag if they only fill in some of the tag name, but this won't work if the tag name includes a space but the user does not enter one.
My idea was create a char array with the chars in the search, so I'm wondering if there is a way to check if the SQL column entry contains each character in the way.
Thanks in advance.
You can use Contains to check for space characters:
"abc defg".Contains(' '); // True
There is also Trim, TrimEnd, and TrimStart depending on what you are trying to do.
Related
I not an expert in C# i've only been doing it for a few months.
I have a token #Html.Raw(Model.Prices.SitePrice.Text) that displays a price for a product ($9.99)
I needed to remove the $ sign from the token so i put #Html.Raw(Model.Prices.SitePrice.Text.Replace("$", ""))
This all works fine, The issue is when I apply a special /promotion to that product, it changes the token to display:
<strike>$9.99</strike> $7.99 it should display 7.99 once working.
I want the <stike>$##.##</strike> removed using a wildcard to deal with any number between the strike.
What would be the best way of writing the .replace() for this this text? (has to be inline for google schema data)
Ive tried:
#Html.Raw(Model.Prices.SitePrice.Text.Replace("$", "").Replace("<strike>[\r\n]+</strike> ", ""))
#Html.Raw(Model.Prices.SitePrice.Text.Replace("$", "").Replace("<strike>(.*)</strike> ", ""))
7 different vaules?
<strike>$9.99</strike> $7.99 it should display 7.99
<strike>$10.99</strike> $8.99 it should display 8.99
<strike>$199.00</strike> $188.00 it should display 188.00
<strike>$20.00</strike> $18.50 it should display 18.50
<strike>$4.20</strike> $2.20 it should display 2.20
<strike>$12345.00</strike> $12225.00 it should display 12225.00
#Html.Raw(Regex.Replace(Model.Prices.SitePrice.Text, #"<strike>[\s\S]*</strike>", "").Trim().TrimStart('$'))
One option to consider:
#Html.Raw(new string(Model.Prices.SitePrice.Text.Reverse().TakeWhile(z => z != '$').Reverse().ToArray()))
This will take the text at the end of the SitePrice up until (and not including) the $.
Reverse means it reverses the string. TakeWhile gets you the text up until the $. Reverse reverses it back again. new string converts the char array back to a string.
I'm trying to remove whitespace and trailing white space from right and left side of my string in DB.
Note how the current results looklike:
Note the string named:
*excellent-purchase*
When I fetch it in my C# application like this:
ctx.Users.ToList();
The output for this string that I get is:
\t*excellent-purchase*
I need to remove this "\t" sign from my C# application either on DB level or inside the C# application.
The way I've tried it is like doing it is like this:
UPDATE
TableName
SET
ColumnName = LTRIM(RTRIM(ColumnName))
But I still get this \t sign in my C# app...
How can I fix this?
Edit:
guys I still have a weird characther like this:
"nl_holyland*555*
And in the C# App it looks like:
\"nl_holyland*555*
Theres an extra \ with this solution like
You can try it:
string value= Regex.Replace(value, #"\t|\n|\r", "");
You might want to try the following:
UPDATE
TableName
SET
ColumnName = LTRIM(RTRIM(REPLACE(ColumnName,char(9),'')))
You can use the Replace also:
UPDATE
TableName
SET
ColumnName = REPLACE(ColumnName, ' ', '')
I have followed this Wordpress tutorial which works great. I have used a listview and when i try to format the string it doesn't recognise the \t (but does recognise \n). It also won't recognise String.Format etc.
Is there anyway that I can format the string using tabs or something similar?
Cheers
EDIT
for( i = 0; i < lstView.Items.Count;i++)
{
name = lstView.Items[i].Text;
state = lstView.Items[i].SubItems[1].Text;
country = lstView.Items[i].SubItems[2].Text;
line += name + "\t" + state + "\t" + country + "\n";
}
StringReader reader = new StringReader(line);
When line is used to print the string is joined together so the \t doesn't work. The \n for a new line does work though. Does anyone know any way that I can format the string without using spaces.
The result is like this
NameStateCountry
LongernameStateCountry
anotherNameAnotherStateAnotherCountry
Where I would like them lined up (like in a table) with name one column, state another column and country then third
Any suggestions greatly appreciated
Well, it is a bit odd that tabs are lost, but on the other hand, tabs will probably be problematic if the individual string elements (name, state etc.) varies in length.
What you could do instead is use string.Format() and use fixed column widths.
To get nice visual output this would include a parse step to determine the correct column width.
When this is done, use something like this to use spaces instead of tabs.
string line = string.Format("{0,-20}{1,-20}{2,-20}", "name", "state", "country");
EDIT: Saw that you did not want to use spaces.
In this case, you will probably need to handle this in the printing algorithm itself. You could still separate items with tabs, then for each line split it on tabs, creating an array of items (columns) per line.
For each item, print it using Graphics.DrawString() with a suitable X-position offset.
See the documentation for Graphics.DrawString.
My question may sound weird but that's the scenario i'm at.
I need to parse an Excel formula (getting it from Office.Interop.Excel) and get the parameters from the formula (# of parameter can vary)
There are multiple cases to consider.
E.g.:
1. myformula("param1", "param2")
2. myformula("param1", A2)
3. myformula("param1", , "param3")
4. myformula(A1, , "param3")
5. myformula("param1, has commas in it", "param2")
Is there a nicer way to parse this as opposed to having multiple if branches (especially considering when string array is mixed, i.e. some parameters have quotations and other parameters are reference parameters)?
Using string split(',') doesn't seem to be too helpful as I can have commas in the parameter itself.
I've also tried
string[] paramArray = new string[]{(parameters)}
where parameters = "\"param1\", \"param2\"";
but that didn't seem to work either (it won't work at all if I have an empty parameter but that's another case).
My outcome should be some sort of an array (or list or any other collection) that would contain all of the parameters, and if a parameter didn't have quotation marks in it then I would need to evaluate it.
Any help would be appreciated.
I would process the string looking for " or , if " found search fwd looking for the closing " ignoring any , along the way and taking into account the possibility of impeded "'s in the parameter. When you get the closing " resume looking for " or , and repeat.
Another option would be to use split but then check the results for elements beginning with " . Merge these with the next array element to for the complete parameter (may be that > 2 consecutive array element need to be merged) Also watch out for strings like "zzz"",""xxx" (a single string that = zzz","xxx)
I have one sharepoint application, in this i have to show the current user, i used SPContext.Current.Web.CurrentUser.LoginName. then it returns XXXXXX\abida. But i want only the username like abida. How to achieve this requirement?
Note that we have to escape the slash...
string loginName = SPContext.Current.Web.CurrentUser.LoginName;
string[] loginNameParts = loginName.Split('\\');
string loginNameWithoutDomain = nameParts[1];
I presume you are doing this in order to use the name-only for some reason and that you aren't relying on the user name being unique in its own right. You could have DOMAIN1\BobSmith and DOMAIN2\BobSmith - so if you were using "BobSmith" as a unique user name, you could come unstuck.
you do not. The name is not guaranteed to be unique without the domain prefix. If you erally want to show it without, then just remove it - split the string at the "\" and use the second element. There are multiple ways do do that, from the Split method on the string to using IndexOf for the "\" and then substring to extract the reminder.