I pull string from textarea by .gettext() and it have a blankline.
test1111
test222
I need to split this String and keep into array, then array[0]=test1111
array[1]=test222
How can I do?
Something like this should work:
String[] lines = text.split("\\s*\\n+\\s*");
Or better if you're using Java 8 (per Pshemo), use "\\R+"
This will skip over multiple blank lines, or lines filled with white space, and should trim leading and ending whitespace as well.
String[] lines = jtextFieldName_you_used.getText().split("\\n");
It is to store textarea elements to array.Hope you find this helpful.
Try this.
String str = abc.getText();
for (String retval: str.split(" ")){ System.out.println(retval); }
If i understood your task right:
final String[] lines = data.split("\\n");
final String results[] = new String[lines.length];
int offset = 0;
for (String line : lines) {
results[offset] = line.split("\\s")[1];
offset++;
}
results:
test1111
blankline
test222
p.s: without data checks
Related
I have a String(assume str) received from some DB query.
str = "+Aa+Bk+Bo+Ac+Lc";
But if copied the same string to intelliJ, It shows the invisible chars in str
I have to split this String (i.e.str) to String[] and then to List.
And getting this[ZWSP] in splatted Array and in converted List as well.
Also tried few/following techniques to trim and remove this, but did not worked.
String str = "+Aa+Bk+Bo+Ac+Lc";
String[] strArr = str.split("\\+");
List<String> splitStrList = Arrays.stream(str.split("\\+"))
.map(String::trim)
.collect(Collectors.toList());
---Approach 2
String[] array2 = Arrays.stream(strArr).map(String::trim).toArray(String[]::new);
String[] trimmedArray = new String[array2.length];
for (int i = 0; i < array2.length; i++) {
trimmedArray[i] = array2[i].trim();
}
List<String> trimmedArrayList = Arrays.asList(trimmedArray);
Also few other approach, but while copying the output to intelliJ IDE seeing those [ZWSP] special chars.
That is creating issue in further processing.
How Can be these spcl chars i.e [ZWSP] removed to get List/Array like
[, Aa, Bk, Bo, Ac, Lc]
Will Appreciate all suggestions/solutions to this problem.
That character it's called zero-width space as #Rogue mentions. You could use unicode character to remove it:
str.replace("\u200B", "");
Or you could split the string like:
str.split("\\+\u200B");
And then process the array as you need.
See:
https://www.fileformat.info/info/unicode/char/200b/index.htm
I have an input stream which has fields separated by tab(\t)
which looks like this
String str = " acc123\tdpId123\t2011-01-01\t2022-01-01\thello#xyz.com\tIN\t1233\tSOMETHING ";
which works fine when I do str = str.trim(); and
strArray = str.split("\t", -1);
strArray=["acc123","dpId123","2011-01-01","2022-01-01","hello#xyz.com","IN","1233","SOMETHING"] will give size as 8
But last field in the input record is not mandatory and can be skipped.
So the input can look like this too.
String str1 = "acc123\tdpId123\t2011-01-01\t2022-01-01\thello#xyz.com\tIN\t1233\t";
but in this case last field should be empty but when I use this string after trim and split my size is 7
str1 = str1.trim();
strArray = str1.split("\t", -1);
strArray=["acc123","dpId123","2011-01-01","2022-01-01","hello#xyz.com","IN","1233"]will give size as 7
But I want
strArray=["acc123","dpId123","2011-01-01","2022-01-01","hello#xyz.com","IN","1233",""]
How can I avoid this situation?
There you go:
String str1 = " acc123\tdpId 123\t201 1-01-01\t2022-01-01\thello#xyz.com\tIN\t1233\t";
str1 = str1.replaceAll("^[ ]+", ""); // removing leading spaces
str1 = str1.replaceAll("[ ]+$", ""); // removing trailing spaces
String[] split = str1.split("\t", -1);
System.out.println(Arrays.toString(split));
System.out.println(split.length);
String#trim method also removes \t. To handle that I have removed only the leading and trailing spaces using regex.
Output:
[acc123, dpId 123, 201 1-01-01, 2022-01-01, hello#xyz.com, IN, 1233, ]
8
You can use split like so :
String[] split = str.split("\t", -1); // note the -1
To avoid spaces you can use
Arrays.stream(split).map(String::trim).toArray(String[]:new);
you can use limit parameter to solve this str.split("\t",-1) .
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array.
read more about split limit in the docs.
Example:
public class GFG {
public static void main(String args[])
{
String str = "a\tb\tc\t";
String[] arrOfStr = str.split("\t",-1);
for (String a : arrOfStr)
System.out.println(a);
System.out.println(arrOfStr.length);
}
}
The conceptually correct way to do this in your case is to split first, only then trim first and last elements:
String[] array = str.split("\t");
array[0] = array[0].trim();
int last = array.length -1;
if (last > 0) {
array[last] = array[last].trim();
}
Also, if you know upfront how many fields there is supposed to be, then you should also use that knowledge, otherwise you can get an invalid number of fields still:
int fieldsCount = getExpectedFieldsCount();
String[] array = str.split("\t", fieldsCount);
Lastly, I advise you to not use whitespace as the data separator. Use something else. For example, see CSV format, it's a lot better for these things.
Try this (the result array is in the variable resultArray):
String str1 = "acc123\tdpId123\t2011-01-01\t2022-01-01\thello#xyz.com\tIN\t1233\t";
String[] strArray = str1.split("\t");
String regex = ".*\\t$";
String[] resultArray;
if (str1.matches(regex)) {
resultArray = new String[strArray.length + 1];
resultArray[strArray.length] = "";
} else {
resultArray = new String[strArray.length];
}
for (int i= 0; i < strArray.length; i++) {
resultArray[i] = strArray[i];
}
System.out.println(resultArray.length);
System.out.println(Arrays.toString(resultArray));
I have some large files with comma separated data in them. Something like:
firstname,middlename,lastname
James,Tiberius,Kirk
Mister,,Spock
Leonard,,McCoy
I'm using a StringTokenizer to parse the data:
StringTokenizer st = new StringTokenizer(sLine, ",");
while (st.hasMoreTokens()) {
String sTok = st.nextTokens;
tokens.add(tok);
}
The problem is, on lines with no middle name, I only get two tokens, { "Mister", "Spock" }, but I want three tokens, { "Mister, "", "Spock" }
QUESTION: How do I get empty tokens included when parsing my comma separated data?
Thanks!
You can use the String#split(String regex) method.
String[] split = sLine.split(",");
for (String s : split) {
System.out.println("S = " + s); //Note there will be one empty S
tokens.add(s);
}
Use split(",") instead of a StringTokenizer:
String[] aux = sLine.split(",");
for(int i = 0; i < aux.length; i++) {
String sTok = aux[i];
tokens.add(sTok);
}
You can see in the documentation that StringTokenizer is a legacy class and is only kept for retro-compatibility:
http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html
Use split method, but pass -1 as the second argument to keep empty strings
sLine.split(",", -1);
Consider the use of Splitter of Guava Splitter
And you can create an splitter with or without omit empty Strings.
//Example without omit empty Strings (default)
Splitter splitterByComma = Splitter.on(",");
Iterable<String> split = splitterByComma.split("Mister,,Spock");
//Example omitting empty Strings
Splitter splitterByComma = Splitter.on(",").omitEmptyStrings();
Iterable<String> split = splitterByComma.split("Mister,,Spock");
I am making a program which would have the user enter a sentence and following that, the app would break the String into sub-strings where spaces are what break the original string up.
import java.util.StringTokenizer;
public class whitespace {
public static void main(String[] args) {
String text = "supervisors signature tom hanks";
int tokenCount; //number of words
int idx=0; // index
String words[]=new String [500]; // space for words
StringTokenizer st=new StringTokenizer(text); // split text into segements
tokenCount=st.countTokens();
while (st.hasMoreTokens()) // is there stuff to get?
{
words[idx]=st.nextToken();
idx++;
}
}
I have this code thus far and while it works fine as a regular Java program, the while loop seems to cause the app to go into an infinite loop. Any ideas?
I think that you can use the String.split method for this:
String text = "supervisors signature tom hanks";
String[] tokens = text.split("\\s+");
for (String str : tokens)
{
//Do what you need with your tokens here.
}
The regex will split the text into sentences wherever it encounters one or more space characters.
According to this page, the StringTokenizer has been replaced with String.split.
Use this:
words = text.split(" ");
String[] words = text.split(" ");
Use Apache StrTokenizer
StrTokenizer strTok = new StrTokenizer(text);
String[] strList = strTok.getTokenArray();
http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/text/StrTokenizer.html
StringTokenizer sta=new StringTokenizer(text); // split text into segements
String[] words= new String[100];int idx=0;
while (sta.hasMoreTokens()) // is there stuff to get?
{
words[idx]=sta.nextToken();
System.out.println(words[idx]);
idx++;
}
This is what I copied your code and executed by changing little and it worked fine.
I want to split and get rid of the comma's in a string like this that are entered into a textfield:
1,2,3,4,5,6
and then display them in a different textfield like this:
123456
here is what i have tried.
String text = jTextField1.getText();
String[] tokens = text.split(",");
jTextField3.setText(tokens.toString());
Can't you simply replace the , ?
text = text.replace(",", "");
If you're going to put it back together again, you don't need to split it at all. Just replace the commas with the empty string:
jTextField3.setText(text.replace(",", ""));
Assuming this is what you really want to do (e.g. you need to use the individual elements somewhere before concatenating them) the following snippet should work:
String s1 = "1,2,3,4,5,6";
String ss[] = s1.split(",", 0);
StringBuilder sb = new StringBuilder();
for (String s : ss) {
// Use each element here...
sb.append(s);
}
String s2 = sb.toString(); // 123456
Note that the String#split(String) method in Java has strange default behavior so using the method that takes an additional int parameter is recommended.
I may be wrong, but I believe that call to split will get rid of the commas. And it should leave tokens an array of just the numbers