tags:

views:

198

answers:

8

I want to split string without using split . can anybody solve my problem I am tried but I cannot find the exact logic.

+6  A: 

Since this seems to be a task designed as coding practice, I'll only guide. No code for you, sir, though the logic and the code aren't that far separated.

You will need to loop through each character of the string, and determine whether or not the character is the delimiter (comma or semicolon, for instance). If not, add it to the last element of the array you plan to return. If it is the delimiter, create a new empty string as the array's last element to start feeding your characters into.

Matchu
Sanjeev
OK, then post what you tried, and we'll help you find your error.
dmazzoni
@Sanjeev: I don't get it. Why would you ever try to duplicate the functionality of split unless you're being required to perform homework-like tasks to better yourself?
Matchu
A: 

The logic is: go through the whole string starting from first character and whenever you find a space copy the last part to a new string.. not that hard?

Jack
A: 

You cant split with out using split(). Your only other option is to get the strings char indexes and and get sub strings.

Babiker
Your second sentence contradicts your first -- that's exactly how you split without using `split()`. It's obviously possible, since `String.split()` is written in Java itself
Michael Mrozek
Thanks Michael but you did't get what i wrote. i meant the built split() in function.
Babiker
@Babiker - the problem is that that what you wrote is not what you were trying to say. Trust me ... English has been my first language for > 50 years! (And BTW - what you were apparently trying to say is a tautology.)
Stephen C
A: 

The way to go is to define the function you need first. In this case, it would probably be:

String[] split(String s, String separator)

The return type doesn't have to be an array. It can also be a list:

List<String> split(String s, String separator)

The code would then be roughly as follows:

  1. start at the beginning
  2. find the next occurence of the delimiter
  3. the substring between the end of the previous delimiter and the start of the current delimiter is added to the result
  4. continue with step 2 until you have reached the end of the string

There are many fine points that you need to consider:

  • What happens if the string starts or ends with the delimiter?
  • What if multiple delimiters appear next to each other?
  • What should be the result of splitting the empty string? (1 empty field or 0 fields)
Roland Illig
+1  A: 

You do now that most of the java standard libraries are open source

In this case you can start here

Peter Tillemans
If this is homework looking at the standard library implementation is probably frowned upon
Michael Mrozek
And probably not pertinent to the question, but it can break the "white paper" syndrome, and offers a glimpse in more realistic codebases than you get in Java101.Being able to read and follow code in a non-trivial code base is an underrated skill and VERY important.
Peter Tillemans
The question wasn't tagged homework, so anything goes..
Brendan Long
A: 

You can do it using java standard libraries. Say the delimiter is ':' and String s = "Harry:Potter" int a = s.find(delimiter); and then add s.substring(start, a) to a new String array. Keep doing this till you start < string length Should be enough I guess

A: 

I'm going to assume that this is homework, so I will only give snippets as hints:

Finding indices of all occurrences of a given substring

Here's an example of using indexOf with the fromIndex parameter to find all occurrences of a substring within a larger string:

String text = "012ab567ab0123ab";

// finding all occurrences forward: Method #1
for (int i = text.indexOf("ab"); i != -1; i = text.indexOf("ab", i+1)) {
    System.out.println(i);
} // prints "3", "8", "14"

// finding all occurrences forward: Method #2
for (int i = -1; (i = text.indexOf("ab", i+1)) != -1; ) {
    System.out.println(i);
} // prints "3", "8", "14"

String API links

  • int indexOf(String, int fromIndex)
    • Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. If no such occurrence exists, -1 is returned.

Related questions


Extracting substrings at given indices out of a string

This snippet extracts substring at given indices out of a string and puts them into a List<String>:

String text = "0123456789abcdefghij";

List<String> parts = new ArrayList<String>();
parts.add(text.substring(0, 5));
parts.add(text.substring(3, 7));
parts.add(text.substring(9, 13));
parts.add(text.substring(18, 20));

System.out.println(parts); // prints "[01234, 3456, 9abc, ij]"

String[] partsArray = parts.toArray(new String[0]);

Some key ideas:

  • Effective Java 2nd Edition, Item 25: Prefer lists to arrays
    • Works especially nicely if you don't know how many parts there'll be in advance

String API links

Related questions

polygenelubricants
A: 

This is the right answer

import java.util.StringTokenizer;

public class tt { public static void main(String a[]){ String s = "012ab567ab0123ab";

String delims = "ab ";

StringTokenizer st = new StringTokenizer(s, delims);
        System.out.println("No of Token = " + st.countTokens());

         while (st.hasMoreTokens())
         {
             System.out.println(st.nextToken());
         }

}

}

Sanjeev