String Array in Random Quote using C#

Submitted by: 
Language: 
Visitors have accessed this post 269 times.

This tutorial, we will create and describes string arrays and it randoms the quotes shows how they work in C# .net. In C# .net, you can use strings as array of characters, However, more common practice is to use the string keyword to declare a string variable. This is the basic implementation for a newbie in programming who plans to build a system in the future.

Sample Code

String Arrays Sample code

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class GenericSample
  5. {
  6. public static void Main(string[] args)
  7. {
  8. List<string> list = new List<string>();
  9. list.Add("First string");
  10. list.Add("Second string");
  11. list.Add("Third string");
  12. list.Add("Fourth string");
  13. for(int n = 0; n < list.Count; n++)
  14. {
  15. Console.WriteLine(list[n]);
  16. }
  17. }
  18. }

For creating a random quote, here is the syntax.

  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class RandomQuotes
  5. {
  6. static void CreateQuotes(List<string> quotes)
  7. {
  8. quotes.Add("I Hate [email protected]");
  9. quotes.Add("Long if its hot, short if its <a href="mailto:cold[email protected]Popstickle" rel="nofollow">[email protected]</a>");
  10. quotes.Add("John 3:<a href="mailto:16@Verse" rel="nofollow">[email protected]</a>");
  11. quotes.Add("Good [email protected]");
  12. quotes.Add("HAHAHA :P [email protected]");
  13. }
  14.  
  15. static void PrintQuote(string quote)
  16. {
  17. string[] substring = quote.Split('@');
  18. Console.WriteLine(substring[0]);
  19. Console.WriteLine("-" + substring[1]);
  20. }
  21.  
  22. static string GetRandomQuote(List<string> quotes)
  23. {
  24. int n = new Random().Next(0, quotes.Count);
  25. return(quotes[n]);
  26. }
  27.  
  28. public static void Main(string[] args)
  29. {
  30. List<string> list = new List<string>();
  31. CreateQuotes(list);
  32. string myRandomQuote = GetRandomQuote(list);
  33. if(args.Length == 0)
  34. {
  35. Console.WriteLine("Error Argument:\n * All\n * Random\n * Search");
  36. return;
  37. }
  38. if(args[0] == "random")
  39. {
  40. PrintQuote(myRandomQuote);
  41. }
  42. else if(args[0] == "all")
  43. {
  44. int x = 0;
  45. for(int n = list.Count; n > 0; n--)
  46. {
  47. PrintQuote(list[x]);
  48. x++;
  49. }
  50. }
  51. }
  52. }

Result

Hope its help to all beginners and professional, Learn and enjoy the programming.
Thanks!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.