6 Replies - 547 Views - Last Post: 24 May 2010 - 05:25 PM Rate Topic: -----

#1 jammmie999  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 117
  • Joined: 01-April 09

String Manipulation

Posted 24 May 2010 - 08:24 AM

Hi guys

In C# Win Forms how can I extract this kinda value:

Blar blar blar\r\n
Content-Length: 35\r\n
Blar blar blar



I need to extract the value 35 from this string or whatever it might be.

Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: String Manipulation

#2 Shivern  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 27
  • Joined: 20-May 10

Re: String Manipulation

Posted 24 May 2010 - 09:05 AM

I'm not sure i understand what you mean/want.

Can you explain a bit more?

Try give a code example, then i'll take a look.
Was This Post Helpful? 0
  • +
  • -

#3 jammmie999  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 117
  • Joined: 01-April 09

Re: String Manipulation

Posted 24 May 2010 - 09:44 AM

If I have got some text e.g.
string text = "blar blar blar\r\n Content-Length: 35\r\nblar blar blar";



How can I extract the number 35 from the string?

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: String Manipulation

Posted 24 May 2010 - 10:16 AM

Something like
Match m = Regex.Match(@"^Content-Length: ([-\d]+)\r\n", myString);
if (m.Success)
    Console.WriteLine("Content length: {0}", m.Groups[1]);


Not tested.
Was This Post Helpful? 1
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: String Manipulation

Posted 24 May 2010 - 11:13 AM

Another option other than regular expressions...

string text = "blar blar blar\r\n Content-Length: 35\r\nblar blar blar";
string[] array = text.Split(@"\r\n", StringSplitOptions.None);
string secondLine = array[1];
string[] secondArray = secondLine.Split(':');
int number = int.Parse(string[1].Trim());



Not tested.
Was This Post Helpful? 3
  • +
  • -

#6 jammmie999  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 117
  • Joined: 01-April 09

Re: String Manipulation

Posted 24 May 2010 - 11:26 AM

View PostJackOfAllTrades, on 24 May 2010 - 06:16 PM, said:

Something like
Match m = Regex.Match(@"^Content-Length: ([-\d]+)\r\n", myString);
if (m.Success)
    Console.WriteLine("Content length: {0}", m.Groups[1]);


Not tested.


Thank You My final code is now
            Match m = Regex.Match(sBuffer.Trim(), @"Content-Length: ([-\d]+)\r\n", RegexOptions.IgnoreCase);
            if (m.Success)
            {
                contentLength = Convert.ToInt32((m.Groups[1].ToString()));
            }



I have based my code on your help, some online documentation and a bit of trial and error, Thanks
Was This Post Helpful? 0
  • +
  • -

#7 EvLSnoopY  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 93
  • Joined: 24-November 09

Re: String Manipulation

Posted 24 May 2010 - 05:25 PM

I know you've figured it out but this is what I came up with.
string sentence = "blar blar blar\r\n Content-Length: 35\r\nblar blar blar";
string[] delimiter = { "\r\n", "blar", ":", " " };
string number = "35";
string[] buff = sentence.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
            
for ( int i = 0; i < buff.Length; i++)
{
    if ( buff[i].Equals(number) )
    {
       Console.WriteLine(buff[i].ToString());
    }
}


You can change the loop but I was just messing around with piece of code.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1