0

I am trying to print a string array into muliple lines within a texbox.

string[] mexicanRestaurants = 
{
    "Jose Locos 853 N Glenstone Ave, Springfield, MO 65802 (417) 831-1300",
    "Tortilleria Perches 1601 W Sunshine St, Springfield, MO 65807 (417) 864-8195",
    "Purple Burrito 5360 S Campbell Ave Springfield, MO 65810 (417) 883-5305",
    "Amigos Mexican Restaurant 2118 S Campbell Ave, Springfield, MO 65807 (417) 887-1401",
    "Cantina Laredo 4109 S National Ave, Springfield, MO 65807 (417) 881-7200" 
};

Random rand = new Random();
String result = mexicanRestaurants[rand.Next(mexicanRestaurants.Length)];
txtResults.Text = result;

Currently the text prints into the textbox as:

Jose Locos 853 N Glenstone Ave, Springfield, MO 65802 (417) 831-1300

I am trying to figue out a way so that it will print into the text box like:

Jose Locos

853 N Glenstone Ave, Springfield, MO 65802

(417) 831-1300

Any help is greatly appreciated. Thanks.

2
  • txtResults.Lines = result; :D Commented Apr 29, 2015 at 0:10
  • You might check out some address parsers online, like: usaddress.codeplex.com Commented Apr 29, 2015 at 1:04

1 Answer 1

3

It is not possible to do this because your source data are provided in a human-readable (not machine-readable) format.

If you added a delimiter of some kind, it would be fairly simple. Here is a way to do it using a pipe delimiter.

string[] mexicanRestaurants = 
    {"Jose Locos|853 N Glenstone Ave, Springfield, MO 65802|(417) 831-1300",
     "Tortilleria Perches|1601 W Sunshine St, Springfield, MO 65807|(417) 864-8195",
     --etc--
    };
    Random rand = new Random();
    String result = mexicanRestaurants[rand.Next(mexicanRestaurants.Length)];
    txtResults.Lines = result.Split("|");
Sign up to request clarification or add additional context in comments.

5 Comments

use regex to split the first and last group after splitting by ','
A RegEx might work under certain assumptions, but probably would not be reliable, e.g. "Bar 71 North sunshine Street" is the restaurant "Bar 71" on "North Sunshine Street" or is is just called "Bar" on "71 North Sunshine Street" ? Don't even get me started on apartment numbers, PO Boxes, etc....
True, but according to the input, all lines has numbers and is displayed on second line. Anyway, an detailed example will clarify this
@vasilenicusor That isn't quite true...the third line does not have a comma before "Springfield"...
Vas, if you can accomplish it via a RegEx, feel free to add another answer. I'd love to see the expression you have to write!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.