What about substring(0,10) or substring(0,11) depending on whether index 10 should inclusive or not? You'd have to check for length() >= index though.
An alternative would be org.apache.commons.lang.StringUtils.substring("your string", 0, 10);
What about substring(0,10) or substring(0,11) depending on whether index 10 should inclusive or not? You'd have to check for length() >= index though.
An alternative would be org.apache.commons.lang.StringUtils.substring("your string", 0, 10);
String s ="123456789abcdefgh";
String sub = s.substring(0, 10);
String remainder = s.substring(10);
How can I split a String without using the Split method?
java - Split string by an index value (not middle of the word) - Stack Overflow
Using charAt(index) method to split a String into an array.
Index out of bounds
Videos
I've written a quick and dirty benchmark test for this. It compares 7 different methods, some of which require specific knowledge of the data being split.
For basic general purpose splitting, Guava Splitter is 3.5x faster than String#split() and I'd recommend using that. Stringtokenizer is slightly faster than that and splitting yourself with indexOf is twice as fast as again.
For the code and more info see https://web.archive.org/web/20210613074234/http://demeranville.com/battle-of-the-tokenizers-delimited-text-parser-performance (original link is dead and corresponding site does not appear to exist anymore)
As @Tom writes, an indexOf type approach is faster than String.split(), since the latter deals with regular expressions and has a lot of extra overhead for them.
However, one algorithm change that might give you a super speedup. Assuming that this Comparator is going to be used to sort your ~100,000 Strings, do not write the Comparator<String>. Because, in the course of your sort, the same String will likely be compared multiple times, so you will split it multiple times, etc...
Split all the Strings once into String[]s, and have a Comparator<String[]> sort the String[]. Then, at the end, you can combine them all together.
Alternatively, you could also use a Map to cache the String -> String[] or vice versa. e.g. (sketchy) Also note, you are trading memory for speed, hope you have lotsa RAM
HashMap<String, String[]> cache = new HashMap();
int compare(String s1, String s2) {
String[] cached1 = cache.get(s1);
if (cached1 == null) {
cached1 = mySuperSplitter(s1):
cache.put(s1, cached1);
}
String[] cached2 = cache.get(s2);
if (cached2 == null) {
cached2 = mySuperSplitter(s2):
cache.put(s2, cached2);
}
return compareAsArrays(cached1, cached2); // real comparison done here
}
Hi,
I was wondering how I could take a string and split it into individual words and put those words back into a String array.
For example, the input "the quick brown fox jumped over the lazy dog" will be translated into the string array: {"the", "quick", "brown", "fox", "jumped", over", "the", "lazy", "dog"}
I can't use the split method for strings so I'm a bit stuck. I was prompted to declare a new string array with more entries than I will actually need, storing new words as I encounter space characters in this new array. Then, at the end, create a new String array with just enough entries, and copy over the non-null elements of the first array over.
I'm not sure how to implement that. Could I get some help on writing this method? Thanks in advance!
This is a simple solution:
import java.util.*;
public class Main
{
public static String[] splitBy(String text, int index)
{
int charIn = 0;
String[] defaultArray = new String[2];
for(charIn = index; charIn < text.length(); charIn++)
{
if(text.charAt(charIn) == ' ')
{
defaultArray[0] = text.substring(0, charIn);
defaultArray[1] = text.substring(charIn, text.length() - 1);
return defaultArray;
}
}
return defaultArray;
}
public static void main(String[] args)
{
String text = "This is a nice text"; // Your text
int index = 10; // Index to split
System.out.println(splitBy(text, index)[0]); // Print first part of the splitted
System.out.println(splitBy(text, index)[1]); // Print part 2
}
}
Here is one way. No explicit loops involved.
- find midpoint of string.
- find first space after mid point.
- find first space before mid point.
- select split position based on distance from midpoint.
int mid = str.length()/2;
int indexBeyondMid = str.indexOf(' ',mid);
int indexBeforeMid = str.substring(0,mid).lastIndexOf(' ');
int splitPoint = mid - indexBeforeMid < indexBeyondMid - mid
? indexBeforeMid
: indexBeyondMid;
String firstHalf = str.substring(0,splitPoint);
String secondHalf = str.substring(splitPoint+1); // ignores leading space.
System.out.println(firstHalf);
System.out.println(secondHalf);
The above will split as evenly as possible. If you want to just split on the first space after the mid point, then just split on indexBeyondMid and forget the rest.
If you want to split a line as optimally as possible to a specified linewidth, you can do it like this.
int lineWidth = 19;
while (!str.isBlank()) {
lineWidth = lineWidth > str.length() ? str.length() : lineWidth;
int indexBeyondMid = str.indexOf(' ',lineWidth);
int indexBeforeMid = str.substring(0,lineWidth).lastIndexOf(' ');
int splitPoint = lineWidth - indexBeforeMid < indexBeyondMid - lineWidth
? indexBeforeMid
: indexBeyondMid;
if (splitPoint < 0) {
System.out.println(str);
break;
}
System.out.println(str.substring(0, splitPoint));
str = str.substring(splitPoint+1);
}
prints
Hey I am string that
will be split but
remember i will not
be cut in the middle
of the word