• We just launched and are currently in beta. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

String Array in Random Quote using C#

Mh114

Data Pipeline Engineer
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
162
Likes
120
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
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. public

    class

    GenericSample
  4. {
  5. public

    static void Main(

    string[

    ]

    args)
  6. {
  7. List<

    string>

    list

    =

    new

    List<

    string>

    (

    )

    ;
  8. list

    .

    Add(

    "First string"

    )

    ;
  9. list

    .

    Add(

    "Second string"

    )

    ;
  10. list

    .

    Add(

    "Third string"

    )

    ;
  11. list

    .

    Add(

    "Fourth string"

    )

    ;
  12. for

    (

    int n =

    0

    ;

    n <

    list

    .

    Count

    ;

    n++

    )
  13. {
  14. Console.

    WriteLine(

    list

    [

    n]

    )

    ;
  15. }
  16. }
  17. }

For creating a random quote, here is the syntax.
  1. using System

    ;
  2. using System

    .

    Collections.

    Generic;

  3. public

    class

    RandomQuotes
  4. {
  5. static void CreateQuotes(

    List<

    string>

    quotes)
  6. {
  7. quotes.

    Add(

    "I Hate Drugs!!!@Weed"

    )

    ;
  8. quotes.

    Add(

    "Long if its hot, short if its cold...@Popstickle"

    )

    ;
  9. quotes.

    Add(

    "John 3:16@Verse"

    )

    ;
  10. quotes.

    Add(

    "Good Bye!!!@Welcome"

    )

    ;
  11. quotes.

    Add(

    "HAHAHA :P !!!@Bleee"

    )

    ;
  12. }

  13. static void PrintQuote(

    string quote)
  14. {
  15. string[

    ]

    substring =

    quote.

    Split

    (

    '@'

    )

    ;
  16. Console.

    WriteLine(

    substring[

    0

    ]

    )

    ;
  17. Console.

    WriteLine(

    "-"

    +

    substring[

    1

    ]

    )

    ;
  18. }

  19. static string GetRandomQuote(

    List<

    string>

    quotes)
  20. {
  21. int n =

    new

    Random(

    )

    .

    Next

    (

    0

    ,

    quotes.

    Count

    )

    ;
  22. return

    (

    quotes[

    n]

    )

    ;
  23. }

  24. public

    static void Main(

    string[

    ]

    args)
  25. {
  26. List<

    string>

    list

    =

    new

    List<

    string>

    (

    )

    ;
  27. CreateQuotes(

    list

    )

    ;
  28. string myRandomQuote =

    GetRandomQuote(

    list

    )

    ;
  29. if

    (

    args.

    Length ==

    0

    )
  30. {
  31. Console.

    WriteLine(

    "Error Argument:\n

    * All\n

    * Random\n

    * Search"

    )

    ;
  32. return

    ;
  33. }
  34. if

    (

    args[

    0

    ]

    ==

    "random"

    )
  35. {
  36. PrintQuote(

    myRandomQuote)

    ;
  37. }
  38. else

    if

    (

    args[

    0

    ]

    ==

    "all"

    )
  39. {
  40. int x =

    0

    ;
  41. for

    (

    int n =

    list

    .

    Count

    ;

    n >

    0

    ;

    n--

    )
  42. {
  43. PrintQuote(

    list

    [

    x]

    )

    ;
  44. x++;
  45. }
  46. }
  47. }
  48. }

images_1_0.jpg


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


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

342,733

342,741

Top