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;
This question already has answers here:
How to get the last five characters of a string using Substring() in C#?
(12 answers)
Closed 4 years ago.
If a string that contains integers and I want to split it based on integers occurrence. how to do that?
string test= "a b cdf 7654321;
then I want to store the integer and all words before it, like this
string stringBefore="";
// the integer will be last item
string integer="";
Note:
in my case the integer always will be 7 digits
You can use Regex.Split with a capture group to return the delimiter:
var ans = Regex.Split(test, #"("\d{7})");
If the number is at the end of the string, this will return an extra empty string. If you know it is always at the end of the string, you can split on its occurrence:
var ans = Regex.Split(test, #"(?=\d{7})");
According to your comments the integer is always 7 digits and it is always the last item of the string.
In that case, just use Substring()
string test = "a b cdf 7654321";
string stringBefore = test.Substring(0, test.Length - 7);
string integer = test.Substring(test.Length - 7);
Substring just makes a string based on a portion of your original string.
EDIT
I was a little surprised to find there wasn't a built in way to easily split a string in to two strings based on an index (maybe I missed it). I came up with a LINQ extension method that achieves what I was trying to do, maybe you will find it useful:
public static string[] SplitString(this string input, int index)
{
if(index < 0 || input.Length < index)
throw new IndexOutOfRangeException();
return new string[]
{
String.Concat(input.Take(input.Length - index)),
String.Concat(input.Skip(input.Length - index))
};
}
I think I would rather use a ValueTuple if using C# 7, but string array would work too.
Fiddle for everything here
This question already has answers here:
Delete last char of string
(12 answers)
Closed 8 years ago.
I want to simply remove the last character of a string that is of an undefined length. This is what I have:
var str = txtBx1.Text;
txtBx2.Text = (str.TrimEnd(str[str.Length - 1]));
It works great, when the last two characters are unique. However, when the last two or more characters are the same, all of the repeating characters are removed.
If txtBx1.Text = '123456789' then txtBx2.Text will be '12345678'
If txtBx1.Text = '199999999' then txtBx2.Text will be '1'; it needs to be '19999999'
How can I simply remove the last character of a string that is of an undefined length?
Use Substring method
txtBx2.Text = str.Substring(0, str.Length - 1);
Use substring:
str.Substring(0, str.Length - 1);
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.
I want to remove last three characters from a string:
string myString = "abcdxxx";
Note that the string is dynamic data.
read last 3 characters from string [Initially asked question]
You can use string.Substring and give it the starting index and it will get the substring starting from given index till end.
myString.Substring(myString.Length-3)
Retrieves a substring from this instance. The substring starts at a
specified character position. MSDN
Edit, for updated post
Remove last 3 characters from string [Updated question]
To remove the last three characters from the string you can use string.Substring(Int32, Int32) and give it the starting index 0 and end index three less than the string length. It will get the substring before last three characters.
myString = myString.Substring(0, myString.Length-3);
String.Substring Method (Int32, Int32)
Retrieves a substring from this instance. The substring starts at a
specified character position and has a specified length.
You can also using String.Remove(Int32) method to remove the last three characters by passing start index as length - 3, it will remove from this point to end of string.
myString = myString.Remove(myString.Length-3)
String.Remove Method (Int32)
Returns a new string in which all the characters in the current
instance, beginning at a specified position and continuing through the
last position, have been deleted
myString = myString.Remove(myString.Length - 3, 3);
I read through all these, but wanted something a bit more elegant. Just to remove a certain number of characters from the end of a string:
string.Concat("hello".Reverse().Skip(3).Reverse());
output:
"he"
The new C# 8.0 range operator can be a great shortcut to achieve this.
Example #1 (to answer the question):
string myString = "abcdxxx";
var shortenedString = myString[0..^3]
System.Console.WriteLine(shortenedString);
// Results: abcd
Example #2 (to show you how awesome range operators are):
string s = "FooBar99";
// If the last 2 characters of the string are 99 then change to 98
s = s[^2..] == "99" ? s[0..^2] + "98" : s;
System.Console.WriteLine(s);
// Results: FooBar98
myString.Remove(myString.Length-3);
string test = "abcdxxx";
test = test.Remove(test.Length - 3);
//output : abcd
You can use String.Remove to delete from a specified position to the end of the string.
myString = myString.Remove(myString.Length - 3);
Probably not exactly what you're looking for since you say it's "dynamic data" but given your example string, this also works:
? "abcdxxx".TrimEnd('x');
"abc"
If you're working in C# 8 or later, you can use "ranges":
string myString = "abcdxxx";
string trimmed = myString[..^3]; // "abcd"
More examples:
string test = "0123456789", s;
char c;
c = test[^3]; // '7'
s = test[0..^3]; // "0123456"
s = test[..^3]; // "0123456"
s = test[2..^3]; // "23456"
s = test[2..7]; // "23456"
//c = test[^12]; // IndexOutOfRangeException
//s = test[8..^3]; // ArgumentOutOfRangeException
s = test[7..^3]; // string.Empty
str= str.Remove(str.Length - 3);
myString.Substring(myString.Length - 3, 3)
Here are examples on substring.>>
http://www.dotnetperls.com/substring
Refer those.
string myString = "abcdxxx";
if (myString.Length<3)
return;
string newString=myString.Remove(myString.Length - 3, 3);
Easy. text = text.remove(text.length - 3). I subtracted 3 because the Remove function removes all items from that index to the end of the string which is text.length. So if I subtract 3 then I get the string with 3 characters removed from it.
You can generalize this to removing a characters from the end of the string, like this:
text = text.remove(text.length - a)
So what I did was the same logic. The remove function removes all items from its inside to the end of the string which is the length of the text. So if I subtract a from the length of the string that will give me the string with a characters removed.
So it doesn't just work for 3, it works for all positive integers, except if the length of the string is less than or equal to a, in that case it will return a negative number or 0.
Remove the last characters from a string
TXTB_DateofReiumbursement.Text = (gvFinance.SelectedRow.FindControl("lblDate_of_Reimbursement") as Label).Text.Remove(10)
.Text.Remove(10)// used to remove text starting from index 10 to end
items.Remove(items.Length - 3)
string.Remove() removes all items from that index to the end. items.length - 3 gets the index 3 chars from the end
You can call the Remove method and pass the last 3 characters
str.Substring(str.Length-3)
Complete code can be
str.Remove(str.Substring(str.Length-3));