Solved

substring()

Posted on 2006-11-15
20
426 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
Comment Utility
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
Comment Utility
use String.Split on "="
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
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
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Where is the string being stored? is it the text or the id of the Panel?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:Islam1424
Comment Utility
its storing in the string
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
try replacing

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

with

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

Author Comment

by:Islam1424
Comment Utility
is it the safe side of doing?

0
 

Author Comment

by:Islam1424
Comment Utility
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
Comment Utility
strange... can you post all your code?
0
 
LVL 28

Expert Comment

by:strickdd
Comment Utility
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
Comment Utility
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
Comment Utility
I used Replace like in VB.NET before. Here is C# version
0
 

Author Comment

by:Islam1424
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now