I need to extract the value from the current url.
For example if my url is
http://stackoverflow.com/questions/ask
I need the result stored in an array splitted with respect to "/".
my array should contain [questions,ask]
You could use the Uri class:
var uri = new Uri("http://stackoverflow.com/questions/ask");
Now look at the uri.Segments property which represents a string array containing what you are looking for.
Use String.Split function.
http://msdn.microsoft.com/en-us/library/ms131448.aspx
Related
I'm trying to make a list in string to get the usernames like this:
string[] whitelisted = { "example", "exampøle222", "example245"};
if (sentRequest.Sender.Username.ToLower().Contains(whitelisted))
You are checking if the username contains the list. You should be checking that the list contains the username.
whitelisted.Contains(sentRequest.Sender.Username.ToLower())
Your comparison is backwards. You need to see whether the array contains the string .
You probably want to check that the sentRequest.Sender.Username.ToLower() is present in the whitelisted list, right?
Then make it inverted if (whitelisted.Contains(sentRequest.Sender.Username.ToLower()))
The error you get says that the method you are calling i.e. string.Contains() can accept a string but cannot accept an array string[].
It can be done quite simply using the LINQ aggregate function.
string[] strings = {"example1","example2"};
string result = strings.Aggrigate((l,r)=>l+r);
You could also use the String.Join method
string[] strings = {"example1","example2"};
string result = String.Join("",strings);
However when I look at the rest of your code I would recommend solving the problem something like this.
if(whitelisted.IndexOf(sentRequest.Sender.Username.ToLower()) != -1)
That if statement will only be executed if the Username is in the white list.
I've got two different json files and would like to merge them and read json strings from one file.
{"ipAddress":"1.1.1.1","port":80, "protocol":"http"}
{"ipAddress":"1.1.1.1", "domainName":"domain.com"}
I tried something, but it still doesn't work properly. I tried array and also the following structure:
{"jsonString1": {"ipAddress":"1.1.1.1","port":80, "protocol":"http"},
"jsonString2": {"ipAddress":"1.1.1.1", "domainName":"domain.com"}}
Not sure if the structure is correct. I just need to get "jsonString1", "jsonString2" separately so I don't need to use more json files.
Your 1st fragment is non standard (effectively, not JSON).
Your 2nd IS standard, but is an object, not an array.
If you want an array, use an array:
[{"ipAddress":"1.1.1.1","port":80, "protocol":"http"},
{"ipAddress":"1.1.1.1", "domainName":"domain.com"}]
Alternatively, if you want to use your 2nd version (which is an object), you can access the 2 "sub-objects" by keys: myObj.jsonString1, myObj.jsonString2. BTW, A better name would be "Obj1" & "Obj2" since these are not strings, they're actual objects.
Use JSON Array to keep those files merged.For example
Conside,You have an JSON array for URL then and you want to print it in a paragraph then
<p id="url"></p>
declare a variable like this
var URL=[{"ipAddress":"1.1.1.1","port":80, "protocol":"http"},
{"ipAddress":"1.1.1.1", "domainName":"domain.com"}]
you can access these array by using
document.getElementById("url").innerHTML =
URL[0].ipAddress+ " " + URL[0].port+ " " +URL[0].protocol;
You can get the array values by using the index values
Thank you. I have finally used this syntax:
json:
{"jsonString1": {"ipAddress":"1.1.1.1","port":80, "protocol":"http"},
"jsonString2": {"ipAddress":"1.1.1.1", "domainName":"domain.com"}}
read the string using this command:
var jsonObject = JObject.Parse(jsonData);
string value = jsonObject["jsonString1"].ToString();
So I'm working with umbraco and using the tag datatype. I'm trying to take all tags added to a given node and putting them into an array of strings but when I grab the value it always seems to come out like this:
"[\"Tag1\",\"Tag2\"]"
How can I convert this string of an array back into a regular array? All I have gotten so far was a string of individual characters
The array format you have provided as an example looks like part of the JSON object.
You could use JSON.net library to parse array token of the JSON object.
var array = JArray.Parse(tagString).Values<string>();
A complete example is available here.
You could try:
string[] newArray = item.Replace("\"", "").Replace("[", "").Replace("]", "").Split(',');
This would output as Tag1, Tag2 etc...
Hope this helps.
Use this
var tagString = "[\"Tag1\",\"Tag2\",\"Tag3\",\"Tag4\"]";
var tags = tagString.Trim().TrimStart('[').TrimEnd(']').Split(',');
I am using a simple statement
NameValueCollection nvCollection = HttpUtility.ParseQueryString(queryString);
I am trying to grab the values passed in the query string. A simple example of something passed would be
form_id=webform_client_form_4&referrer=http://myURL/webform/request-information?agent=agent&keyword=keyword&full_name=Jon Harding&company=RTS Financial - TEST&phone=913-555-5555
The issue I am having is that the referrer is actually the full string after it. I do need the other values. In this case becauase the ParseQueryString splits the string at the & character the first value is always ignored in the NameValueCollection
Currently when I try to get the value of the agent it is a blank value, understandably.
What would be the best method to make sure I grab all variables? I could split the string at the question mark and then prepend an ampersand to the string before I do the ParseQueryString. Is there a more elegant want to do this?
This will work with least effort:
NameValueCollection nvCollection = HttpUtility.ParseQueryString(queryString.Replace("?","&"));
Ok, I have a string of the form
string temp = "http://www.example.com?file=666111&submitter=Betty&origin=Office&telNo=05555";
what I need to do is extract the value of the file variable in order to use it. If this was the referrer url I could've done Request.QueryString and got it but the problem is that I have it as a string variable.
I could try to do substring and get the value but I was hoping there was a cleaner way to do this?
Perhaps you can use the HttpUtility.ParseQueryString method. It returns a NameValueCollection with all parameters.