Solved

substring()

Posted on 2006-11-15
20
428 Views
Last Modified: 2012-06-27
i have a string =
"asDfsl00_PlaceHolder1_default_ctl01_ctl43_radiobutton1&info_id=5634&Age=17"

i want the my value to be stored in different variables

string info_id = 5634 (node: can be one number or 5 digits or more)
string age = 17 (note: can be one number of more numbers, i can not control on that)

how do i extract from above string?

0
Comment
Question by:Islam1424
  • 8
  • 6
  • 6
20 Comments
 
LVL 28

Expert Comment

by:strickdd
ID: 17949810
I'm assuming this is the value or some other property of a place holder so why not try:

((ControlType)PlaceHolder1.FindControl("ControlName")).Property (usually .Text or .SelectedValue)
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17949854
use String.Split on "="
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17949864
str="asDfsl00_PlaceHolder1_default_ctl01_ctl43_radiobutton1&info_id=5634&Age=17"

Dim arrValues() as string
Dim id, age as string

arrValues=str.Split("=")
id=arrValues(arrValues.Length-2)
age=arrValues(arrValues.Length-1)
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 35

Expert Comment

by:YZlat
ID: 17949896
actually you'll need


Dim arrValues() As String
        Dim str, infor_id, age As String

        str = "asDfsl00_PlaceHolder1_default_ctl01_ctl43_radiobutton1&info_id=5634&Age=17"

       
        arrValues = Str.Split("=")
        info_id= Replace(arrValues(arrValues.Length - 2), "&Age", "")
        age = arrValues(arrValues.Length - 1)
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17949926
using C#:

string str = "asDfsl00_PlaceHolder1_default_ctl01_ctl43_radiobutton1&info_id=5634&Age=17";
string[] arrValues = Str.Split("=");
string info_id = Replace(arrValues(arrValues.Length - 2), "&Age", "");
string age = arrValues(arrValues.Length - 1);
0
 
LVL 28

Expert Comment

by:strickdd
ID: 17949935
If the case is that it isn't a control inside a control you can do:

string someString = "asDfsl00_PlaceHolder1_default_ctl01_ctl43_radiobutton1&info_id=5634&Age=17";

someString = someString.Substring(someString.IndexOf("info_id=") + 8);

string info_id = someString.Substring(0, someString.IndexOf("&"));
string Age = someString.Substring(someString.IndexOf("&") + 1);
0
 
LVL 28

Expert Comment

by:strickdd
ID: 17950000
sorry:

string Age = someString.Substring(someString.IndexOf("&") + 1);

should be:

string Age = someString.Substring(someString.IndexOf("Age=") + 4);



but once again if you are accessing controls in this placeholder, it is best to type case a findcontrol on the control ID you want to access ie:

((TextBox)PlaceHolder1.FindControl("Age")).Text
0
 

Author Comment

by:Islam1424
ID: 17950326
Yzlat:

Error      2      Argument '1': cannot convert from 'string' to 'char[]'      

Error      3      The name 'Replace' does not exist in the current context      

Error      4      'arrValues' is a 'variable' but is used like a 'method'      

Error      5      'arrValues' is a 'variable' but is used like a 'method'       
0
 

Author Comment

by:Islam1424
ID: 17950360
strickdd :

>but once again if you are accessing controls in this placeholder, it is best to type case a findcontrol on the >control ID you want to access ie:

thanks but i'm not finding any control

but in fact i'm adding into the string and from there i'm extracting.

0
 
LVL 28

Expert Comment

by:strickdd
ID: 17950399
Where is the string being stored? is it the text or the id of the Panel?
0
 

Author Comment

by:Islam1424
ID: 17950416
its storing in the string
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17950424
try replacing

string[] arrValues = Str.Split("=");

with

string[] arrValues = str.Split(new char[] {'='});
0
 

Author Comment

by:Islam1424
ID: 17950428
is it the safe side of doing?

0
 

Author Comment

by:Islam1424
ID: 17950467
string str = "asDfsl00_PlaceHolder1_default_ctl01_ctl43_radiobutton1&info_id=5634&Age=17";
                    string[] arrValues = str.Split(new char[] { '=' });
                    string info_id = Replace(arrValues(arrValues.Length - 2), "&Age", "");
                    string age = arrValues(arrValues.Length - 1);

Error      1      The name 'Replace' does not exist in the current context
Error      2      'arrValues' is a 'variable' but is used like a 'method'
Error      3      'arrValues' is a 'variable' but is used like a 'method'
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17950499
strange... can you post all your code?
0
 
LVL 28

Expert Comment

by:strickdd
ID: 17950505
have you tried this yet:

string someString = "asDfsl00_PlaceHolder1_default_ctl01_ctl43_radiobutton1&info_id=5634&Age=17";

someString = someString.Substring(someString.IndexOf("info_id=") + 8);

string info_id = someString.Substring(0, someString.IndexOf("&"));
string Age = someString.Substring(someString.IndexOf("Age=") + 4);
0
 
LVL 35

Assisted Solution

by:YZlat
YZlat earned 75 total points
ID: 17950556
try this:

string str = "asDfsl00_PlaceHolder1_default_ctl01_ctl43_radiobutton1&info_id=5634&Age=17";
                        string[] arrValues = str.Split(new char[] { '=' });
                        string info_id = arrValues[arrValues.Length - 2];
                              info_id=info_id.Replace("&Age", "");
                        string age = arrValues[arrValues.Length - 1];
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17950560
I used Replace like in VB.NET before. Here is C# version
0
 

Author Comment

by:Islam1424
ID: 17950686
strickdd, yes your approach word

but just want to get some opinion

is that the safe way of extracting?

0
 
LVL 28

Accepted Solution

by:
strickdd earned 50 total points
ID: 17950720
It is not a real dynamic way of doing things, and there is really no good way, unless you add hidden fields to the panel and access those with a foreach loop.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question