Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, I'm trying to parse data from a website and I'm receiving a string like

                                 +9%

from these I just need (9) how can I retrieve this number to a variable?
I use Trim, Remove and other functios that they are working for strings but here they are not working and now why.

Here is the code that I'm using:

WebClient wc = new WebClient();
string htmlString = wc.DownloadString("http://sentra.com.gr/index.php?option=com_content&view=article&id=3291:rekor-times-oloklirothike-dimoprasia-kopenhagen-fur-dekembrios-2012&catid=75:auction&Itemid=141");

string price = "";
Match mprice = Regex.Match(htmlString, @"

Chinchilla(.*?)

", RegexOptions.Singleline);
if (mprice.Success)
{
price = mCountry.Groups[1].Value;
TextBox1.Text = price;
airplaneCountry.Remove(0,81);
Console.WriteLine(airplaneCountry);
// res.Text = price;
}
price = Regex.Replace(price, "(.*?)>", "").Trim();
TextBox2.Text = price;


thnx in advance
Jason

Posted
Updated 19-Nov-13 11:34am
v2
Comments
Sergey Alexandrovich Kryukov 19-Nov-13 17:19pm    
You can use Trim, Remove, Substring or Split, only correctly. Regex, too. Just do it accurately, read documentation, use the debugger...
No need to use Regex.Replace, just using the Match would be enough.
—SA
JasonTsoum77 19-Nov-13 17:39pm    
I have read the documentation tried it but they didn't work in string price, I don't know where is the wrong because I tried with simple strings like
string s = "0123456";
s.remove(0,3);
e.t.c.
and they work properly, I thing that here because I retreiving html is something going wrong and these functions that you refer in your reply (trim,regax) they didn't work.

Anyway thnx for your response.
Sergey Alexandrovich Kryukov 19-Nov-13 18:34pm    
When it would work depends on what is the expected format. Say, it could be Regex like ".*([0-9]*).*", group in round brackets goes to output. If you need to allow decimal point, that's a bit mode complex, and also culture-dependent...
—SA
The Manoj Kumar 19-Nov-13 18:03pm    
You need to assign the value to a new variable. So, it should be s = s.Remove(0,3). Most string operations are immutable.
Sergey Alexandrovich Kryukov 19-Nov-13 18:35pm    
Not "most". String is an immutable type, period.
—SA

1 solution

Depending on what type of strings you are processing, and what types of information you are "scraping:" you might use different strategies.

If you want only numbers, write a RegEx that removes everything but numbers. Something like:
YourString = Regex.Replace(YourString, "[^0-9.]", "");
In the case of your example : "+9%" : only the percent sign needs to be removed to get a valid integer:
C#
private int i;
private bool IsNumber;

private string n = "+9%";

private void SomeMethod(string testString)
{
    n = n.TrimEnd('%');

    IsNumber = Int32.TryParse(n, out i);

    if (IsNumber)
    {
        // code to handle the valid number
        // in the variable i
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900