Array assignment to string [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have php code that puts some values into an Array as follows:
$hunter=addslashes($MessageArray[1]);
$time=addslashes($MessageArray[2]);
I wrote the same code in C# and wanted to know if it was correct.
string Hunter = Messagearray[1].tostring();
string time = Messagearray[2].tostring();

As James mentioned, use Pascal casing:
string hunter = messageArray[1].ToString();
string time = messageArray[2].ToString();
Also, C# arrays are indexed starting at 0. You can change the starting index of arrays in PHP, but you can't in C#. Perhaps you do wish to take the 2nd and 3rd items, but keep it in mind. You might want:
string hunter = messageArray[0].ToString();
string time = messageArray[1].ToString();
As far as addslashes() goes, it will depend on your usage of hunter and time. If you're using them in a SQL statement, there are other ways of accomplishing the functionality of PHP's addslashes().

Snipped from Here
public static string AddSlashes(string input)
{
return System.Text.RegularExpressions.Regex.Replace(input, #"(\\)([\000\010\011\012\015\032\042\047\134\140])", "$2");
}
Usage:
//
var Messagearray = new object[] { "item 0", 1 };
var hunter = AddSlashes(Messagearray[0].ToString());
var time = AddSlashes(Messagearray[1].ToString());

Related

Getting value by name from OrderedDictionary [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am looking for something like PHP's associative arrays that supports nesting too. For instance, I am creating a Dictionary Object like following:
System.Collections.Specialized.OrderedDictionary userRoles = new System.Collections.Specialized.OrderedDictionary();
userRoles["UserId"] = "120202";
userRoles["UserName"] = "Jhon Doe";
// 2D array Like
userRoles["UserRoles"][0] = "CAN_EDIT_LIST";
userRoles["UserRoles"][1] = "CAN_EDIT_PAGE";
Then I would like to access them by KeyNames instead of index values. Is it possible?
OrderedDictionary uses objects for both keys and values.
To achieve nesting, just set the value to be another dictionary:
userRoles["UserRoles"] = new Dictionary();
Then you can use:
((Dictionary())userRoles["UserRoles"])["MyKey"] = "My Value";

How to write the regex expression? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
The input string: A+SUM(A)C+AB-C+SUM(A)+1
I want to replace A with 0, the result like this:0+SUM(A)C+AB-C+SUM(A)+1
or replace SUM(A) with 0, the result like this:A+SUM(A)C+AB-C+0+1
Thanks
Without Regex (because Regex is overkill for this):
var s = "A+SUM(A)+B-C";
var replaceBeginningA = s.Replace("A+", "0+");
var replaceSumA = s.Replace("SUM(A)", "0");
Console.WriteLine(replaceBeginningA); // 0+SUM(A)+B-C
Console.WriteLine(replaceSumA); // A+0+B-C
As pointed out in the comments though, you need to provide more detail if this input is expected to have a different format.
maybe:
replace ^A with 0
replace SUM\(A\) with 0
Try thus:
string replaced = Regex.Replace(input, #"\b(\w+)\+SUM\(\1\)", "0+0");
This will match anything of the form
foo+SUM(foo)
and replace it with 0+0

C# asp.net parameters cast to a page [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
if you please help me out i am trying to pass 3 different parameters in a page but i am new in asp.net C# and i don't know the correct syntax if you please help me out
;
For one parameter like this it works:
Response.Redirect("~/WebPage2.aspx?q=" + ListBox1.SelectedValue);
how can i write it for 3 parameters like this don't seem to work?
Response.Redirect("~/WebPage2.aspx?q=" + ListBox1.SelectedValue+"&cr="+ListBox3.SelectedValue+"&p="+ListBox1.SelectedValue)
thanks in advance
You forgot a + in your string concatenations after the cr parameter. This being said a far safer and better approach which ensures that your parameters are properly encoded is the following:
var parameters = HttpUtility.ParseQueryString(string.Empty);
parameters["q"] = ListBox1.SelectedValue;
parameters["cr"] = ListBox3.SelectedValue;
parameters["p"] = ListBox1.SelectedValue;
var url = string.Format("~/WebPage2.aspx?{0}", parameters.ToString());
Response.Redirect(url);
And of course if you are using ASP.NET MVC (as you've tagged your question with it) you would use:
return RedirectToAction("SomeAction", new {
q = ListBox1.SelectedValue,
cr = ListBox3.SelectedValue,
p = ListBox1.SelectedValue
});
I very sincerely hope that if you are using ASP.NET MVC then ListBox1 and ListBox2 is not what I think it is.
Here's what I typically do (assuming a constant number of parameters for each URL):
string url = "~/WebPage2.aspx?q={q}&cr={cr}&p={p}";
url = url.Replace("{q}", ListBox1.SelectedValue)
.Replace("{cr}", ListBox2.SelectedValue)
.Replace("{q}", ListBox3.SelectedValue);
Response.Redirect(url);
I have not tested it, but this may be relatively inefficient. The reason I do it this way is so I know exactly what the URL pattern looks like and which parameters are used.
It's a trade-off to be sure, and am curious to see other peoples' feedback.

How do I remove a portion of my string? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Continuing from my previous question, I now want to remove the number once I have found it and stored it in a variable.
Just a slight tweak to my response to your first question to instead use Regex.Replace method will git 'er done.
Don't worry I figured it out. Just need to find the length then delete the chars
(qual is the intergers found)
string length = qual.ToString();
int length2 = length.Length;
text.Remove(0, length2);
I think the following code resolves the root problem:
string originalString = "35|http://www.google.com|123";
string[] elements = originalString.Split(new char[] { '|' }, 2);
int theNumber = int.Parse(elements[0]); // 35
string theUrl = elements[1]; // http://www.google.com|123

How can I create a string of random characters (as for a password...)? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I've written some code to generate a sequence of random characters, but it does not:
byte[] sbytes = { 1, 0, 1, 0, 1 };
String sstring;
System.Random r = new System.Random();
r.NextBytes(sbytes);
sstring = Convert.ToBase64String(sbytes);
sstring = Path.GetRandomFileName();
sstring = sstring.Replace("=", "");
sstring = sstring.Replace(".", "");
textBox1.Text = sstring.ToString();
I believe the problem is between NextBytes and ToBase64String. I don't know how to make this work. How do you do the correct conversion to pass it to a textbox for display?
If you want to generate a random password here's a great example. And here's another one which is more naive. Simply forget about the Path.GetRandomFileName function.
if you wrote
sstring += Path.GetRandomFileName();
I'm guessing the result would be more like what you are looking for however it's only pseudo random and you should take a look at the link Darin posted
You're replacing the random data with the filename:
sstring = Convert.ToBase64String(sbytes);
sstring = Path.GetRandomFileName();
The second statement ignores the existing value set by the first statement, so the calls to NextRandom() and ToBase64String are completely irrelevant.
What exactly are you trying to do?
EDIT: This isn't really a very good way of creating a random password. For one thing you should use SecureRandom rather than Random, and you might also want to avoid confusing features such as "0" and "O", and "l" and "1" looking the same. You could just use Path.GetRandomFileName on its own, but it's not really what it's designed for.

Categories