Get the rest of the string after occurrence of particular letters [duplicate] - c#

This question already has answers here:
Regular Expression Groups in C#
(5 answers)
Closed 4 years ago.
string ABC = "This is test AZ12346";
Need value that occurs after AZ. AZ will always be present in the string and it will always be the last word. But number of characters after AZ will vary.
Output should be : AZ123456

Simply :
ABC.Substring(ABC.LastIndexOf("AZ"));

You can try LastIndexOf and Substring:
string ABC = "This is test AZ12346";
string delimiter = "AZ";
// "AZ123456"
string result = ABC.Substring(ABC.LastIndexOf(delimiter));
In case delimiter can be abscent
int p = ABC.LastIndexOf(delimiter);
string result = p >= 0
? ABC.Substring(p)
: result; // No delimiter found
If you are looking for whole word which starts from AZ (e.g. "AZ123", but not "123DCAZDE456" - AZ in the middle of the word) you can try regular expressions
var result = Regex
.Match(ABC, #"\bAZ[A-Za-z0-9]+\b", RegexOptions.RightToLeft)
.Value;

Related

How to find the immediate integer value written before a string? [duplicate]

This question already has answers here:
How to get the digits before some particular word using regex in c#?
(8 answers)
Closed 2 years ago.
How to find the immediate integer value written before a string in c#? For example
50+ boxes were ordered, however only 2 are delivered.
I need to know the number of boxes (integer value) written just before "delivered". The output should be 2. I have written a code in c# using Regex:
string line = "50+ boxes were ordered, however only 2 are delivered.";
string boxesDelivered = Regex.Match(line, #"\d+").Value;
//The output I get is 50 instead of 2.
To get the last number that is followed by the word "delivered", you may use the following pattern:
\b\d+\b(?=[^\d]*\bdelivered\b)
Regex demo.
Here's a full example:
string line = "50+ boxes were ordered, however only 2 are delivered.";
var match = Regex.Match(line, #"\b\d+\b(?=[^\d]*\bdelivered\b)");
if (match.Success)
{
string boxesDelivered = match.Value;
// TODO: convert the value to a numeric type or use it as is.
}
Try it online.
written just before delivered
I'm going to take that verbatim as your user requirement - find the last number in the string that appears before "delivered".
You can use (\d+)[^\d]*(?:delivered), which says "match any sequence of numbers that does not occur before another sequence of numbers and does occur before delivered".
string line = "50+ boxes were ordered, however only 2 are delivered.";
string boxesDelivered = Regex.Match(line, #"(\d+)[^\d]*(?:delivered)").Groups[1].Value;
// boxesDelivered = 2

How can I get the first character of a string? [duplicate]

This question already has answers here:
C#: how to get first char of a string?
(15 answers)
Closed 4 years ago.
for example, i have some string
var a = '2,,2,';
var b = ',,,'
how to get only first character,
so i will get
a = '2'
b = ','
Use the index of the character you want, so:
a[0]
For a complete answer:
char getFirstChar(string inString, char defaultChar)
{
if (string.IsNullOrEmpty(inString)) return defaultChar;
return inString[0];
}
string s = "abcdefg";
string sB = s[0];
sB = "a"
string a= "abc";
string subString = a.Substring(0,1);
Characters in Strings can be accessed in the same way as arrays.
So for example, to get the first character in a string variable name you use the expression name[0]

splitting the string and choosing the middle part containing two set of parenthesis [duplicate]

This question already has answers here:
How do I extract text that lies between parentheses (round brackets)?
(19 answers)
Closed 7 years ago.
As I know for selecting a part of a string we use split. For example, if node1.Text is test (delete) if we choose delete
string b1 = node1.Text.Split('(')[0];
then that means we have chosen test, But if I want to choose delete from node1.Text how can I do?
Update:
Another question is that when there are two sets of parenthesis in the string, how one could aim at delete?. For example is string is test(2) (delete) - if we choose delete
You can also use regex, and then just remove the parentheses:
resultString = Regex.Match(yourString, #"\((.*?)\)").Value.
Replace("(", "").Replace(")", "");
Or better:
Regex.Match(yourString, #"\((.*?)\)").Groups[1].Value;
If you want to extract multiple strings in parentheses:
List<string> matches = new List<string>();
var result = Regex.Matches(yourString, #"\((.*?)\)");
foreach(Match x in result)
matches.Add(x.Groups[1].Value.ToString());
If your string is always xxx(yyy)zzz format, you can add ) character so split it and get the second item like;
var s = "test (delete) if we choose delete";
string b1 = s.Split(new[] { '(', ')' })[1];
string tmp = node1.Text.Split('(')[1];
string final = tmp.Split(')')[0];
Is also possible.
With the index [x] you target the part of the string before and after the character you have split the original string at. If the character occurs multiple times, your resulting string hat more parts.

Extract number from end of string [duplicate]

This question already has answers here:
Find and extract a number from a string
(32 answers)
Closed 8 years ago.
I have some strings like "pan1", "pan2", and "pan20" etc. I need to extract number. I use it:
char ch = s[(s.Length) - 1];
int n = Convert.ToInt32(Char.GetNumericValue(ch));
But in case of, for example, "pan20" the result is not correct 0.
Index Approach
if you know where is the starting index of the number then simply you can do this :
string str = "pan20";
int number = Convert.ToInt32(str.Substring(3));
Note that "3" is the starting index of the number.
Fixed Prefix Approach
try to remove "pan" from the string; like this
string str = "pan20";
int number = Convert.ToInt32(str.Replace("pan", ""));
Regular Expression Approach
use regular expression only when string contains undetermined text inside
string str = "pan20";
int number = Convert.ToInt32(System.Text.RegularExpressions.Regex.Match(str, #"\d+").Value;
You can use for example regular expressions, for example [0-9]+$ to get the numbers in the end. See the Regex class in MSDN.

Regex Limit the count of characters in a string [duplicate]

This question already has answers here:
How would you count occurrences of a string (actually a char) within a string?
(34 answers)
Closed 9 years ago.
Here is my string
string countCommas = 12,34,56
I am looking for REGEX for algorithm below
BOOL isCountExaclty2 = if(number of commas in string == 2){return TRUE;}else return FALSE
I want the right hand expression as one single REGEX expression which returns either TRUE or FALSE but not the count
(I know to use Regex.COUNT..but it ends up in 2 statements)
If you're looking for a pattern that will only match if there's exactly two commas in the string, this should work:
bool isCountExactly2 = Regex.IsMatch("12,34,56", "^([^,]*,){2}[^,]*$");
But regular expressions really aren't the right tool for this job.
Try this :
string countCommas = "12,34,56"
bool isCountExaclty2 = Regex.Split(countCommas, ",").Length == 2;

Categories

Resources