Solved

substring()

Posted on 2006-11-15
20
429 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

749 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