[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Splitting String Efficiency

Posted on 2006-07-02
5
Medium Priority
?
267 Views
Last Modified: 2011-09-20
Hi,

The scenario is that I want to split the string into 10 chars length each. do you think that the below codes are efficient as String is immutable? If not, can you let me know how to improve below codes?

            String a = "12345667788";
            
            StringBuffer sb = new StringBuffer();
            sb.append(a);
            
            int part = 1;
            while (sb.length() > 0) {
                  System.out.println("Entering " + part);
                  
                  String temp = sb.substring(0, sb.length() >= 10 ? 10 : sb.length());
                  sb.delete(0, sb.length() >= 10 ? 10 : sb.length());                  
                  
                  System.out.println("temp = " + temp);
                  part++;
            }
            
            sb = null;

Thanks
David
0
Comment
Question by:suprapto45
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 17028787
you can just use substring directly on the original string

int pos = 0;
while (pos<a.length()) {
   String next = a.substring(pos, Math.min(pos+10, a.length()));
   pos += 10;
}
0
 
LVL 16

Author Comment

by:suprapto45
ID: 17028796
Thanks,

But I thought that String is immutable and that is the reason why I used StringBuffer. Or am I missing some concepts here? ;)

Thanks
David
0
 
LVL 16

Author Comment

by:suprapto45
ID: 17028801
Oppsss...

I think that I know what you mean.

Do you mean this? If so, yes it is much cleaner and better I think.

            String a = "12345667788";
            
            StringBuffer sb = new StringBuffer();
            sb.append(a);
            
            int part = 1;
            int pos = 0;
            
            while (pos < sb.length()) {
                  System.out.println("Entering " + part);
                  String temp = sb.substring(pos, Math.min(pos+10, sb.length()));                  
                  System.out.println("temp = " + temp);
                  
                  pos += 10;
                  part++;
            }
            
            sb = null;

Thanks
David
0
 
LVL 92

Expert Comment

by:objects
ID: 17028810
substring() returns a new string, it doesn't modify the original one
0
 
LVL 16

Author Comment

by:suprapto45
ID: 17028827
Thanks mate
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question