LinkButton section = (LinkButton)gridcheck.Rows[i].FindControl("lbSection");
if (section == sender)
Response.Redirect(section.Text + ".aspx");
Lets say I have an "AirPlane.aspx" page and the database table I am calling is named "Air Plane". I tried to use this to trim the space between the "r" and "P" but it does not work
Convert.ToString(section).Replace(" ", "");
It appears you're trying to convert the entire object to a string. Instead, you should be able to use the text field and perform a replace directly against it.
Response.Redirect(section.Text.Replace(" ", string.Empty) + ".aspx");
Related
So I have a Label being initialized by a WebService. I want to see if that label contains any commas. The problem is, even if the label has commas, Contains() returns false and if I do a Split(), the array is only 1 element long, containing the entire string.
// text is "255,255,0,0"
string wat = myLabel.Text;
string[] wats = wat.Split(',');
// This IF never happens, for some reason
if (wat.Contains(","))
{
anotherLabel.Text = wats[0] + " VS " + wats[1];
}
Why don't Split() and Contains() work? Can it be some kind of diferent encode in the string that comes from the label? If I do wat = wat + ",", then Contains()returns True.
Unicode symbols are often weird. Unicode has a lot of commas, e.g.
string wat = "255,255,0,0"; // Full range commas
bool hasComma = wat.Contains(','); // false
If wat.Contains(',') returns false then delimiters are not commas ,. You can check it with string decoded:
string wat = myLabel.Text;
// Let's have a close look at wat: which characters (codes included) does it contain
MessageBox.Show(
$"wat: [{wat}] encoded as {string.Join(" ", wat.Select(c => ((int)c).ToString("x4")))}");
You should get
wat: [255,255,0,0] encoded as 0032 0035 0035 002c 0032 0035 0035 002c
0030 002c 0030
if not check what character code(s) do you have instead of expected 002c.
The following line is always going to evaluate to false:
if (wats.Contains(","))
string.Split(',') will only return the values in between commas as you are specifying a comma as your delimiter. None of the items in the array will ever contain a comma.
If you want to check whether your label text contains commas simply do:
if (lblteste.Text.Contains(','))
I'm completely lost with what is happening here.
string send = "!points add " + entries[winner] + " " + prize.ToString();
What I want to send is "!points add winnername prizeamount" but what I get is "!points add winnername\nprizeamount". I put \n because it writes a new line but trying to replace "\n", "\r" and "\t" with " " does nothing.
enter image description here
all I need is the message to be exactly"!points space add space winnername space prizeamount space"
If it's important the entries in my code is a List of strings
The entries strings already contain the new line character(s).
I suggest you replace with Environment.NewLine:
Replace Line Breaks in a String C#
The string object, 'entries[winner]' is having line-feeds (LF) or carriage-returns (CR). You try this to remove all LFs and CRs,
string send = "!points add "
+ entries[winner].Replace("\r", string.Empty).Replace("\n", string.Empty)
+ " " + prize.ToString();
Alternatively, you can use Trim() to remove leading\ trailing LFs\ CRs.
string send = "!points add "
+ entries[winner].Trim()
+ " " + prize.ToString();
No repro. The following code doesn't assert.
var entries=new List<string>{"Aaa", "Bbb", "Ccc"};
int prize=90;
int winner=1;
var send=String.Format("!points add {0} {1}",entries[winner],prize);
var send2="!points add " + entries[winner] + " " + prize.ToString();
Trace.Assert("!points add Aaa 90"==send);
Trace.Assert(send2==send);
If the result contains newlines, it's because the entries values contain newlines.
The best solution would be to clean the input data before storing it in the list, eg with String.TrimEnd or String.Trim, When loading data from a file for example, you can't be sure it doesn't contain trailing spaces.
To read clean data from a file you could use :
var entries=File.ReadLines()
.Select(line=>line.Trim())
.ToList();
If you add the entries one by one from user input :
entries.Add(newEntry.Trim());
If you can't change how the data is read (why?) you can trim when whenever you use an entry value:
var send=String.Format("!points add {0} {1}",entries[winner].Trim(),prize);
Loading clean data is a lot easier
I want to form a string as <repeat><daily dayFrequency="10" /></repeat>
Wherein the value in "" comes from a textboxe.g in above string 10. I formed the string in C# as
#"<repeat><daily dayFrequency=""+ txt_daily.Text + "" /></repeat>" but i get the output as
<repeat><daily dayFrequency="+ txt_daily.Text+ " /></repeat>. How to form a string which includes the input from a textbox and also double quotes to be included in that string.
To insert the value of one string inside another you could consider string.Format:
string.Format("foo {0} bar", txt_daily.Text)
This is more readable than string concatenation.
However I would strongly advise against building the XML string yourself. With your code if the user enters text containing a < symbol it will result in invalid XML.
Create the XML using an XML library.
Related
How can I build XML in C#?
Escape it with \ Back slash. putting # in front wont do it for you
string str = "<repeat><daily dayFrequency=\"\"+ txt_daily.Text + \"\" /></repeat>";
Console.Write(str);
Output would be:
<repeat><daily dayFrequency=""+ txt_daily.Text + "" /></repeat>
You could do it like this:
var str = String.Format(#"<repeat><daily dayFrequency="{0}" /></repeat>",
txt_daily.Text);
But it would be best to have an object that mapped to this format, and serialize it to xml
string test = #"<repeat><daily dayFrequency=" + "\"" + txt_daily.Text + "\"" + "/></repeat>";
I have a field called Description in the front end form, it is a textarea where user can type/copy past the text which include line breaks aswell.
From asp.net all this data goes to sharepoint.
Now I have a search page which returns all these values from sharepoint using webserivices in the format of xml.
The problem is that all of the line breaks in the value in replaced with
I am trying to display the description field values to the label, but its not working I tried below things :
lblDesc.Text = xmlValuesPath.Attribute("ows_Description").Value.Replace("
", "\n");
lblDesc.Text = xmlValuesPath.Attribute("ows_Description").Value.Replace("
", "</p><p>");
The formatting works fine in a textbox, but nothing seems to be working, kindly help.
Did you clear out all HTML tags from it?
public static string ClearHTMLTagsFromString(string htmlString)
{
string regEx = #"\<[^\<\>]*\>";
string tagless = Regex.Replace(htmlString, regEx, string.Empty);
// remove rogue leftovers
tagless = tagless.Replace("<", string.Empty).Replace(">", string.Empty);
tagless = tagless.Replace("Body:", string.Empty);
return tagless;
}
Try to replace "
" with "<br/>" it should work in ASP.NET Label.
By default asp.net coverts it to \n .which at the run time wont be parsed by the html code to you just need to replace \n with ""
xmlValuesPath.Attribute("ows_Description").Value.Replace("\n", "</p><p>")
I have a textbox1 two and three. In textbox1 number zero, in two number one and in three number two and use this code
textBox4.Text = "" +
(int.Parse(textBox1.Text) +
(int.Parse(textBox2.Text +
(int.Parse(textBox3.Text)))));
but result is 012..you can have the entire amount, 3?
int result =
int.Parse(textBox1.Text) +
int.Parse(textBox2.Text) +
int.Parse(textBox3.Text);
textBox4.Text = result.ToString();
Try this
What's happening here is that the sum is being evaluated from left to right and this is causing a different type of addition to be performed than what you would expect. In C#, you can add two strings. If you add "foo" to "bar" then this will give you the string "foobar". If you add a string and a number together, then it will convert the number to a string and add the two strings together. So "foo"+13 yields "foo13".
So what's happening in your example is quite complicated. Starting from the inside, you have: int.Parse(textBox3.Text). This takes textBox3.Text which is "2" and converts it to the number 2. Next you do textBox2.Text + (int.Parse(textBox3.Text) which gets the string "1" and then adds the number 2 to it. This causes the number 2 to be converted to the string "2" and then adds "1"+"2", giving the string "12" as the answer since strings are added by joining them. Next you do int.Parse(textBox2.Text + (int.Parse(textBox3.Text)) which converts the string "12" to the number 12. You also do int.Parse(textBox1.Text) which gives the number 0. So at this point you're adding "" + 0 + 12. It does this from left to right, first adding "" to 0. This causes 0 to be converted to "0" and "" + "0" gives "0". Then we are adding "0" + 12. When we do this, 12 gets converted to "12" and "0"+"12" gives "012".
Without making big changes, you could get the correct result just by changing your parentheses. If the numbers were all added together before any of them are added to strings, then you'll get the correct result. We can accomplish this with parentheses.
textBox4 = "" + (int.Parse(textBox1.Text) + int.Parse(textBox2.Text) + int.Parse(textBox3.Text));
In short, it's really important to pay attention to what's happening in what order and what the types are because adding two strings is completely different from adding two numbers.
Put the " mark at the end that way it does the regular math first, then the string conversion.
You can you smth like this:
int sum=int.Parse(textBox1.Text) + int.Parse(textBox2.Text) + int.Parse(textBox3.Text);
textBox4.Text = String.Format("{0}",sum);
You have 2 problems here. The first one is the "" at the beginning. When you do the first +, textBox1.Text is first parsed, then converted to string again by the string concatenating operator. I'd prefer something like this:
textBox4.Text = (int.Parse(textBox1.Text) + int.Parse(textBox2.Text) + int.Parse(textBox3.Text)).ToString();
The second problem (the real one) is the fact that you miss a closing parenthesis after textBox2.Text. In this way you are first concatenating textBox1.Text ("1") and int.Parse(textBox2.Text).ToString() ("2"), and only at this point you parse the result. If the parenthesis were not missing your code would give "3" and not "012"