.NET Development
Re: String Manipulation
From: | Craig Tucker |
To: | Gregory |
Very Nice! This gives me something good to study.
Thanks much, Craig
Craig,
RegExes work. Try this. Whether it is any better, I don't know
class RegexTest {publicstaticvoid Main() {string[] tests = { @"XYZ Company [- Phone: [({404}) ]{321-4321} [Ext: {6789}]]", @"XYZ Company [- Phone: [({404}) ]{321-4321} [Ext: {}]]", @"XYZ Company [- Phone: [({}) ]{321-4321} [Ext: {}]]", @"XYZ Company [- Phone: [({}) ]{6} [Ext: {}]]" };foreach (string s in tests) {string newValue = StoryManipulation(s); Console.WriteLine("{0}\n{1}\n", s, newValue); } Console.ReadLine(); } privatestaticstring StoryManipulation(string theStory) {string item = @"(?<Item>\[[^[{]*[{]VariablePattern[}][^]]*\])";string variablePattern = @"(?<VariableContents>[^}]*)";string pattern = item.Replace("VariablePattern", variablePattern); MatchCollection matches;while ((matches = Regex.Matches(theStory, pattern, RegexOptions.Singleline)).Count > 0) { Match match = matches[matches.Count -1];string s = match.Groups["Item"].ToString();if (match.Groups["VariableContents"].Length == 0) {// replace [ ...] with '' theStory = theStory.Remove(match.Groups["Item"].Index, match.Groups["Item"].Length); }else {// remove [ ] { and } to avoid subsequent match theStory = theStory.Remove(match.Groups["Item"].Index + match.Groups["Item"].Length - 1, 1) // ] .Remove(match.Groups["VariableContents"].Index + match.Groups["VariableContents"].Length, 1) // } .Remove(match.Groups["VariableContents"].Index - 1, 1) // { .Remove(match.Groups["Item"].Index , 1) // [ ; } } return theStory; } }
Hi Everyone,
This was presented to me by one of our graphic guys. I thought I'd post it to you guru's to play with too...
I suspect this would be better written using Regular Expressions but am not that proficient with them yet. Thoughts?
Thanks much, Craig
Given the string "story" below, write a script to do the following.
1. Variable text is enclosed by { }.
2. If the variable text is blank, remove any other text enclosed in [ ].
3. Text to be removed can be nested deep with [ ].
"XYZ Company [- Phone: [({404}) ]{321-4321} [Ext: {6789}]]"
Examples.
1. All variable text filled in.
XYZ Company - Phone: (404) 321-4321 Ext: 6789
2. No Extension entered, remove "Ext:".
XYZ Company - Phone: (404) 321-4321
3. No Extension and no area code entered, remove "Ext:" and "( ) ".
XYZ Company - Phone: 321-4321
4. No extension, no phone number, and no area code entered, remove "Ext:" and "( ) " and "- Phone: ".
XYZ Company
privatestring StoryManipulation(string theStory) {// Loop through story while there are still curly bracketswhile (theStory.IndexOf("{") > 0) {// Extract the first curly text areastring lcCurlyText = StringUtils.ExtractString(theStory, "{", "}"); // Look for surrounding brackets and blank all text betweenif (String.IsNullOrWhiteSpace(lcCurlyText)) {for (int lnCounter = theStory.IndexOf("{"); lnCounter >= 0; lnCounter--) {if (theStory.Substring(lnCounter - 1, 1) == "[") {string lcSquareText = StringUtils.ExtractString(theStory.Substring(lnCounter - 1), "[", "]"); theStory = StringUtils.ReplaceString(theStory, ("[" + lcSquareText + "]"), "", false);break; } } }else {// Replace current curly brackets surrounding the text theStory = StringUtils.ReplaceString(theStory, ("{" + lcCurlyText + "}"), lcCurlyText, false); } }// Replace all brackets with blank (-1 all instances) theStory = StringUtils.ReplaceStringInstance(theStory, "[", "", -1, false); theStory = StringUtils.ReplaceStringInstance(theStory, "]", "", -1, false);return theStory.Trim(); }