C # format processing class

Source: Internet
Author: User



 
 
 
The above is. I originally wanted to integrate it into my own tools, but after searching, there was no relevant code in China, so I had to go to a foreign website to search. You can simply apply the classes written by others. Just think about it. Haha. In addition, you can see how cool Microsoft Visual Studio 2012 is. That's awesome. I like this interface, although not quite suitable.
 
On VS2012.
 
 
 
No nonsense. Go to csharp content spinner code:
 
Public static class Spinner
{
// From http://www.xssxss.com
Private readonly static Random Randomizer = new Random ();
 
Public static string Spin (string content)
{
Const char OPEN_BRACE = '{';
Const char CLOSE_BRACE = '}';
Const char DELIMITER = '| ';
 
Var start = content. IndexOf (OPEN_BRACE );
Var end = content. IndexOf (CLOSE_BRACE );
 
If (start =-1 & end =-1 | start =-1 | end <start)
{
Return content;
}
 
If (end =-1)
{
Throw new ArgumentException ("Unbalanced brace .");
}
 
Var substring = content. Substring (start + 1, content. Length-(start + 1 ));
Var rest = Spin (substring );
End = rest. IndexOf (CLOSE_BRACE );
 
If (end =-1)
{
Throw new ArgumentException ("Unbalanced brace .");
}
 
Var splits = rest. Substring (0, end). Split (DELIMITER );
Var item = splits [Randomizer. Next (0, splits. Length)];
Return content. Substring (0, start) + item + Spin (rest. Substring (end + 1, rest. Length-(end + 1 )));
}
}
How to use it. I can demonstrate it.
Private void button11_Click (object sender, EventArgs e)
{
RichTextBox4.Text = Spinner. Spin (richTextBox3.Text );
}
Attach another SPINNER class. You just need to select a class.
Private string returnSpinnedPart (string part)
{
Random random = new Random ();
 
String [] values = part. Split ('| ');
 
If (values. Length> 0)
Return values [random. Next (0, values. Length)];
 
Return "";
}
// From http://www.xssxss.com
Private string returnSpinnedContent (string input)
{
String spinned = input;
Try
{
Regex regexObj = new Regex (@"\{.*? \} ", RegexOptions. Singleline );
MatchCollection m = regexObj. Matches (input );
Foreach (Match match in m)
{
String part = returnSpinnedPart (match. Value. Replace ("{", ""). Replace ("}",""));
Spinned = spinned. Replace (match. Value, part );
}
}
Catch (ArgumentException ex)
{
MessageBox. Show ("RegEx prob ");
}
Return spinned;
}
Private void button12_Click (object sender, EventArgs e)
{
RichTextBox4.Text = returnSpinnedContent (richTextBox3.Text );
}
In addition, I would like to ask for the invitation code for various forums. I found that a lot of black and wide forums require an invitation code or an original article. I hope that people who like aojing can hold a forum.
Email: shine52shine # 126.com

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.