Home > OS >  Splitting string value c#
Splitting string value c#

Time:11-09

I want to know about how to splitting a value in string format in to two parts. Here in my asp application I'm parsing string value from view to controller.

And then I want to split the whole value in to two parts.

Example like: Most of the times value firest two letters could be TEXT value (like "PO" , "SS" , "GS" ) and the rest of the others are numbers (SS235452).

The length of the numbers cannot declare, since it generates randomly. So Want to split it from the begining of the string value. Need a help for that.

My current code is

string approvalnumber = approvalCheck.ApprovalNumber.ToUpper();

Thanks.

CodePudding user response:

As you already mentioned that first part will have 2 letters and it's only second part which is varying, you can use Substring Method of String as shown below.

        var textPart = input.Substring(0,2);
        var numPart = input.Substring(2);

The first line fetches 2 characters from starting index zero and the second statement fetches all characters from index 2. You can cast the second part to a number if required.

Please note that the second parameter of Substring is not mentioned in second line. This parameter is for length and if nothing is mentioned it fetches till end of string.

CodePudding user response:

You could try using regex to extract alpha, numbers from the string.

This javascript function returns only numbers from the input string.

function getNumbers(input) {
            return input.match(/[0-9] /g);
}

CodePudding user response:

I'd use a RegExp. Considering the fact that you indicate ASP-NET-4 I assume you can't use tuples, out var etc. so it'd go as follows:

using System.Text.RegularExpressions;
using FluentAssertions;
using Xunit;

namespace Playground
{
    public class Playground
    {
        public struct ProjectCodeMatch
        {
            public string Code { get; set; }

            public int? Number { get; set; }
        }

        [Theory]
        [InlineData("ABCDEFG123", "ABCDEFG", 123)]
        [InlineData("123456", "", 123456)]
        [InlineData("ABCDEFG", "ABCDEFG", null)]
        [InlineData("ab123", "AB", 123)]
        public void Split_Works(string input, string expectedCode, int? expectedNumber)
        {
            ProjectCodeMatch result;
            var didParse = TryParse(input, out result);

            didParse.Should().BeTrue();
            result.Code.Should().Be(expectedCode);
            result.Number.Should().Be(expectedNumber);
        }

        private static bool TryParse(string input, out ProjectCodeMatch result)
        {
            /*
             * A word on this RegExp:
             * ^ - the match must happen at the beginning of the string (nothing before that)
             * (?<Code>[a-zA-Z] ) - grab any number of letters and name this part the "Code" group
             * (?<Number>\d ) - grab any number of numbers and name this part the Number group
             * {0,1} this group must occur at most 1 time
             * $ - the match must end at the end of the string (nothing after that)
             */
            var regex = new Regex(@"^(?<Code>[a-zA-Z] ){0,1}(?<Number>\d ){0,1}$");

            var match = regex.Match(input);

            if (!match.Success)
            {
                result = default;
                return false;
            }

            int number;
            var isNumber = int.TryParse(match.Groups["Number"].Value, out number);

            result = new ProjectCodeMatch
            {
                Code = match.Groups["Code"].Value.ToUpper(),
                Number = isNumber ? number : null
            };
            return true;
        }
    }
}

CodePudding user response:

A linq answer:

string d = "PO1232131";
string.Join("",d.TakeWhile(a => Char.IsLetter(a)))
  • Related