Solved

Splitting String Efficiency

Posted on 2006-07-02
5
258 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

760 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

21 Experts available now in Live!

Get 1:1 Help Now