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, 2022 in C# by krishna
• 2,820 points
1,501 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, 2022 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, 2022 in C# by krishna
• 2,820 points
1,742 views
0 votes
1 answer

How to iterate over a dictionary?

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

answered Jun 7, 2022 in C# by pranav
• 2,590 points
456 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, 2022 in C# by krishna
• 2,820 points
714 views
0 votes
1 answer

How to practice a language like C#

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

answered Jun 13, 2022 in C# by krishna
• 2,820 points
628 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
911 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,250 points
2,819 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, 2022 in Others by narikkadan
• 63,600 points
803 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, 2022 in Blockchain by Soham
• 9,710 points
813 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, 2022 in C# by jyoti
• 1,240 points
426 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, 2022 in C# by jyoti
• 1,240 points
635 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP