简体   繁体   中英

using strings in c#.net

Hi How do I retrieve the number from the following string,

{"number":100,"data":[test]}

The number could be of any length.

I used the following code. but it gives and error message

strValue.Substring((strValue.IndexOf(":")+1), (strValue.IndexOf("data")));

the output comes like

100,"data":[

Thanks,

It looks like your input string is JSON. Is it? If so, you should use a proper JSON parser library like JSON.NET

Your attempt is close. There are two (possibly three issues) I found.

  1. Your string has a comma after the number you're looking for. Since your code is searching for the index of "data", your end index will end up one character too far.
  2. The second paramter of String.Substring(int, int) is actually a length, not an end index.
  3. Strings are immutable in C# . Because of this, none of the member functions on a string actually modify its value. Instead, they return a new value. I don't know if your code example is complete, so you may be assigning the return value of SubString to something, but if you're not, the end result is that strValue remains unchanged.

Overall, the result of your current call to string.Substring is returning 100,"data":[tes . (and as far as I can see, it's not storing the result).

Try the following code:

string justTheNumber = null;
// Make sure we get the correct ':'
int startIndex = strValue.IndexOf("\"number\":") + 9;
// Search for the ',' that comes after "number":
int endIndex = strValue.IndexOf(',', startIndex);
int length = endIndex - startIndex;
// Note, we could potentially get an ArguementOutOfRangeException here.
// You'll want to handle cases where startPosition < 0 or length < 0.
string justTheNumber  = strValue.Substring(startIndex, length);

Note: This solution does not handle if "number": is the last entry in the list inside your string, but it should handle every other placement in it.

If your strings get more complex, you could try using Regular Expressions to perform your searches.

As noted by Jon, your input string seems to be a JSON string which needs to be deserialized. You can write your own deserializer, or use an existing library, such as Json.NET . Here is an example:

string json = @"[
  {
    ""Name"": ""Product 1"",
    ""ExpiryDate"": ""\/Date(978048000000)\/"",
    ""Price"": 99.95,
    ""Sizes"": null
  },
  {
    ""Name"": ""Product 2"",
    ""ExpiryDate"": ""\/Date(1248998400000)\/"",
    ""Price"": 12.50,
    ""Sizes"": null
  }
]";

List<Product> products = JsonConvert.DeserializeObject<List<Product>>(json);

Parsing JSON spring in that way is very bad practice as everything is hardcoded. Have you though of using 3rd party library for parsing JSON strings, like Newtonsoft JSON .

我想您需要使用IndexOf(“,”)而不是IndexOf(“ data”)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM