-4

I have a string and i want to detect the value next to the string . for eg:

string s="the #awb 58981140743 is the number";

The value next to #awb needs to be detected.I want the result string to be (after #awb) .

string output=58981140743;
8
  • 3
    Okay, so have you read through the string documentation to see which methods might be applicable? (Hint: IndexOf and Substring...) Also, this isn't really specific to ASP.NET at all..
    – Jon Skeet
    Commented Jun 15, 2017 at 11:42
  • 1
    RegEx is what you are looking for :)
    – Bambuk
    Commented Jun 15, 2017 at 11:42
  • StackOverflow is not a code writing service. Did you try to solve this by yourself?
    – steliosbl
    Commented Jun 15, 2017 at 11:45
  • I have tried both Substring and RgEx.
    – bk sumedha
    Commented Jun 15, 2017 at 11:46
  • Exactly. Regex.Match(s, @"#awb\s*(\d+)").Groups[1].Value - add a check to see if a match is .Success Commented Jun 15, 2017 at 11:46

3 Answers 3

2

Use below regex to solve your problem.

string s = "the #awb 58981140743 is the number";

string result = Regex.Replace(s, @"[^\d]", "");

If you wan't specific string after #awb then use below code

string s = "the #awb 58981140743 is the number";

Regex.Match(s, @"#awb\s*(\d+)").Groups[1].Value;
3
  • This works.Exactly .So regular expression is what i Wanted to use.
    – bk sumedha
    Commented Jun 15, 2017 at 11:54
  • one of the best way to manipulate strings is by regular expresions : regular-expressions.info/examples.html Commented Jun 15, 2017 at 12:24
  • It certainly works for integer values.Whats ur opinion on values which includes the decimal values. Say that the value is 25.14. Regex.Match(s, @"#awb\s*(\d+)").Groups[1].Value; .This code fetches only the value of 25 and neglects .14 value
    – bk sumedha
    Commented Jun 15, 2017 at 17:29
0
string s = "the #awb 58981140743 is the number";

            if (s.IndexOf("#awb") >= 0)
            {
                var temp = s.Split(new string[] { "#awb" }, StringSplitOptions.None)[1];
            }

this code snippet will give you the result.

0
string s="the #awb 58981140743 is the number";
var splittedValue= s.Split(new string[] { "#awb" }, StringSplitOptions.None)[1];

This is how simply you can split and show your value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.