I just found a piece of Java code inside a method:
if (param.contains("|")) {
StringTokenizer st = new StringTokenizer(param.toLowerCase().replace(" ", ""), "|");
if (st.countTokens() > 0) {
...
}
} else {
return myString.contains(param);
}
Can countTokens in the above case ever be less than 1?
It can, if the string you're trying to tokenize is empty, otherwise it'll always at least be 1
Example 1:
String myStr = "abcdefg";
StringTokenizer st = new StringTokenizer(myStr, ";");
int tokens = st.countTokens();
System.out.println("Number of tokens: " + tokens);
> "Number of tokens: 1"
Example 2:
String myStr = "";
StringTokenizer st = new StringTokenizer(myStr, ";");
int tokens = st.countTokens();
System.out.println("Number of tokens: " + tokens);
> "Number of tokens: 0"
Example 3:
String myStr = "abc;defg";
StringTokenizer st = new StringTokenizer(myStr, ";");
int tokens = st.countTokens();
System.out.println("Number of tokens: " + tokens);
> "Number of tokens: 2"
Below return 0:
new StringTokenizer("", "|").countTokens()
new StringTokenizer("|", "|").countTokens()
new StringTokenizer("||||", "|").countTokens()
so countTokens() returns 0 when:
the String is empty
the String contains only the delimeter
Look at this
String param="";
StringTokenizer st = new StringTokenizer(param.toLowerCase().replace(" ", ""), "|");
System.out.println(st.countTokens());
answer is 0(zero)
Related
Am Looping through a sentence splitting it to capitalize.But its hard to build it back after getting the individual words.
String str = "Not the answer you're looking for.";
StringBuilder stringBuilder = new StringBuilder();
String oneWord =" ";
for (String word : str.toLowerCase().split(" ")){
char firstLetter = word.substring(0,1).toUpperCase().charAt(0);
oneWord = firstLetter + word.substring(1);
System.out.println(stringBuilder.append(oneWord + " "));
}
}
I expect to get only one fully built String "Not The Answer You're Looking For."
String str = "Not the answer you're looking for.";
StringBuilder stringBuilder = new StringBuilder();
String oneWord =" ";
for (String word : str.toLowerCase().split(" ")){
char firstLetter = word.substring(0,1).toUpperCase().charAt(0);
oneWord = firstLetter + word.substring(1);
stringBuilder.append(oneWord + " ");
}
System.out.println(stringBuilder.toString());
You are not getting just one string because you use System.out.println inside for loop.
Consider my example above
oneWord += firstLetter + word.substring(1) + " ";
after loop
oneWord = oneWord.trim();
System.out.println(oneWord);
So the solution is:
String str = "Not the answer you're looking for.";
StringBuilder sb = new StringBuilder();
for (String word : str.toLowerCase().split(" ")) {
sb.append(str.substring(0, 1).toUpperCase());
sb.append(str.substring(1));
sb.append(" ");
}
System.out.println(stringBuilder.toString().trim());
Also your solution is not optimal.
Check String.join() or use somth like this
Arrays.stream(str.toLowerCase().split(" "))
.map(word -> str.substring(0, 1).toUpperCase() + str.substring(1))
.collect(Collectors.joining(" "));
I have a function which reads stop words from a file and saves it in a HashSet.
HashSet<String> hset = readFile();
This is my string
String words = "the plan crash is invisible";
I am trying to remove all the stop words from the string but it is not working correctly
The output i am getting: plan crash invible
Output i want => plan crash invisible
Code:
HashSet<String> hset = readFile();
String words = "the plan crash is invisible";
String s = words.toLowerCase();
String[] split = s.split(" ");
for(String str: split){
if (hset.contains(str)) {
s = s.replace(str, "");
} else {
}
}
System.out.println("\n" + "\n" + s);
While hset.contains(str) matches full words, s.replace(str, ""); can replace occurrences of the "stop" words which are part of words of the input String. Hence "invisible" becomes "invible".
Since you are iterating over all the words of s anyway, you can construct a String that contains all the words not contained in the Set:
StringBuilder sb = new StringBuilder();
for(String str: split){
if (!hset.contains(str)) {
if (sb.length() > 0) {
sb.append(' ');
}
sb.append(str);
}
}
System.out.println("\n" + "\n" + sb.toString());
No need so check if your string contain the stop word or split your string, you can use replaceAll which use regex, like this :
for (String str : hset) {
s = s.replaceAll("\\s" + str + "|" + str + "\\s", " ");
}
Excample :
HashSet<String> hset = new HashSet<>();
hset.add("is");
hset.add("the");
String words = "the plan crash is invisible";
String s = words.toLowerCase();
for (String str : hset) {
s = s.replaceAll("\\s" + str + "|" + str + "\\s", " ");
}
s = s.replaceAll("\\s+", " ").trim();//comment and idea of #davidxxx
System.out.println(s);
This can gives you :
plan crash invisible
I am working on a simple method in Java where I take single string from the input, say:
username#192.168.1.1:2531
And I need to separate the username, ip address/host, and port into three different strings. My current thinking is to convert the whole input into a char[], run it through a loop marking the indices which the '#' and the ':' occupy. Then using the offset constructor for a new String to construct each individual string for username, ip/host, and port.
Here is my code:
import java.io.*;
public class StringSymbolParse {
public static void main(String[] args){
String uname;
String host;
int port; //uname#ip:host
String total = args[0];
System.out.println("Input: " + args[0]);
char totalChar[] = total.toCharArray();
int size = totalChar.length;
int markerAt = 0; //marks last filtered symbol placement;
int markerColon = 0;
for(int i=0; i<size; i++){
if((totalChar[i] == '#')){
markerAt = i;
}
else if(totalChar[i] == ':'){
markerColon = i;
}
}
System.out.println("MarkerAT: " + markerAt);
System.out.println("MarkerColon: " + markerColon);
uname = new String(totalChar,0,(markerAt));
System.out.println("Username: " + uname);
host = new String(totalChar, 8, );
port = Integer.parseInt(new String(totalChar, (markerColon), size));
System.out.println("Username: " + uname);
System.out.println("Host: " + host);
System.out.println("Port: " + port);
}
}
`
You can use split method of String class
String s = "username#192.168.1.1:2531";
String[] arr = s.split("#|:");
for (String str : arr) {
System.out.println(str);
}
For something like that, a regular expression is your friend.
String text = "username#192.168.1.1:2531";
Matcher m = Pattern.compile("([^#]*)#([^:#]*):(.*)").matcher(text);
if (m.matches()) {
System.out.println(m.group(1));
System.out.println(m.group(2));
System.out.println(m.group(3));
}
Output
username
192.168.1.1
2531
You can use String.split() method.
String s = "username#192.168.1.1:2531";
String[] arr = s.split("#");
String username = arr[0];
String ipAdd = arr[1].split(":")[0];
String port = arr[1].split(":")[1];
System.out.println("Username : " + username);
System.out.println("IP address : " + ipAdd);
System.out.println("Port : " + port);
Output:
Username : username
IP address : 192.168.1.1
Port : 2531
I have a string data like this
string1 = ["car","house","boat"]["one","two","three","four"]["tiger","cat"]
I want the output to be like this :
first : car,house,boat
second : one,two,three,four
third : tiger,cat
How should I perform manipulation on that string?
This is my current attempt:
result6 = string1.substring(1);
String[] parts = result6.split("\\[");
String part1 = parts[0];
String part2 = parts[1];
String part3 = parts[2];
result3 = part1.replaceAll("[^a-zA-Z0-9,]", "");
result4 = part2.replaceAll("[^a-zA-Z0-9,]", "");
result5 = part3.replaceAll("[^a-zA-Z0-9,]", "");
result1 = "first : " + part1 + "\n" + "second : " + part2 + "\n" + "third : \n" + part3;
But that gets me erroneous output.
You have added part1, part2, part3 instead of result3, 4, 5 in the result1 assignment part. And also i suggest you to split according to ][, i you do the split according to [ only, you would get an empty string in parts[0].
String string1 = "[\"car\",\"house\",\"boat\"][\"one\",\"two\",\"three\",\"four\"][\"tiger\",\"cat\"]";
String parts[] = string1.split("\\]\\[");
String part1 = parts[0];
String part2 = parts[1];
String part3 = parts[2];
String result3 = part1.replaceAll("[^a-zA-Z0-9,]", "");
String result4 = part2.replaceAll("[^a-zA-Z0-9,]", "");
String result5 = part3.replaceAll("[^a-zA-Z0-9,]", "");
String result1 = "first : " + result3 + "\n" + "second : " + result4 + "\n" + "third : " + result5;
System.out.println(result1);
Output:
first : car,house,boat
second : one,two,three,four
third : tiger,cat
I want to execute a query like
select ID from "xyz_DB"."test" where user in ('a','b')
so the corresponding code is like
String s="(";
for(String user:selUsers){
s+= " ' " + user + " ', ";
}
s+=")";
Select ID from test where userId in s;
The following code is forming the value of s as ('a','b',)
i want to remove the comma after the end of array how to do this ?
Here is one way to do this:
String s = "(";
boolean first = true;
for(String user : selUsers){
if (first) {
first = false;
} else {
s += ", ";
}
s += " ' " + user + " '";
}
s += ")";
But it is more efficient to use a StringBuilder to assemble a String if there is looping involved.
StringBuilder sb = new StringBuilder("(");
boolean first = true;
for(String user : selUsers){
if (first) {
first = false;
} else {
sb.append(", ");
}
sb.append(" ' ").append(user).append(" '");
}
sb.append(")");
String s = sb.toString();
This does the trick.
String s = "";
for(String user : selUsers)
s += ", '" + user + "'";
if (selUsers.size() > 0)
s = s.substring(2);
s = "(" + s + ")";
But, a few pointers:
When concatenating strings like this, you are advised to work with StringBuilder and append.
If this is part of an SQL-query, you probably want to sanitize the user-names. See xkcd: Exploits of a Mom for an explanation.
For fun, a variation of Stephen C's answer:
StringBuilder sb = new StringBuilder("(");
boolean first = true;
for(String user : selUsers){
if (!first || (first = false))
sb.append(", ");
sb.append('\'').append(user).append('\'');
}
sb.append(')');
you could even do the loop it like this :-)
for(String user : selUsers)
sb.append(!first || (first=false) ? ", \'" : "\'").append(user).append('\'');
Use the 'old style' of loop where you have the index, then you add the comma on every username except the last:
String[] selUsers = {"a", "b", "c"};
String s="(";
for(int i = 0; i < selUsers.length; i++){
s+= " ' " + selUsers[i] + " ' ";
if(i < selUsers.length -1){
s +=" , ";
}
}
s+=")";
But as others already mentioned, use StringBuffer when concatenating strings:
String[] selUsers = {"a", "b", "c"};
StringBuffer s = new StringBuffer("(");
for(int i = 0; i < selUsers.length; i++){
s.append(" ' " + selUsers[i] + " ' ");
if(i < selUsers.length -1){
s.append(" , ");
}
}
s.append(")");
Use StringUtils.join from apache commons.
Prior to adding the trailing ')' I'd strip off the last character of the string if it's a comma, or perhaps just replace the trailing comma with a right parenthesis - in pseudo-code, something like
if s.last == ',' then
s = s.left(s.length() - 1);
end if;
s = s + ')';
or
if s.last == ',' then
s.last = ')';
else
s = s + ')';
end if;
Share and enjoy.
i would do s+= " ,'" + user + "'"; (place the comma before the value) and add a counter to the loop where i just do s = "'" + user + "'"; if the counter is 1 (or 0, depending on where you start to count).
(N.B. - I'm not a Java guy, so the syntax may be wrong here - apologies if it is).
If selUsers is an array, why not do:
selUsers.join(',');
This should do what you want.
EDIT:
Looks like I was wrong - I figured Java had this functionality built-in. Looks like the Apache project has something that does what I meant, though. Check out this SO answer: Java: function for arrays like PHP's join()?
I fully support Stephen C's answer - that's the one I wanted to suggest aswell: simply avoid adding the additional comma.
But in your special case, the following should work too:
s = s.replace(", \\)", ")");
It simply replaces the substring ", )" with a single bracket ")".
Java 1.4+
s = s.replaceFirst("\\, \\)$", ")");
Edited: I forgot last space before parethesis
StringBuilder has a perfectly good append(int) overload.
String [] array = {"1","2","3" ...};
StringBuilder builder = new StringBuilder();
builder.append(s + "( ")
for(String i : array)
{
if(builder.length() != 0)
builder.append(",");
builder.append(i);
}
builder.append(" )")
Answer shamelessly copied from here