Response
Response

Reputation: 61

input textbox into array

I want to create a textbox for my string input, and then separate it into an array if the input has ".". For example:

The answer lies in machine translation. The best machine translation technology cannot always provide translations tailored to a site or users like a human. Simply copy and paste a code snippet anywhere.

In that case, that input will consist of 3 arrays.

Please take a look at the following code from Microsoft. I want to change the hard code from the input using the textbox. Then pass each array to be translated.

class TranslateArraySample
{
    public static async Task Run(string authToken)
    {
        var from = "en";
        var to = "es";
       ** var translateArraySourceTexts = new []
        {
            "The answer lies in machine translation.",
            "the best machine translation technology cannot always provide translations tailored to a site or users like a human ",
            "Simply copy and paste a code snippet anywhere "
        };
        var uri = "https://api.microsofttranslator.com/v2/Http.svc/TranslateArray";
        var body = "<TranslateArrayRequest>" +
                       "<AppId />" +
                       "<From>{0}</From>" +
                       "<Options>" +
                       " <Category xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" +
                           "<ContentType xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\">{1}</ContentType>" +
                           "<ReservedFlags xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" +
                           "<State xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" +
                           "<Uri xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" +
                           "<User xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" +
                       "</Options>" +
                       "<Texts>" +
                           "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">{2}</string>" +
                           "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">{3}</string>" +
                           "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">{4}</string>" +
                       "</Texts>" +
                       "<To>{5}</To>" +
                   "</TranslateArrayRequest>";
        string requestBody = string.Format(body, from, "text/plain", translateArraySourceTexts[0], translateArraySourceTexts[1], translateArraySourceTexts[2], to);

        using (var client = new HttpClient())
        using (var request = new HttpRequestMessage())
        {
            request.Method = HttpMethod.Post;
            request.RequestUri = new Uri(uri);
            request.Content = new StringContent(requestBody, Encoding.UTF8, "text/xml");
            request.Headers.Add("Authorization", authToken);
            var response = await client.SendAsync(request);
            var responseBody = await response.Content.ReadAsStringAsync();
            switch (response.StatusCode)
            {
                case HttpStatusCode.OK:
                    Console.WriteLine("Request status is OK. Result of translate array method is:");
                    var doc = XDocument.Parse(responseBody);
                    var ns = XNamespace.Get("http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2");
                    var sourceTextCounter = 0;
                    foreach (XElement xe in doc.Descendants(ns + "TranslateArrayResponse"))
                    {
                        foreach (var node in xe.Elements(ns + "TranslatedText"))
                        {
                        **    Console.WriteLine("\n\nSource text: {0}\nTranslated Text: {1}", translateArraySourceTexts[sourceTextCounter], node.Value);
                        }
                        sourceTextCounter++;
                    }
                    break;
                default:
                    Console.WriteLine("Request status code is: {0}.", response.StatusCode);
                    Console.WriteLine("Request error message: {0}.", responseBody);
                    break;
            }
        }
    }
}

Upvotes: 0

Views: 66

Answers (3)

JB Cooper
JB Cooper

Reputation: 109

You need String.Split(charArray, stringSplitoptions) to get only three strings in your resulting array.

in your example

   string translatableString = "The answer lies in machine translation. The best 
    machine translation technology cannot always provide translations tailored to 
    a site or users like a human. Simply copy and paste a code snippet anywhere.";

    string[] arr = translatableString.Split(new char[] { '.' }, 
    StringSplitOptions.RemoveEmptyEntries);

you would get a array of 4 strings with translatableString.Split('.') because one will be empty. This is why I provided the overloaded method.

Upvotes: 0

sTrenat
sTrenat

Reputation: 1049

use (StringObject).Split("<separator>") sample code:

var translateArraySourceTexts = new[]
            {
                "The answer lies in machine translation.",
                "the best machine translation technology cannot always provide translations tailored to a site or users like a human ",
                "Simply copy and paste a code snippet anywhere "
            };
    var array = string.Join(",",translateArraySourceTexts).Split('.');

Upvotes: 1

Derek
Derek

Reputation: 8793

Here is some example code. Replace string s, with your .Text from your textbox.

string s = @"Changing your development practice to introduce an automated testing strategy can revolutionise your deployments. If you approach the software release date with a sense of dread, this technique is for you.

Implementing a test-driven development strategy using tSQLt leads to robust, modular code that becomes a pleasure to work with. As more tests are created, trust builds that releases will provide beneficial new functionality with no negative side-effects.";

var translateArraySourceTexts = s.Split( Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries ).ToArray();

Upvotes: 0

Related Questions