If I have an ArrayList that has lines of data that could look like:
bob, jones, 123-333-1111
james, lee, 234-333-2222
How do I delete the extra whitespace and get the same data back? I thought you could maybe spit the string by "," and then use trim(), but I didn't know what the syntax of that would be or how to implement that, assuming that is an ok way to do it because I'd want to put each field in an array. So in this case have a [2][3] array, and then put it back in the ArrayList after removing the whitespace. But that seems like a funny way to do it, and not scaleable if my list changed, like having an email on the end. Any thoughts? Thanks.
Edit:
Dumber question, so I'm still not sure how I can process the data, because I can't do this right:
for (String s : myList) {
String st[] = s.split(",\\s*");
}
since st[] will lose scope after the foreach loop. And if I declare String st[] beforehand, I wouldn't know how big to create my array right? Thanks.
You could just scan through the entire string and build a new string, skipping any whitespace that occurs after a comma. This would be more efficient than splitting and rejoining. Something like this should work:
String str = /* your original string from the array */;
StringBuilder sb = new StringBuilder();
boolean skip = true;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (skip && Character.isWhitespace(ch))
continue;
sb.append(ch);
if (ch == ',')
skip = true;
else
skip = false;
}
String result = sb.toString();
If you use a regex for you split, you can specify, a comma followed by optional whitespace (which includes spaces and tabs just in case).
String[] fields = mystring.split(",\\s*");
Depending on whether you want to parse each line separately or not you may first want to create an array split on a line return
String[] lines = mystring.split("\\n");
Just split() on each line with the delimiter set as ',' to get an array of Strings with the extra whitespace, and then use the trim() method on the elements of the String array, perhaps as they are being used or in advance. Remember that the trim() method gives you back a new string object (a String object is immutable).
If I understood your problem, here is a solution:
ArrayList<String> tmp = new ArrayList<String>();
tmp.add("bob, jones, 123-333-1111");
tmp.add(" james, lee, 234-333-2222");
ArrayList<String> fixedStrings = new ArrayList<String>();
for (String i : tmp) {
System.out.println(i);
String[] data = i.split(",");
String result = "";
for (int j = 0; j < data.length - 1; ++j) {
result += data[j].trim() + ", ";
}
result += data[data.length - 1].trim();
fixedStrings.add(result);
}
System.out.println(fixedStrings.get(0));
System.out.println(fixedStrings.get(1));
I guess it could be fixed not to create a second ArrayLis. But it's scalable, so if you get lines in the future like: "bob, jones , bobjones#gmail.com , 123-333-1111 " it will still work.
I've had a lot of success using this library.
Could be a bit more elegant, but it works...
ArrayList<String> strings = new ArrayList<String>();
strings.add("bob, jones, 123-333-1111");
strings.add("james, lee, 234-333-2222");
for(int i = 0; i < strings.size(); i++) {
StringBuilder builder = new StringBuilder();
for(String str: strings.get(i).split(",\\s*")) {
builder.append(str).append(" ");
}
strings.set(i, builder.toString().trim());
}
System.out.println("strings = " + strings);
I would look into:
http://download.oracle.com/docs/cd/E17476_01/javase/1.4.2/docs/api/java/lang/String.html#split(java.lang.String)
or
http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/api/java/util/Scanner.html
you can use Sting.split() method in java or u can use split() method from google guava library's Splitter class as shown below
static final Splitter MY_SPLITTER = Splitter.on(',')
.trimResults()
.omitEmptyStrings();
Related
I have a String like this : String attributes = " foo boo, faa baa, fii bii," I want to get a result like this :
String[] result = {"foo boo", "faa baa", "fii bii"};
So my issue is how should to make split and trim in one shot i already split:
String[] result = attributes.split(",");
But the spaces still in the result :
String[] result = {" foo boo", " faa baa", " fii bii"};
^ ^ ^
I know that we can make a loop and make trim for every one but I want to makes it in shot.
Use regular expression \s*,\s* for splitting.
String result[] = attributes.split("\\s*,\\s*");
For Initial and Trailing Whitespaces
The previous solution still leaves initial and trailing white-spaces. So if we're expecting any of them, then we can use the following solution to remove the same:
String result[] = attributes.trim().split("\\s*,\\s*");
Using java 8 you can do it like this in one line
String[] result = Arrays.stream(attributes.split(",")).map(String::trim).toArray(String[]::new);
If there is no text between the commas, the following expression will not create empty elements:
String result[] = attributes.trim().split("\\s*,+\\s*,*\\s*");
You can do it with Google Guava library this way :
List<String> results = Splitter.on(",").trimResults().splitToList(attributes);
which I find quite elegant as the code is very explicit in what it does when you read it.
ApaceCommons StringUtils.stripAll function can be used to trim individual elements of an array. It leaves the null as null if some of your array elements are null.
Here,
String[] array = StringUtils.stripAll(attributes.split(","));
create your own custom function
private static String[] split_and_trim_in_one_shot(String string){
String[] result = string.split(",");
int array_length = result.length;
for(int i =0; i < array_length ; i++){
result[i]=result[i].trim();
}
return result;
Overload with a consideration for custom delimiter
private static String[] split_and_trim_in_one_shot(String string, String delimiter){
String[] result = string.split(delimiter);
int array_length = result.length;
for(int i =0; i < array_length ; i++){
result[i]=result[i].trim();
}
return result;
with streams
public static List<String> split(String str){
return Stream.of(str.split(","))
.map(String::trim)
.map (elem -> new String(elem))//optional
.collect(Collectors.toList());
What about spliting with comma and space:
String result[] = attributes.split(",\\s");
// given input
String attributes = " foo boo, faa baa, fii bii,";
// desired output
String[] result = {"foo boo", "faa baa", "fii bii"};
This should work:
String[] s = attributes.trim().split("[,]");
As answered by #Raman Sahasi:
before you split your string, you can trim the trailing and leading spaces. I've used the delimiter , as it was your only delimiter in your string
String result[] = attributes.trim().split("\\s*,[,\\s]*");
previously posted here: https://blog.oio.de/2012/08/23/split-comma-separated-strings-in-java/
Best way is:
value.split(",").map(function(x) {return x.trim()});
This might have been asked before, but I spent some time looking, so here's what I have.
I have a string containing an array:
'["thing1","thing2"]'
I would like to convert it into an actual array:
["thing1","thing2"]
How would I do this?
You could create a loop that runs through the whole string, checking for indexes of quotes, then deleting them, along with the word. I'll provide an example:
ArrayList<String> list = new ArrayList<String>();
while(theString.indexOf("\"") != -1){
theString = theString.substring(theString.indexOf("\"")+1, theString.length());
list.add(theString.substring(0, theString.indexOf("\"")));
theString = theString.substring(theString.indexOf("\"")+1, theString.length());
}
I would be worried about an out of bounds error from looking past the last quote in the String, but since you're using a String version of an array, there should always be that "]" at the end. But this creates only an ArrayList. If you want to convert the ArrayList to a normal array, you could do this afterwards:
String[] array = new String[list.size()];
for(int c = 0; c < list.size(); c++){
array[c] = list.get(c);
}
You can do it using replace and split methods of String class, e.g.:
String s = "[\"thing1\",\"thing2\"]";
String[] split = s.replace("[", "").replace("]", "").split(",");
System.out.println(Arrays.toString(split));
I have a string that looks something like:
10.20.30.444,10.20.30.555,10.20.600.300
Now, I want to search for all the commas in the string and would like to add [2000] before the comma as well as in the last position. Now for the above string it would be like:
10.20.30.444[2000],10.20.30.444[2000],10.20.600.300[2000]
How can I achieve this in Java?
I would really appreciate a helping hand here.
Thanks a lot in advance.
use String.indexOf(",") in a loop then to insert 2000
for example if index returns 4
x = x.substring(0, 4) + "2000" + x.substring(4, x.length());
and do this in a loop
str = str.replace(",","[2000],");
replace all commas with [2000], and append [2000] at the end that's all
String sometext ="10.20.30.444,10.20.30.555,10.20.600.300";
sometext=sometext.replaceAll(",", "[2000],")+"[2000]";
I would use StringBuilder for this. String replace() creates a new String object each time you call it. StringBuilder does not, but I use two StringBuilder objects below.
StringBuilder sb = new StringBuilder("10.20.30.444,10.20.30.555,10.20.600.300");
System.out.println(sb);
StringBuilder sbnew = new StringBuilder();
char c;
for (int i=0; i < sb.length(); i++) {
c = sb.charAt(i);
if (c == ',') {
sbnew.append("[2000]");
}
sbnew.append(c);
}
sbnew.append("[2000]");
System.out.println(sbnew);
I have the following string:
String n = "(.........)(......)(.......)(......) etc"
I want to write a method which will fill a List<String> with every substring of n which is between ( and ) . Thank you in advance!
It can be done in one line:
String[] parts = input.replaceAll("(^.*\\()|(\\).*$)", "").split("\\)\\(");
The call to replaceAll() strips off the leasing and trailing brackets (plus any other junk characters before/after those first/last brackets), then you just split() on bracket pairs.
I'm not very familiar with the String methods, so I'm sure there's a way that it could be done without having to code it yourself, and just using some fancy method, but here you go:
Tested, works 100% perfect :)
String string = "(stack)(over)(flow)";
ArrayList<String> subStrings = new ArrayList<String>();
for(int c = 0; c < string.length(); c++) {
if(string.charAt(c) == '(') {
c++;
String newString = "";
for(;c < string.length() && string.charAt(c) != ')'; c++) {
newString += string.charAt(c);
}
subStrings.add(newString);
}
}
If the (...) pairs aren't nested, you can use a regular expression in Java. Take a look at the java.util.regex.Pattern class.
I made this regex version, but it's kind of lengthy. I'm sure it could be improved upon. (note: "n" is your input string)
Pattern p = Pattern.compile("\\((.*?)\\)");
Matcher matcher = p.matcher(n);
List<String> list = new ArrayList<String>();
while (matcher.find())
{
list.add(matcher.group(1)); // 1 == stuff between the ()'s
}
This should work:
String in = "(bla)(die)(foo)";
in = in .substring(1,in.length()-1);
String[] out = in .split(Pattern.quote(")("));
Dear stackoverflow members,
I have a small problem,
I want to replace some characters in my string with other in java, and my code to do it is as follow,
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1.removeAllItems();
String str = new GenericResource_JerseyClient().getlist();
String [] temp = null;
temp = str.split(",");
temp[1]=temp[1].replaceAll("'", "");
temp[1]=temp[1].replaceAll(" ", "");
temp[1]=temp[1].replace("[", "");
temp[1]=temp[1].replace("]", "");
for(int i =0; i < temp.length ; i++)
{
jComboBox1.addItem(temp[i]);
} // TODO add your handling code here: // TODO add your handling code here:
// TODO add your handling code here
}
As it can be seen from the above code i replace "'","[","]",and empty space with nothing.
As it can be also seen from the code that i split the string into two. In the part of string after , the code works well but the part of string before , the code doesn't seem to work properly.
i have also attached a copy of the dropdown list output in the client side.
Any help would be very much appreciated on how to remove [ and ' from the string.
Cheers.
Perform all your replacements BEFORE splitting the string. This is better than executing the same code in a loop.
For example:
String cleanString = str.replace("'", "").replace(" ", "").replace("[", "").replace("]", "");
String[] temp = cleanString.split(",");
for(int i = 0; i < temp.length ; i++) {
jComboBox1.addItem(temp[i]);
}
You're only performing replacements on temp[1] - whereas the problem appears to be in the first item shown in the dropdown, which is presumably the data from temp[0].
I suspect you should just extract the removal code into a separate method, and call it in your loop:
for(int i =0; i < temp.length ; i++)
{
jComboBox1.addItem(removeUnwantedCharacters(temp[i]));
}
Additionally, I would strongly recommend that you use replace rather than replaceAll for any case where you don't explicitly want regular expression pattern matching. It can be very confusing to have code such as:
foo = foo.replaceAll(".", "");
which looks like it's removing dots, but will actually remove all characters, as the "." is treated as a regular expression...
well, you performing replace only in item with index 1 (the second one). But then adding to the combo all of them (two, actually). Try:
for(int i =0; i < temp.length ; i++){
temp[i]=temp[i].replaceAll("'", "");
temp[i]=temp[i].replaceAll(" ", "");
temp[i]=temp[i].replace("[", "");
temp[i]=temp[i].replace("]", "");
}
You are only doing the replacing on the temp[1] which is the second part of the string. You need to do in temp[0] as well. It is better to create a function that takes a string and does the replacements and call it on both temp[0] and temp[1]. You can also look at using a regular expression to replace all characters at once instead of doing it one at a time.
String [] temp = String.split(",")
for (int i = 0;i<temp.length;i++) {
temp[i] = replaceSpecialChars(temp[i]);
}
public String replaceSpecialChars(String input) {
// add your replacement logic here
return input
}
Common problem.
I think this code meets your requirements.