For Example, I am having a string like:
string str = "santhosh,ravi,phani,praveen,sathish,prakash";
Now , I need to remove ,phani from str.
can any one help me?
I tried by using trim and substring.but I am not getting that.
Try the following,
str = str.Replace(",phani", string.Empty);
You can use String.Replace() method for that;
Returns a new string in which all occurrences of a specified Unicode
character or String in the current string are replaced with another
specified Unicode character or String.
string str = "santhosh,ravi,phani,praveen,sathish,prakash";
str = str.Replace(",phani", "");
Console.WriteLine(str);
Here is a DEMO.
Related
I have a string value returned by a method and it's within quotation. As an example '59'. Now i want to remove quotation marks and give an output as 59. How can i do it?
'59' -> 59
The simplest way I think is to replace the ' with an empty string:
string Quoted = "'59'";
string UnQuoted = Quoted.Replace("'", "");
yet another more generic solution to extract numeric values from strings:
string str = "'59'";
int num = int.Parse(Regex.Match(str, #"\d+").Value); //59
I want to match only numbers in the following string
String : "40’000"
Match : "40000"
basically tring to ignore apostrophe.
I am using C#, in case it matters.
Cant use any C# methods, need to only use Regex.
Replace like this it replace all char excpet numbers
string input = "40’000";
string result = Regex.Replace(input, #"[^\d]", "");
Since you said; I just want to pick up numbers only, how about without regex?
var s = "40’000";
var result = new string(s.Where(char.IsDigit).ToArray());
Console.WriteLine(result); // 40000
I suggest use regex to find the special characters not the digits, and then replace by ''.
So a simple (?=\S)\D should be enough, the (?=\S) is to ignore the whitespace at the end of number.
DEMO
Replace like this it replace all char excpet numbers and points
string input = "40’000";
string result = Regex.Replace(input, #"[^\d^.]", "");
Don't complicate your life, use Regex.Replace
string s = "40'000";
string replaced = Regex.Replace(s, #"\D", "");
I need a c# function which will replace all special characters customized by the client from a string Example
string value1 = #"‹¥ó׬¶ÝÆ";
string input1 = #"Thi¥s is\123a strÆing";
string output1 = Regex.Replace(input1, value1, "");
I want have a result like this : output1 =Thi s is\123a str ing
Why do you need regex? This is more efficient, concise also readable:
string result = string.Concat(input1.Except(value1));
If you don't want to remove but replace them with a different string you can still use a similar(but not as efficient) approach:
string replacement = "[foo]";
var newChars = input1.SelectMany(c => value1.Contains(c) ? replacement : c.ToString());
string result = string.Concat( newChars ); // Thi[foo]s is\123a str[foo]ing
Someone asked for a regex?
string value1 = #"^\-[]‹¥ó׬¶ÝÆ";
string input1 = #"T-^\hi¥s is\123a strÆing";
// Handles ]^-\ by escaping them
string value1b = Regex.Replace(value1, #"([\]\^\-\\])", #"\$1");
// Creates a [...] regex and uses it
string input1b = Regex.Replace(input1, "[" + value1b + "]", " ");
The basic idea is to use a [...] regex. But first you have to escape some characters that have special meaning inside a [...]. They should be ]^-\ Note that you don't need to escape the [
note that this solution isn't compatible with non-BMP unicode characters (characters that fill-up two char)
A solution that is compatible with them is more complex, but for normal use it shouldn't be a problem.
Does anybody know I can replace a group of numbers in a string by one *. For example if I have a string like this "Test123456.txt", I want to convert it to "Test#.txt". I have seen plenty of examples that can replace each individual number with a new character, but none that deal with a group of numbers. Any help is much appreciated!
Regex r = new Regex(#"\d+", RegexOptions.None);
Console.WriteLine(r.Replace("Test123456.txt", "#"));
Console.Read();
Use Regex.Replace() as follows:
string fileName = "Test12345.txt";
string newFileName = Regex.Replace(fileName, #"[\d]+", "#");
you can use regex, to do this, but if you know the exact text, then using the string.Replace method would be more efficient:
string str = "blahblahblahTest123456.txt";
str = string.Replace("Test#.txt","Test123456.txt");
I would like to declare ^\ this as a string or character. I used the below but i am getting an error
string str="^\";
So can any one help me
You have to escape the backslash.
string str = "^\\";
Or, you can use the verbatim syntax.
string str = #"^\";
\ is the escape token, you have to double it to use it as a literal in a string:
string str = "^\\";
Alternatively, you can use literal string syntax:
string str = #"^\";
You need to escape backslash characters, so instead of:
string str = "^\";
use:
string str = "^\\";
I have no idea about C#, but can you try:
string str="^\\";