Avatar of tricks801
tricks801 asked on

Email Client/Server : Splitting up A String

The server stores the message as a string. When the client selects an email to read, i want to display the message but only 50 characters per line. So, a few questions.
1. Should the server send 50 characters at a time, and then the client can just readLine and print. If so, whats the best way for the server to split the string and send.
2. Should the server send the whole string, and then client can split it up and print. I dont see a real difference except that in a real world application you would want the client doing more of the work.
3. Is there a better way?

Thanks
Java

Avatar of undefined
Last Comment
Mick Barry

8/22/2022 - Mon
CEHJ

The number of characters per line belongs to the view of a data model. The data model in this case is the message string. Depending on what is inspecting this message, you can change the view to display as many characters per line you wish. Typically this is done in the real world by an email client and is implemented by line wrap in the message display area. The data model remains constant
ASKER
tricks801

right...but im using the console, and would  not like to scroll right to read the long string.  
CEHJ

So the console view could be something like

a. Put the message into a StringBuffer
b. Insert a newline character '\n' every 50 characters
c. Turn it back into a String and print it

or

a. Iterate the char array (char[] messageChars = message.toCharArray())
b. System.out.print(......the message char or if index % 50 == 0, a newline character)
Your help has saved me hundreds of hours of internet surfing.
fblack61
CEHJ

Personally i prefer the second one:


char[] messageChars = message.toCharArray();
for(int i = 0;i < messageChars;i++) {
      if (i % 50 == 0 && messageChars[i] != '\n') {
            System.out.print('\n');
      }
      else {
            System.out.print(messageChars[i]);      
      }
}
CEHJ

But don't leave that literal 50 in the real thing of course
ASKER
tricks801

so this should happen client side and the server is still going to send one long string? Is this the message in message.toCharArray()?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
CEHJ

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
GrandSchtroumpf

with a declaration and initialization for my linechars variable it would be even better:
int linechars = 0; // before the for loop;
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CEHJ

8-)
GrandSchtroumpf

:°)
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Mick Barry