Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing substrings into a variable.

Status
Not open for further replies.

dandot

Programmer
Jul 19, 2005
53
CA
Hi All,

I am reading from a file, where I need to move certain pieces of information into variables which I will move later to db.

Here is an example of the file...
-------->> General Statistics <<--------

, Report Range: 09/02/2008 00:00:00 - 09/02/2008 23:59:59,
, Generated On Wednesday September 03 2008 - 05:25:18,
, ,
Hits, Entire Site (Successful), 11037849
, Average Per Day, 11037849
, Home Page, 7653
Page Views, Page Views (Impressions), 420744
, Average Per Day, 420744
, Document Views, 104133
Visits, Visits, 37502
, Average Per Day, 37502
, Average Visit Length, 00:10:44
, Median Visit Length, 00:04:21
, International Visits, 0%
, Visits of Unknown Origin, 100%
, Visits from United States, 0%
Visitors, Unique Visitors, 26750
, Visitors Who Visited Once, 22186
, Visitors Who Visited More Than Once, 4564


I am using StreamReader to read from the file and getting stuff line by line. My question is what string functions would I use to extract the items in bold?
 
I would use Regex. As for the exact regex sequence. I would find a Regex forum and ask there.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I agree with jmeckley that regex would give you the best performance. However, assuming you want to get the numbers for Entire Site hits and Document Views regex may be overkill (since your search pattern would be words). The following examples illustrates how you could grab these values using regex or by using the built in string functions.

Code:
string input = "Hits, Entire Site (Successful), 11037849";
string value;
//Get Value using Regex
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"Hits, Entire Site \(Successful\),");
if(regex.IsMatch(input))
    value = regex.Replace(input, "");

//Get Value using string functions
string[] dimensions = input.Split(',');
if (dimensions[1] == "Entire Site (Successful)")
    value = dimensions[2];

I'm sure you could come up with a more elaborate search pattern, but without understanding your exact parsing criteria it difficult to provide a more useful example.

You can apply the same logic to get the value for Document Views.

Hope this helps.

 
Thank you dave

I ended up goign with a solution like this

Code:
                         temp= temp.Replace(" ", "");
                         temp = temp.Replace("%", "");
                        string[] dimensions = temp.Split(',');
                        if (dimensions.Length > 1)
                        {
                            if (dimensions[1] == "EntireSite(Successful)")
                                //Console.WriteLine(dimensions[2]);
                                sql = sql + dimensions[2] + ", " + dimensions[2] + ", ";                            
                            if (dimensions[1] == "HomePage")
                                sql = sql + dimensions[2] + ", ";   
                            if (dimensions[1] == "PageViews(Impressions)")
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top