Java split by comma in string containing whitespace - java

I have a below string which I want to split by ',' only and also want to separate 3rd index which is (1005,111222) of each line .
1002,USD,04/09/2019,1005,1,cref,,,,,,,,,
1001,USD,11/04/2018,111222,10,reftpt001,SHA,Remittance Code,BCITIT31745,,,RTGS,,,,
I am using code down below :
List<String> elements = new ArrayList<String>();
List<String> elements2 = new ArrayList<String>();
StringTokenizer st = new StringTokenizer((String) object);
while(st.hasMoreTokens()) {
String[] row = st.nextToken().split(",");
if (row.length == 5) {
elements.add(row[3]);
}
if (row.length == 12) {
elements2.add(row[3]);
}
}
In the above string, There is a space between 'Remittance Code' but it is splitting till remittance and after that, it counts the code a new line or string. Please advise how can I skip the white space as it is.

There is no apparent need for StringTokenizer here, and the nextToken() call stops at the first space. Instead I suggest calling output.split(",") directly like
String[] row = ((String) object).split("\\s*,\\s*", -1);
And remove the StringTokenizer, note the JavaDoc explicitly says StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

First you can split with, and then use trim operation

String stringToSplit= "1001,ZAR,11/04/2018,111222,10,reftpt001,SHA,Remittance Code,BCITIT31745,,,RTGS,,,,";
StringTokenizer tokenizer = new StringTokenizer(stringToSplit, ",");
while (tokenizer.hasMoreTokens()) { System.out.println(tokenizer.nextToken()); }
Output :
1001 ZAR 11/04/2018 111222 10 reftpt001 SHA Remittance Code
BCITIT31745 RTGS

I tried with this code:
1st approach :
String str = "1001,ZAR,11/04/2018,111222,10,reftpt001,SHA,Remittance Code,BCITIT31745";
String[] words = str.split(",");
for(String word : words) {
System.out.println(word);
}
2nd approach :
String str = "1001,ZAR,11/04/2018,111222,10,reftpt001,SHA,Remittance Code,BCITIT31745";
StringTokenizer tokenizer = new StringTokenizer(str, ",");
while(tokenizer.hasMoreTokens())
{
System.out.println(tokenizer.nextToken());
}
Output :
11/04/2018
111222
10
reftpt001
SHA
Remittance Code
BCITIT31745
Hope this helps you. :)

Related

JSP JSTL funciton fn:split is not working properly

Today, I come across one issue and need your help to fix it.
I am trying to split the string using JSTL fn:split function that is likewise,
<c:set var="stringArrayName" value="${fn:split(element, '~$')}" />
Actual String :- "abc~$pqr$xyz"
Expected Result :-
abc
pqr$xyz
only 2-string part expecting, but it gives
abc
pqr
xyz
here, total 3-string parts returning, which is wrong.
NOTE :- I have added <%#taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> at the top of JSP.
any help really appreciates!!
JSTL split not work like the Java split you can check the difference from the code source :
org.apache.taglibs.standard.functions.Functions.split
public static String[] split(String input, String delimiters) {
String[] array;
if (input == null) {
input = "";
}
if (input.length() == 0) {
array = new String[1];
array[0] = "";
return array;
}
if (delimiters == null) {
delimiters = "";
}
StringTokenizer tok = new StringTokenizer(input, delimiters);
int count = tok.countTokens();
array = new String[count];
int i = 0;
while (tok.hasMoreTokens()) {
array[i++] = tok.nextToken();
}
return array;
}
java.lang.String.split
public String[] split(String regex, int limit) {
return Pattern.compile(regex).split(this, limit);
}
So it's clearly that fn:split use StringTokenizer
...
StringTokenizer tok = new StringTokenizer(input, delimiters);
int count = tok.countTokens();
array = new String[count];
int i = 0;
while (tok.hasMoreTokens()) {
array[i++] = tok.nextToken();
}
...
Not like java.lang.String.split which use regular expression
return Pattern.compile(regex).split(this, limit);
//-----------------------^
from the StringTokenizer documentation it says :
Constructs a string tokenizer for the specified string. The characters
in the delim argument are the delimiters for separating tokens.
Delimiter characters themselves will not be treated as tokens.
How `fn:split` exactly work?
It split on each character in the delimiter, in your case you have two characters ~ and $ so if your string is abc~$pqr$xyz it will split it like this :
abc~$pqr$xyz
^^ ^
1st split :
abc
$pqr$xyz
2nd split :
abc
pqr$xyz
3rd split :
abc
pqr
xyz
Solution
use split in your Servlet instead of JSTL
for example :
String[] array = "abc~$pqr$xyz".split("~\\$");

StringBuilder append "," basic concern

I have a code like:
projsForWhichPermCheckFailedSBuilder.append(currentProject.getDisplayString()).append(", ");
This is dynamic append and above code is in for loop... so at last I want to remove extra ", ", I have done something like:
projsForWhichPermCheckFailedString = projsForWhichPermCheckFailedSBuilder.substring(0, projsForWhichPermCheckFailedSBuilder.length()-2);
Please suggest some better way!!! Thanks...
I'm not sure what your input looks like, but let's say for the sake of an example, the input is a collection of String objects, e.g. List<String> input and given:
final String delimiter = ",";
then you could use one of the following approaches ...
Using Java StringBuilder class.
StringBuilder sb = new StringBuilder();
for (String s : input)
{
sb.append("s").append(delimiter);
}
if (sb.toString().endsWith(delimiter))
{
sb.deleteCharAt(sb.length()-1);
}
System.out.println(sb.toString());
// Results in output: s,s,s
Using Guava (see Strings Explained for a full example).
List<String> input = new ArrayList<>();
input.add("1");
input.add("2");
input.add("3");
final String result = Joiner.on(delimiter).join(input);
System.out.println(result);
// Results in output: 1,2,3

How do you rebuild a string using StringBuilder?

Im trying to rebuild a string using StringBuilder. I'm a little unsure of which method to use to get the "'," inserted back into the same place. In the code below I'm using the
"insert(int dstOffset, CharSequence s, int start, int end)" method. My code doesn't contain any errors however it doesn't run properly.
Please note I will also be escaping characters (i.e., =) in the string but I havent written that part of the code yet. Currently I'm trying to learn how to split the string and then rebuild it.
Thanks
public class StringTestProgram
{
public static void main(String[] args)
{
String relativeDN = "cn=abc,dn=xyz,ou=abc/def";
String[] stringData = relativeDN.split(",");
for (String stringoutput : stringData)
{
System.out.print(stringoutput);
StringBuilder sb = new StringBuilder(stringoutput);
CharSequence charAdded = ",";
sb.insert(6,charAdded,0,12);
System.out.print(sb.toString());
}
}
}
Revised code
public class StringTestProgram {
public static void main(String[] args) {
String relativeDN = "cn=abc,dn=xyz,ou=abc/def";
System.out.println(relativeDN);
//Split String
String[] stringData = relativeDN.split(",");
{
StringBuilder sb = new StringBuilder();
CharSequence charAdded = ",";
// loop thru each element of the array
for (int place = 0; place < stringData.length; place++) {
System.out.println(stringData[place]);
{
int eq = relativeDN.indexOf('=');
String sub = relativeDN.substring(0, eq);
System.out.println(sub);
}
// append element to the StringBuilder
sb.append(stringData[place]);
// avoids adding an extra ',' at the end
if (place < stringData.length - 1)
// if not at the last element, add the ',' character
sb.append(charAdded);
}
System.out.print(sb.toString());
}
}
}
Im new to stackoverflow and I'm not sure if its ok to ask this question in this thread or if I should create a seperate thread for this question. If possible please advise.
The code above now splits the string at the "," character. It also rebuilds the
string back to its original state. I would also like to use the indexof and .substring
methods to get the string value after the "=" sign. Currently my program only outputs
the first two characters of the initial string value before the "=" sign. Not sure where
in my code I'm making an error. Any help would be appreciated.
My Current Output
cn=abc,dn=xyz,ou=abc/def
cn=abc
cn
dn=xyz
cn
ou=abc/def
cn
cn=abc,dn=xyz,ou=abc/def
Desired Output
cn=abc,dn=xyz,ou=abc/def
cn=abc
abc
dn=xyz
xyz
ou=abc/def
abc/def
cn=abc,dn=xyz,ou=abc/def
The easiest way to do this pre Java 8 is to use 1 StringBuilder for all the elements and add Strings to the builder by using the append() method
StringBuilder builder = new StringBuilder();
for (String stringoutput : stringData) {
builder.append(stringoutput).append(',');
}
//have an extra trailing comma so remove it
//use length -1 as end coord because it's exclusive
String result = builder.substring(0, builder.length() -1);
If you are using Java 8 you can use the new Stream API and Collectors.joining()
String result = Arrays.stream(relativeDN.split(","))
.collect(Collectors.joining(","));
You're initializing sb every time you enter the loop, meaning that you're disposing of your StringBuilder every time you enter the loop and recreate it with only the next subtring.
Fixed:
String relativeDN = "cn=abc,dn=xyz,ou=abc/def";
String[] stringData = relativeDN.split(",");
StringBuilder sb = new StringBuilder();
CharSequence charAdded = ",";
for (String stringoutput : stringData) {
System.out.print(stringoutput);
sb.append(stringoutput).append(charAdded);
}
sb.setLength(sb.length() - 1);
System.out.print(sb.toString());
Try out this code
public class StringTestProgram {
public static void main(String[] args) {
String relativeDN = "cn=abc,dn=xyz,ou=abc/def";
String[] stringData = relativeDN.split(",");
StringBuilder sb = new StringBuilder();
CharSequence charAdded = ",";
for (int i = 0; i < stringData .length; i++) { //walk over each element of the array
System.out.println(stringData[i]);
sb.append(stringData[i]); // append element to the StringBuilder
if (i < stringData.length - 1) //avoids adding an extra ',' at the end
sb.append(charAdded); // if not at the last element, add the ',' character
}
System.out.print(sb.toString());
}
}
Here you will reconstruct the original string exactly as it was (i.e. without adding a trailing ','):
cn=abc,dn=xyz,ou=abc/def
UPDATE: In the for loop I just walk over every element of the array that stores the splitted String and append the elements to the StringBuilder instance one by one. After appending each element I check if we are currently at the last element of the array. If not, I append the ',' character.
Like this:
for (String stringoutput : stringData)
sb.append(stringoutput).append(',');
Fixed: Using this approach, you would have to remove the last ,
String result = sb.toString().substring(0,sb.toString().length()-1);
System.out.println(result);
I noticed in the other answers that there would be an extra comma at the end. You have to use a prefix variable and then change it in the loop so that there won't be an extra comma.
String relativeDN = "cn=abc,dn=xyz,ou=abc/def";
String[] stringData = relativeDN.split(",");
StringBuilder sb = new StringBuilder();
String prefix = "";
for (String element : stringData) {
sb.append(prefix);
prefix=",";
sb.append(element);
}
String output = sb.toString();
Inside the loop the prefix is appended, but on the first time through the loop the prefix is set to empty quotes so that there won't be a comma before the first element. Next prefix is changed to a comma so that in the next turn through the loop a comma will be added after the first element. Lastly, the element is added. This results in the correct output because the comma is added before the element, but only after the first iteration.

How to search word by word in android

How to search a word in a string?
For example
String text = "Samsung Galaxy S Two";
If I use text.contains("???");
It will get any related alphabets even it is not a proper word such as "axy" from "Galaxy".
Any suggestion or solution?
List<String> tokens = new ArrayList<String>();
String text = "Samsung Galaxy S Two";
StringTokenizer st = new StringTokenizer(text);
//("---- Split by space ------");
while (st.hasMoreElements()) {
tokens.add(st.nextElement().toString());
}
String search = "axy";
for(int i=0;i<tokens.size();i++)
{
if(tokens.get(i).contains(search))
{
System.out.println("Word is "+tokens.get(i));
break;//=====> Remove Break if you want to continue searching all the words which contains `axy`
}
}
output====>Galaxy
For most simple usage, you can use a StringTokenizer
Look at this link.
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html
For using Regular expressions, Look at Patterns in android.
http://developer.android.com/reference/java/util/regex/Pattern.html
use indexOf:
int i= string.indexOf('1');
or substring:
String s=string.substring("koko",0,1);
Try this..
String string = "madam, i am Adam";
// Characters
// First occurrence of a c
int index = string.indexOf('a'); // 1
// Last occurrence
index = string.lastIndexOf('a'); // 14
// Not found
index = string.lastIndexOf('z'); // -1
// Substrings
// First occurrence
index = string.indexOf("dam"); // 2
// Last occurrence
index = string.lastIndexOf("dam"); // 13
// Not found
index = string.lastIndexOf("z"); // -1
I know this is an old question, but I am writing here so that the next person who needs help can be helped.
You can use matches.
String str = "Hello, this is a trial text";
str1 = str.toLowerCase();
if(str1.matches(".*trial.*")) //this will search for the word "trial" in str1
{
//Your Code
}

