ajita srivastava
ajita srivastava

Reputation: 45

Get a part of string

I have a string as

"cmp197_10_27_147ee7b825-2a3b-4520-b36c-bba08f8b0d87_TempDoc_197"

I want to fetch the last digits(i.e. 197) For obtaining same i have implemented below code

int lastUnderscore = addUcoid.LastIndexOf('_');
string getucoid = addUcoid.Substring(lastUnderscore).TrimStart('_');

The getucoid string gets the digit part properly.The problem now is that I have to also check if the digits occur in string,i.e. string can be like

"cmp197_10_27_147ee7b825-2a3b-4520-b36c-bba08f8b0d87_TempDoc"

How to perform the check on such string, whether that part(197) exists at the end in the string or not.

Here 197 is just an example.It could be any numeric data,for example 196,145,etc

Upvotes: 1

Views: 106

Answers (7)

Tyress
Tyress

Reputation: 3653

When you say exists at the "end in the string", do you mean if it exists anywhere in the string? You can do a .Contains() to get it.

bool inTheString = addUcoid.Contains(getucoid);

Also:

FYI, your code:

int lastUnderscore = addUcoid.LastIndexOf('_');
string getucoid = addUcoid.Substring(lastUnderscore).TrimStart('_');

can be simplified shortened into:

string getucoid = addUcoid.Split('_').Last();

EDIT:

Ehm, Marc Gravell rightly points out that the string is there anyway. If you want to find out if another instance of the string is there (aside from getucoid):

int lastUnderscore = addUcoid.LastIndexOf('_');
string getucoid = addUcoid.Split('_').Last();
bool inTheString = addUcoid.Substring(0,lastUnderscore).Contains(getucoid);

Upvotes: 0

Fratyx
Fratyx

Reputation: 5797

I think Regex is the easiest way. If match.Success is true the digits have been found.

Match match = System.Text.RegularExpressions.Regex.Match(addUcoid, @"(?<=_)\d+$");
if(match.Success)
{
    int i = int.Parse(match.Value);
}

Upvotes: 1

w.b
w.b

Reputation: 11228

You can use LINQ (also to check if the last char is a digit):

string str = "cmp197_10_27_147ee7b825-2a3b-4520-b36c-bba08f8b0d87_TempDoc_197";

if (Char.IsDigit(str.Last()))
{
    string digits = new string(str.Reverse().TakeWhile(c => Char.IsDigit(c)).Reverse().ToArray());
}

Upvotes: 0

Patrice Gahide
Patrice Gahide

Reputation: 3744

I suspect you just want to know if this last part is a numeric or not:

bool isNumeric = Regex.IsMatch(getucoid, @"^\d+$");

Upvotes: 0

elnigno
elnigno

Reputation: 1821

The presence of a substring can be checked with String.Contains(): http://msdn.microsoft.com/en-us/library/dy85x1sa%28v=vs.110%29.aspx

Assuming you have removed the final "_197" after checking that it exists, calling Contains("197") on your string will return true if "197" is a substring, or false if it isn't.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062705

string.Contains won't help: we know it is there at least once already. I would use:

int otherLocation = addUcoid.IndexOf(getucoid);

and compare this to lastUnderscore. If otherLocation is non-negative and less than lastUnderscore, then it is there in an earlier position too. You could also use:

int otherLocation = addUcoid.IndexOf(getucoid, 0, lastUnderscore);

and compare to -1; this second approach stops at the underscore, so won't find the instance from the end.

Upvotes: 2

malkam
malkam

Reputation: 2355

Use String.EndsWith method to findout.

http://msdn.microsoft.com/en-us/library/2333wewz%28v=vs.110%29.aspx

 string s="cmp197_10_27_147ee7b825-2a3b-4520-b36c-bba08f8b0d87_TempDoc";
    bool isends=s.EndsWith("197");//returns false;
    s="cmp197_10_27_147ee7b825-2a3b-4520-b36c-bba08f8b0d87_TempDoc_197";
    isends=s.EndsWith("197");//returns true;

Upvotes: 0

Related Questions