How to get substring from string containing newlines

0 votes

If the response length is more than provided, I'm trying to obtain a substring from it and loop through it. My code is as follows:

string response = //response from API
if (response.Length > 4096)
{
    for (int i = 0; i < response.Length; i += 4096)
    {
        string rplymsg = response.Substring(i, 4095);
        //other code using rplymsg 
    }
}
else
{
    //other code using response 
}

I experimented with String. In debugging, the Substring method returns a substring of the provided length, but the real substring length is longer. (I double-checked this by pasting a substring into notepad++.)

I believe the issue is caused by the response string's substring rplymsg, which contains new lines n. Is there a better way to count the number of newlines in a string so that I may extract substrings based on that length?

Jun 11 in C# by krishna
• 2,540 points
19 views

1 answer to this question.

0 votes

If you want to maintain the newline characters if a string is less than 4096 characters, I believe you should divide the string on that character rather than just String. Substring(...) consider using String. Split(...) also processes the array of strings it returns. e.g.:

    string response = "abcd\nefgh\nijklmnop\nrst\n";
    var maxLength = 5;
    var delimiter = new char[]{'\n'};
    var strings = response.Split(delimiter, StringSplitOptions.None); 
      

    foreach(var s in strings)
    {
        var delimited = s + '\n';//add newline as Split removes them from the result
        if(delimited.Length>maxLength)
        {
            var sub = delimited;
            while(sub.Length>maxLength)
            {
               var p = sub.Substring(0, maxLength-1);
               Console.WriteLine($"partial :{p}");
               sub = sub.Remove(0,maxLength-1);
            }
            Console.WriteLine($"end :{sub}");
        }
        else
        {
            Console.WriteLine($"original :{delimited}");
        }
    }

The code will need some idea to you. This is by no means perfect.

answered Jun 14 by jyoti
• 1,240 points

Related Questions In C#

0 votes
1 answer

How to initialize a list of strings (List<string>) with many string values

Simply remove the () at the end. List<string> ...READ MORE

answered Jun 13 in C# by krishna
• 2,540 points
21 views
0 votes
1 answer

How to iterate over a dictionary?

I have seen this being used. In ...READ MORE

answered Jun 7 in C# by pranav
• 2,580 points
23 views
0 votes
1 answer

How do I turn a C# object into a JSON string in .NET

Newtonsoft.json can be used to accomplish this. ...READ MORE

answered Jun 13 in C# by krishna
• 2,540 points
25 views
0 votes
1 answer

How to practice a language like C#

I believe you should begin with personal ...READ MORE

answered Jun 13 in C# by krishna
• 2,540 points
16 views
0 votes
1 answer

Authenticate on an ASP.Net Forms Authorization website from a console app

Essentially, we need to record a regular ...READ MORE

answered Sep 20, 2018 in IoT (Internet of Things) by Annie97
• 2,160 points
263 views
0 votes
1 answer

Validating String against USPS state abbreviations

This is how I'd do it: private static ...READ MORE

answered Nov 10, 2018 in Others by DataKing99
• 8,240 points
1,267 views
0 votes
1 answer

Writing SEO friendly url gets infinite loop asp.net

you should know that regex avoid cases ...READ MORE

answered Feb 11 in Others by narikkadan
• 11,280 points
203 views
0 votes
0 answers

How to get Bitcoin value for corresponding USD value in ASP.NET C#?

I want to get the Bitcoin value ...READ MORE

Mar 1 in Blockchain by Soham
• 8,740 points
137 views
0 votes
1 answer

How to enumerate an enum

foreach (Suit suit in (Suit[]) Enum.GetValues(typeof(Suit))) { } Note that ...READ MORE

answered Jun 14 in C# by jyoti
• 1,240 points
16 views
0 votes
1 answer

How to find the extension of a file in C#

Go to here https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getextension?redirectedfrom=MSDN&view=net-6.0#System_IO_Path_GetExtension_System_String_ string myFilePath = @"C:\MyFile.txt"; string ext ...READ MORE

answered Jun 21 in C# by jyoti
• 1,240 points
26 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP