Solved

Splitting String Efficiency

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB Script to add site to Java Exception List 4 78
split string containing \r\n in Java 46 44
What's wrong with this code? 4 23
Adding multiple JVM environments to RedHat 6 7 21
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

777 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