c# replace inside textbox string - c#

Hy I have an application that gets his data pushed now from a other system. To communicate this with an api the , needs to be removed from the string.
eg:
var AMOUNT_INPUT txt_bedrag.txt = 12,30
var AMOUNT_INPUT txt_bedrag.txt = 1230 /*this is the expected result*/
I have tried:
I tried to remove the , char but that did not work.
How can i fix this so the users dont need to manualy remove the , character.

If you just want all the commas out you can use String.Replace() on it:
var cleanString = textbox.Text.Replace(",","")

Related

Find each instance and replace with unique value in string

I have some text in a string as below:
{121:SOMETHING1}}{4:
.
.
.
{121:SOMETHING2}}{4:
.
.
.
{121:SOMETHING3}}{4:
I want to sequentially find and replace the value between 121: and the first }. I can successfully do this using the following code in C#
var rx = #"121:(?<value>[\s\S]+?)}";
string temp = tag121;
string stringToChange = //as above
for (var m = Regex.Match(stringToChange , rx); m.Success; m = m.NextMatch())
{
temp = generateUniqueValue()
stringToChange= Regex.Replace(stringToChange, m.Value, temp);
}
However, if instead of having different values between 121: and } I have exactly the same value for all the tags e.g instead of SOMETHING1, SOMETHING 2 etc, I just have SOMETHING for all the lines, then this code does not work. It ends up setting just one value for all the lines instead of unique values for each.
When all the lines in the string are all {121:SOMETHING}}{4:, the first cycle of the loop already replaced every SOMETHING in the string to first call result of generateUniqueValue(). You can see that happening by printing out stringToChange in the end of every for-loop cycle. (A good way to debug when working around regex, too)
You need to consider a new approach or look at what you trying to achieve again:
Is it acceptable that there are at least 2 lines with same value in the input?
Should lines with same values be replaced into different UniqueValue?
If answers to both questions are yes, one approach I suggest is replace line by line. Not optimal I guess though...
Split the string by lines. String.Split("\\n") Maybe?
Foreach through that split string array.
Find part to replace by regex {121:([^}]+)} - Group 1 of the match is the string you need to replace.
After foreach loop, concat the array into one single string again.
Reference:
Match.Groups

c# - Splitting a string with Email Id in it is truncating the last 3 characters of the Email

I have a readonly textbox field which a value in it like 'Dan, Sealey - Dan.s42#KPS.com' , I want to retrieve the email id from the above string.
I tried using the below code
string email = Convert.ToString(this.txtUser).split('-')[1].Trim();
I even tried in the below way as well but no use, still the result is same.
string str = Convert.ToString(this.txtUser);
string trimmedString = str.Substring(str.IndexOf("-") + 1);
The issue i am having with the above line of code is it is truncating .com and giving the value as "Dan.s42#KPS..."
Not able to figure out why this is truncating ".com"
Any help on this ?
Looks like the issue is with this.txtUser text box. Debug what value is it returning. Check the max length or any limitation on length property. See if there's any masked control trimming the text based on length limitation.
issue resolved, instead of this.txtUser i had to use this.txtUser.text

looking for a replace() wildcard for numbers

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.

Removing whitespace in MSSQL and trailing spaces

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, ' ', '')

Problem with string format

I would like to assign string "{"MY_URL", "MY"}" to string variable. So in my code behind (C#)
I wrote like string str ="{\"MY_URL\", \"MY\"}". When i will assign this to textbox it will print escape character ("\") as well. That i don't want so what i have to do to achieve this??
When i will assign this to textbox it will print escape character ("\") as well.
It shouldn't, and it doesn't on my side, at least on a vanilla VS 2008 ASP.Net application. Perhaps you have explained this question wrong?
I have just tested this on my end, and it is working as expected.
string str = "{\"PCT51_URL\", \"MY\"}";
TextBox1.Text = str;// this is working fine to me.
But if it is still not working for you, you can replace those. e.g.
TextBox1.Text = str.Replace("\\","");
Note: I am using VS2010

Categories