0

How can I extract the value 2 from a string pid = {id=2}.

My code looks like

string pidnew= pid.Substring(3,5)

It's showing me error, have I made anything wrong?

1
  • second argument (length) should be 1 Commented Oct 25, 2013 at 17:20

3 Answers 3

2

To get individual values out of a string like string pid = "{id=2, genderid=5, stateid=4}", you could use this method:

public string GetValue(string idPart, string test)
{
    var escIdPart = Regex.Escape(idPart);
    var pattern = string.Format("(?<=[\\{{\\s,]{0}\\s*=\\s*)\\d+", escIdPart);
    var result = default(string);
    var match = Regex.Match(test, pattern, RegexOptions.IgnoreCase);
    if (match.Success)
    {
        result = match.Value;
    }
    return result;
}

...

var pid = "{id=2, genderid=5, stateid=4}";

var id = GetValue("id", pid); // returns "2"
var genderid = GetValue("genderid", pid); // returns "5"
var stateid = GetValue("stateid", pid); // returns "4"
Sign up to request clarification or add additional context in comments.

2 Comments

Instead of hardcoding the values as 4 , is there any way that i could get 4 using anyohter function so that I can use globally
I've updated my answer to give the ability to pick any value out of the string you specified in your comment to the accepted answer, without hardcoding anything.
1

if there's only one number you can just Regex it

string pidnew = Regex.Match(pid, @"\d+").Value;

and you won't need to worry about the location

if you want to match on stateid:

string pidnew = Regex.Match(pid, @"(?<=stateid=)\d+",RegexOptions.IgnoreCase).Value;

2 Comments

if more numbers are there in a string like say for eample string pid={id=2, genderid=5,stateid=4}. then how to get Stateid alone from th string
Didnot work for me when multiple integers where there in a string example string pid={ selectedid = 9, genderid=1} how to get genderid I did like but its giving me null value string pidnew = Regex.Match(pid, @"(?<=genderid=)\d+",RegexOptions.IgnoreCase).Value;
0

The second parameter for Substring tells the number of characters to take. Thus if you have the string "{id=2}" and perform string pidnew= pid.Substring(3,5), what you're saying is: "Start at the character index 3 in the string (fourth character), and take five characters". This means the string would need to be at least 8 characters long. Since you only want to take one character, change the second parameter to a 1. You should also start at index 4 rather than 3 if the brackets are part of your string.

What you actually need is: string pidnew= pid.Substring(4,1);

MSDN Docs:

Parameters

startIndex Type: System.Int32 The zero-based starting character position of a substring in this instance.

length Type: System.Int32 The number of characters in the substring.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.