Solved

Splitting String Efficiency

Posted on 2006-07-02
5
261 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
  • 3
  • 2
5 Comments
 
LVL 92

Accepted Solution

by:
objects earned 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 68
@SBGen Method 3 35
backtracking recursion  code 19 53
Oracle SQL syntax check  without executing 6 29
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

929 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now