0

I have string "10-1283-01" the length going to be same , i want to check is that string like "10-xxxx-0x" only want first 3 character like "10-" and last but one two character like "-0".

So I am diving/Substring something like below. for first3 its giving me required string but for lastPart its giving me ArgumentOutOfRangeException.

Please let me know how i will get -0 string? Thanks in Advance

 string partnoveri = lpartno.Text; string first3=partnoveri.Substring(0, 3); string lastPart = partnoveri.Substring(partnoveri.IndexOf("-0"),partnoveri.Length-1); 
1
  • if ((partnoveri.Substring(0,3)=="10-"&&(partnoveri .substring(7,2)=="-0")) then valid else not valid Commented May 20, 2016 at 23:35

4 Answers 4

1

why dont you use regular expression?

using System; using System.Threading; using System.Threading.Tasks; using System.Linq; using System.Collections.Generic; using Newtonsoft; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Linq; using System.Text.RegularExpressions; class Program { static void Main(string[] args) { try { var pattern = "[0-9]{2}-[0-9]{4}-[0-9]{2}"; var input = "10-1283-01"; var matches = Regex.Matches(input, pattern); if(Regex.IsMatch(input, pattern)) { Console.WriteLine("MATCH"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } } 
Sign up to request clarification or add additional context in comments.

5 Comments

The OP gave a criterion for which the regex pattern would be "^10-[0-9]{4}-0[0-9]$" (assuming the x is intended to be [0-9]).
my fault. I'm sure he can figure it out. hopefully, he'll invest some time and learn regex.
@KMC thanks for your help. that's work great for me, in my coding i need to add some more patterns like "15-xxxxx-0x","800-xxxxx-0x" etc. so I am trying to understand who write var pattern. I am using msdn.microsoft.com/en-us/library/ff650303.aspx . Any advice where i get clear idea of Regex Pattern. I dont have much time to go in details right now.
You can learn regex expressions from here. Give it a try and if you're really stuck then reply and I'll give you some help. msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx
@KMC...I got it. Thank you so much for your help.
1

The C# Substring method is not

String.Substring( int startIndex, int endIndex ) 

as your code suggests, rather it is

String.Substring( int startIndex [, int numberofCharacters ] ) 

Given that, if you're looking for two characters, you'd use:

string lastPart = partnoveri.Substring(partnoveri.IndexOf("-0"), 2); 

That being said, I'd opt for @KMC's regular expression solution. While Substring will work in the case of your example, it will fail if the fourth character is zero, as in 10-0283-01

Comments

1

Just reading about Ranges introduces in C# 8.0 spec.

Reading this question:

I have string "10-1283-01" the length going to be same , i want to check is that string like "10-xxxx-0x" only want first 3 character like "10-" and last but one two character like "-0"."

one can do (since c# 8.0 specs):

string partno = "10-1283-01"; Console.WriteLine($"{partno[0..3]}"); // returns "10-" Console.WriteLine($"{partno[^3..^1]}"); // returns "-0" 

Comments

0

To receive two last characters you can use substring like below:

partnoveri.Substring(partnoveri.Length-2, 2);

You can also use Regular Expression for this case.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.