This question already has answers here:
How to split string while ignoring portion in parentheses?
(5 answers)
Closed 1 year ago.
I have a nested comma string such as
a(x,y,z),b,c(n,o,p),d,e,f(t,w)
Want to split this string in C# such as
a(x),a(y),a(z),b,c(n),c(o),c(p),d,e,f(t),f(w)
I tried splitting using combination String.Split & String.SubString. Please let me know f you any solution for this problem.
Many problems get easier if you split them into smaller problems. This is one of them.
Step 1: Split on , while ignoring separators in parenthesis (see this related question for a regex-based solution: How to split string while ignoring portion in parentheses?)
This will yield a(x,y,z), b, c(n,o,p), ...
Step 2: Split the part before and inside the parenthesis (using a regular expression or just String.Split), split the inside part on ,, loop through it and add the component before the parenthesis.
This will transform a(x,y,z) into a(x), a(y), ...
Related
This question already has answers here:
Replace the last occurrence of a word in a string - C#
(7 answers)
Closed last year.
I've got string "55, 55,55, 55, " and I want to replace last two characters (comma and white space) to nothing using only the method .Replace(). The example below clear all commas but I want to clear only two last ones. In Java you write:
variable.replaceAll(", $",""); // it's correct effect two last characters are changed to nothing
Replace(", ","") // but in c# you get: 55555,5555 and Replace(", $","") doesn't work
How to write it properly to get this effect using only Replace method?
Use Regex.Replace:
Regex.Replace(input, ", $", "");
If it is always two characters, you could also combine String.EndsWith and String.Substring, which is likely cheaper than compiling and applying a regex:
if (input.EndsWith(", ")) {
input = input.Substring(0, input.Length - 2);
}
This question already has answers here:
Efficient way to remove ALL whitespace from String?
(18 answers)
Closed 3 years ago.
I'm looking for a efficient way for removing all of the white spaces in an string.
I have checked replace (replace(' ','')) but I'm looking for a more efficient way.
I'd appreciate the help.
You may use Regular Expression.
For example:
var result=System.Text.RegularExpressions.Regex.Replace(input, #"\s+", "");
Input is your string
See more Removing whitespaces using C#
This question already has answers here:
How to use string.Endswith to test for multiple endings?
(9 answers)
Closed 5 years ago.
I need to check if a string last word is either "...abc" or "...xyz" or "...fgh".
How i can achieve the same thing using regex as i am trying to learn it?
e.g Sentence 1: Hi My Name is abc.
Sentence 2: I live in xyz.
The above sentence is a sample one to demonstrate.
You don't need any Regex. Just use String.EndsWith :
string a = "asdasd abc";
Console.WriteLine(a.EndsWith("abc.") || a.EndsWith("xyz.") || a.EndsWith("fgh."));
You can use this simple regex pattern:
(abc|xyz|fgh)$
Put your possible options between parenthesis separated by pipes. The $ means the end of the string.
This question already has answers here:
Remove text in-between delimiters in a string (using a regex?)
(5 answers)
Closed 6 years ago.
I am trying to remove characters starting from (and including) rgm up to (and including) ;1..
Example input string:
Sum ({rgmdaerudsb;1.Total_Value}, {rgmdaerub;1.Major_Value})
Code:
string strEx = "Sum ({rgmdaerudsb;1.Total_Value}, {rgmdaerub;1.Major_Value})";
strEx = strEx.Substring(0, strEx.LastIndexOf("rgm")) +
strEx.Substring(strEx.LastIndexOf(";1.") + 3);
Result:
Sum ({rgmdaerub;1.Total_Value}, {.Major_Value})
Expected result:
Sum ({Total_Value}, {Major_Value})
Note: only rgm and ;1. will remain static and characters between them will vary.
I would recommend to use Regex for this purpose. Try this:
string input = "Sum ({rgmdaerudsb;1.Total_Value}, {rgmdaerub;1.Major_Value})";
string result = Regex.Replace(input, #"rgm.*?;1\.", "");
Explanation:
The second parameter of Regex.Replace takes the pattern that consists of the following:
rgm (your starting string)
. (dot - meaning any character)
*? (the preceding symbol can occure zero or more times, but stops at the first possible match (shortest))
;1. (your ending string - the dot needed to be escaped, otherwise it would mean any character)
You need to use RegEx, with an expression like "rgm(.);1\.". That's just off the top of my head, you will have to verify the exact regular expression that matches your pattern. Then, use RegEx.Replace() with it.
This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 7 years ago.
I want to replace a single comma (,) with (',').
For example:
"text,text,text" with "text','text','text".
I tried
MyText.Replace(',',"','");
but can't get anything working properly.
Any help would be appreciated.
Try:
MyText = MyText.Replace(",","','");
There are two .Replace methods on strings. One for single characters and one for strings.
When you ',' that defines a single character so it goes to the single character version of the method. If you use double quotes then it defines a string so that version of the method is selected.
Docs on the string replace method: https://msdn.microsoft.com/en-us/library/system.string.replace(v=vs.110).aspx