I want to split string without using split . can anybody solve my problem I am tried but I cannot find the exact logic.
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.
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?
You cant split with out using split(). Your only other option is to get the strings char indexes and and get sub strings.
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:
- start at the beginning
- find the next occurence of the delimiter
- the substring between the end of the previous delimiter and the start of the current delimiter is added to the result
- 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)
You do now that most of the java standard libraries are open source
In this case you can start here
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
String substring(int beginIndex, int endIndex)
- Returns a new string that is a substring of this string. The substring begins at the specified
beginIndex
and extends to the character at indexendIndex - 1
.
- Returns a new string that is a substring of this string. The substring begins at the specified
Related questions
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());
}
}
}