string tokenizer in Java

I have a text file which contains data seperated by '|'. I need to get each field(seperated by '|') and process it. The text file can be shown as below :
ABC|DEF||FGHT
I am using string tokenizer(JDK 1.4) for getting each field value. Now the problem is, I should get an empty string after DEF.However, I am not getting the empty space between DEF & FGHT.
My result should be - ABC,DEF,"",FGHT but I am getting ABC,DEF,FGHT
From StringTokenizer documentation :
StringTokenizer is a legacy class that
is retained for compatibility reasons
although its use is discouraged in new
code. It is recommended that anyone
seeking this functionality use the
split method of String or the
java.util.regex package instead.
The following code should work :
String s = "ABC|DEF||FGHT";
String[] r = s.split("\\|");
Use the returnDelims flag and check two subsequent occurrences of the delimiter:
String str = "ABC|DEF||FGHT";
String delim = "|";
StringTokenizer tok = new StringTokenizer(str, delim, true);
boolean expectDelim = false;
while (tok.hasMoreTokens()) {
String token = tok.nextToken();
if (delim.equals(token)) {
if (expectDelim) {
expectDelim = false;
continue;
} else {
// unexpected delim means empty token
token = null;
}
}
System.out.println(token);
expectDelim = true;
}
this prints
ABC
DEF
null
FGHT
The API isn't pretty and therefore considered legacy (i.e. "almost obsolete"). Use it only with where pattern matching is too expensive (which should only be the case for extremely long strings) or where an API expects an Enumeration.
In case you switch to String.split(String), make sure to quote the delimiter. Either manually ("\\|") or automatically using string.split(Pattern.quote(delim));
StringTokenizer ignores empty elements. Consider using String.split, which is also available in 1.4.
From the javadocs:
StringTokenizer is a legacy class that
is retained for compatibility reasons
although its use is discouraged in new
code. It is recommended that anyone
seeking this functionality use the
split method of String or the
java.util.regex package instead.
you can use the constructor that takes an extra 'returnDelims' boolean, and pass true to it.
this way you will receive the delimiters, which will allow you to detect this condition.
alternatively you can just implement your own string tokenizer that does what you need, it's not that hard.
Here is another way to solve this problem
String str = "ABC|DEF||FGHT";
StringTokenizer s = new StringTokenizer(str,"|",true);
String currentToken="",previousToken="";
while(s.hasMoreTokens())
{
//Get the current token from the tokenize strings
currentToken = s.nextToken();
//Check for the empty token in between ||
if(currentToken.equals("|") && previousToken.equals("|"))
{
//We denote the empty token so we print null on the screen
System.out.println("null");
}
else
{
//We only print the tokens except delimiters
if(!currentToken.equals("|"))
System.out.println(currentToken);
}
previousToken = currentToken;
}
Here is a way to split a string into tokens (a token is one or more letters)
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
s = s.replaceAll("[^A-Za-z]", " ");
StringTokenizer arr = new StringTokenizer(s, " ");
int n = arr.countTokens();
System.out.println(n);
while(arr.hasMoreTokens()){
System.out.println(arr.nextToken());
}
scan.close();
}
package com.java.String;
import java.util.StringTokenizer;
public class StringWordReverse {
public static void main(String[] kam) {
String s;
String sReversed = "";
System.out.println("Enter a string to reverse");
s = "THIS IS ASHIK SKLAB";
StringTokenizer st = new StringTokenizer(s);
while (st.hasMoreTokens()) {
sReversed = st.nextToken() + " " + sReversed;
}
System.out.println("Original string is : " + s);
System.out.println("Reversed string is : " + sReversed);
}
}
Output:
Enter a string to reverse
Original string is : THIS IS ASHIK SKLAB
Reversed string is : SKLAB ASHIK IS THIS

Categories

Resources