Quantcast
Channel: West Wind Message Board Messages
Viewing all articles
Browse latest Browse all 10393

String Manipulation

$
0
0
String Manipulation
.NET Development
String Manipulation
11/07/2012
12:36:38 PM
3N00R13WFShow this entire thread in new window
Gratar Image based on email address
From:Craig Tucker
To:All
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(); }

HTML Help Builder

Viewing all articles
Browse latest Browse all 10393

Trending Articles