-1

Hi I am facing problem to get specific string. The string as below:

string myPurseBalance = "Purse Balance: +0000004000 556080";

I want to get 4000 out only.

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
user2156169
  • 41
  • 1
  • 5
  • 6
    What is your pattern? You want to get last `4` digits that starts with `+` for example? – Soner Gönül Jun 13 '14 at 07:29
  • If you want to extract desired value from the passed string and you know the format you want to output. You can use Regular Expression to do so – K D Jun 13 '14 at 07:31
  • 5
    Use SubString. myPursebalance.SubString(22,4); – captainsac Jun 13 '14 at 07:31
  • 2
    @captainsac I don't think this is a good way. It works on this case but it is not a general solution.. – Soner Gönül Jun 13 '14 at 07:31
  • 2
    @SonerGönül As there is only one string specified and is there is no any pattern then substring is a good way to go for – captainsac Jun 13 '14 at 07:33
  • @SonerGönül - Given the amount of effort provided by the OP I think its a pretty good answer. OP, what have you tried? – Sayse Jun 13 '14 at 07:33
  • 2
    I like when one comes here, ask (politely) as simple enough question that he/she should at least try to solve by itself, and then sits at its monitor waiting someone to solve the problem for her/him. At least. What have you tried? – Steve Jun 13 '14 at 07:33
  • you can do a simple split on '+' to remove string... then split again on ' ' to give only 00000004000... then convert it to int or double – angel Jun 13 '14 at 07:35
  • I'm with @captainsac - given the example in the question, SubString will work 100%. Now, if the questioner wanted something more flexible, they should have made that clear. – Polyfun Jun 13 '14 at 08:14

4 Answers4

3

if your string format/pattern is fix then you can get specific value

  string myPurseBalance = "Purse Balance: +0000004000 556080";
            //
  var newPursebal =Convert.ToDouble(myPurseBalance.Split('+')[1].Split(' ')[0]);
Anant Dabhi
  • 10,864
  • 3
  • 31
  • 49
2

You can use this regular expression:

string extract = Regex.Replace(myPurseBalance, @"(.*?)\: \+[0]*(?<val>\d+) (.*)", "${val}")

It searches for the decimals after :, trims the leading 0s and removes everything after the last whitespace.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

You can use string.Split to get the +0000004000 and then use string.Substring to get the last four characters by passing the Length-4 as starting index.

string str = myPurseBalance.Split(' ')[2];
str = str.Substring(str.Length-4);
Adil
  • 146,340
  • 25
  • 209
  • 204
1

Learn Regex . Here is just simple tutorial

using System; 
using System.Text.RegularExpressions;
namespace regex
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            string input = "Purse Balance: +0000504000 556080";

    // Here we call Regex.Match.
    Match match = Regex.Match(input, @"\+[0]*(\d+)",
        RegexOptions.IgnoreCase);

    // Here we check the Match instance.
    if (match.Success)
    {
        // Finally, we get the Group value and display it.
        string key = match.Groups[1].Value;
        Console.WriteLine(key);
    }
        }
    }
}
qwr
  • 3,660
  • 17
  • 29