"Stu Cazzo" <ff***@yahoo.com> wrote in message
news:61**************************@posting.google.c om...
I have the following:
String[] myStringArray;
String myString = "98 99 100";
I want to split up myString and put it into myStringArray.
If I use this:
myStringArray = myString.split(" ");
it will split myString up using the delimiter of 1 space
so that
myStringArray[0] = 98
myStringArray[1] = 99
myStringArray[2] = 100
How do I write my split code so that it will split myString
on 1 or more spaces???
In other words, the following 2 strings should split up the
same so that the 2 spaces between the 88 and 89 are
handled.
"88 89 90"
"88 89 90"
myStringArray[0] = 88
myStringArray[1] = 89
myStringArray[2] = 90
A suitable solution has already been posted - using 'split' with an RE [in
this case, it would probably be "\x20+" using a greedy quantifier to 'gobble
up' all the space characters].
I'd like to offer an alternative solution, one that might be useful to those
using a pre-1.4 J2SDK [besides, I always enjoy writing string
manipulation-related code ;) !]. The code consists of:
* A 'split' method that mimics 1.4's 'String.split' except
for the RE-expansion component
* A 'squeeze' method which reduces all multiple sequential
occurrences of a specifed character
To effect a split on the space character ensuring multiple sequential
occurrences have been removed you would do:
String string = "...";
String[] array = Str.split(Str.squeeze(string, ' '), " ");
Please note this is offered more as a learning exercise than a practical
solution, though it can be the latter in certain cases.
Cheers,
Anthony Borla
// ===================================
import java.util.*;
public class TestStr
{
public static void main(String[] args)
{
// *** Testing 'squeeze'
// String s1 = Str.squeeze("ABC", '|', true);
// String s2 = Str.squeeze("", '|', true);
// String s3 = Str.squeeze("|", '|', true);
// String s4 = Str.squeeze("||||", '|', true);
// String s5 = Str.squeeze("ABC|def", '|', true);
// String s6 = Str.squeeze("ABC||def|", '|', true);
// String s7 = Str.squeeze("ABC||def|||", '|', true);
// String s8 = Str.squeeze("ABC||| |def||||| |||", ' ', true);
// String s9 = Str.squeeze("ABC||| |def||||| |||", '|', true);
// System.out.println(s1);
// System.out.println(s2);
// System.out.println(s3);
// System.out.println(s4);
// System.out.println(s5);
// System.out.println(s6);
// System.out.println(s7);
// System.out.println(s8);
// System.out.println(s9);
// *** Testing 'split'
// String[] s1 = Str.split("asv rrr 345 123 nmj", " ", true);
// String[] s2 = Str.split("asv rrr 345 123 nmj ", " ", true);
// String[] s3 = Str.split("asv rrr 345 123 nmj ", " ", true);
// String[] s4 = Str.split("asvrrr345123nmj", " ", true);
// *** Testing 'squeeze' and 'split' together
String unsqueezed = "1|||2||3|4||5|||6|7||8";
String[] numbers1 = Str.split(unsqueezed, "|", false);
for (int i = 0; i < numbers1.length; ++i)
System.out.println(numbers1[i]);
String numberStr = Str.squeeze(unsqueezed, '|', false);
String[] numbers2 = Str.split(numberStr, "|", false);
for (int i = 0; i < numbers2.length; ++i)
System.out.println(numbers2[i]);
}
}
class Str
{
public static String squeeze(String source, char squeezeChar, boolean
debug)
{
StringBuffer sb = new StringBuffer(source);
String squeezeStr = Character.toString(squeezeChar);
int spos = 0;
while (!((spos = sb.indexOf(squeezeStr, spos)) < 0))
while (++spos < sb.length() && sb.charAt(spos) == squeezeChar)
{
// *** Removable code
if (debug)
sb.setCharAt(spos, '#');
else
sb.deleteCharAt(spos--);
}
return sb.toString();
}
public static String[] split(String source, String pattern, boolean debug)
{
ArrayList list = new ArrayList();
int spos = 0, epos = 0, patternLength = pattern.length();
if (source.indexOf(pattern) < 0)
{
list.add(source);
}
else
{
String item = new String();
do
{
if ((epos = source.indexOf(pattern, spos)) < 0)
{
item = source.substring(spos, source.length());
// *** Removable Code
if (debug)
{
System.out.println("Item = " + item + " length: " +
item.length());
System.out.println("Pattern= " + pattern + " length: " +
pattern.length());
System.out.println("S: " + spos + " E: " + epos + " ==> " +
item);
}
if (item.length() > 0 && item.compareTo(pattern) != 0)
list.add(item);
break;
}
item = source.substring(spos, epos);
// *** Removable Code
if (debug)
{
System.out.println("S: " + spos + " E: " + epos + " ==> " + item);
}
if (item.length() > 0 && item.compareTo(pattern) != 0)
list.add(item);
spos = epos + patternLength;
} while (epos <= spos);
}
// *** Removable Code
if (debug)
{
System.out.println("List Size: " + list.size());
}
return ((String[]) list.toArray(new String[list.size()]));
}
}