Remove spaces and tokens from a string - java

I am giving string input as "He is a very very good boy, isn't he?"
Scanner scanner = new Scanner(System.in);
String s = scanner.nextLine();
String[] split = s.split("[',',''','?','\\s','(',')',',',';']");
for(String s1:split){
if(s1.equalsIgnoreCase(""))
{
s1.trim();
}
System.out.println(s1);
}
Expected Result:
Actual Result:

Just put a + after your bracket for:
String[] split = s.split("[',',''','?','\\s','(',')',',',';']+");
Or simplify it to:
String[] split = s.split("[,'?\\s();]+");.
It will work how you expected since it will now match multiple characters in a row.
You will also no longer need to use trim() and just call:
for(String s1:split){
System.out.println(s1);
}

.trim() removes whitespace. I.e. " " becomes "". It is not able, however, to remove it from the list. String.split() doesn't know about your list.
The following will do what you want:
String[] split = s.split("<your regex>");
List<String> list = Arrays.asList(split);
list.stream() // convert to a stream for easy filtering
.filter(s -> s.trim().equals("")) // if s.trim().equals(""), remove it from the list/stream
.forEach(System.out::println); // print every remaining element

Related

Java capitalizing first letter of every word in string array

Excuse the brevity, currently on mobile.
I have a string array of values ABC, DEF, GHI that I would like to change to capitalized form: Abc, Def, Ghi
My code looks something like this:
import org.apache.commons.lang3.text.WordUtils;
....
final String[] split = stringToConvert.split(", ");
final StringBuilder sb = new StringBuilder();
for ( String s : split) {
//s = WordUtils.capitalizeFully(s.toLowerCase());
if (sb.length() > 0) {
sb.append(", ");
}
sb.append(WordUtils.capitalizeFully(s.toLowerCase()));
}
return sb.toString();
The first value is always abc, but the second and following ones are correct, e.g. Def, Ghi. I don't know why the first value stays lowercase.
Any help would be appreciated!
Check your code again.
StringBuilder buf = new StringBuilder();
for (String str : new String[]{"ABC", "DEF", "GHI"})
buf.append(WordUtils.capitalizeFully(str.toLowerCase()));
System.out.println(buf);
Prints AbcDefGhi, as expected.
It could be simplier, if you use Stream:
String res = Stream.of("ABC", "DEF", "GHI")
.map(WordUtils::capitalizeFully)
.collect(Collectors.joining(", ")); // if you want to split words with comma
Your code should work.
May I however suggest using a stream instead?
String concatenatedString = Arrays.stream(array)
.map(WordUtils::capitalizeFully)
.collect(Collectors.joining());
Which, with appropriate static imports fits well on one line without losing readability:
String concatenatedString = stream(array).map(WordUtils::capitalizeFully).collect(joining());
Note that joining() uses a StringBuilder iternally, so you don't need to worry about performance here. Also, joining() allows you to choose which string you want to delimit the content of the stream with, in this case I chose an empty string, which would result in AbcDefGhi.
This should do :
String[] stringToSplit = {"ABC", "DEF", "GHI"};
StringBuilder sb = new StringBuilder();
for(String s: stringToSplit) {
sb.append(s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase());
}
Update: I'm tired...
The first character was actually a [ from the array instead of "a", thus the a was never capitalized
Thanks all, and sorry for wasting your time

How to split string from String array [] by strings (word by by word instead of characters) with delimitter?

void displayResult() {
String str = "tamilnadu||chennai-karanataka||bengaluru";
String[] res = str.split("\\-");
System.out.println(res.length);//res length is 2
//res contains two strings splited by -
String[] result = res.toString().split("\\||");
//again splitting by || but getting as characters, i need to get word by word
// how to achieve this
System.out.println(result.length);//result length is 28
}
// i was supposed to get tamilnadu and chennai from first string[] res
String[] res holds two strings split by - i am trying to split res in the same way to get strings split by || pipe symbol but i am getting as characters
how to get as like before
Don't use split method two times.you can complete your task with one split method. like this,
void displayResult() {
String str = "tamilnadu||chennai-karanataka||bengaluru";
String[] res = str.split("\\|\\||-");
for(String city : res){
System.out.println(city);
}
}
your output will be:-
tamilnadu
chennai
karanataka
bengaluru
You need to use two \ to escape |,so the regex is \\|\\||- ,we can get the words with only one split
String str = "tamilnadu||chennai-karanataka||bengaluru";
String[] strs = str.split("\\|\\||-");
for(String s:strs){
System.out.println(s);
}
Output is
tamilnadu
chennai
karanataka
bengaluru
You should not call res.toString. It will return something like this:
[Ljava.lang.String;#2c7b84de
Instead, you can split on the elements in res with regex \|\|:
String[] result = res[0].split("\\|\\|");
System.out.println(result.length); // 2
I think you must be trying your hands with learning Regex. You could use | operator to specify OR condition. So if you use following you should get desired array :
String[] res = str.split("\-|\|\|");
i.e. your method will be :
void displayResult(){
String str = "tamilnadu||chennai-karanataka||bengaluru";
String[] res = str.split("\\-|\\|\\|");
System.out.println(Arrays.asList(res)); // don't forget to import Arrays
}
you split the **str** into substrings and stored in **res** array,again
your
trying to split the substrings, you should specify that which substring going
to split.
void displayResult() {
String str = "tamilnadu||chennai-karanataka||bengaluru";
String[] res = str.split("\\-");
System.out.println(res.length);
**String[] result = res[0].toString().split("\\|");**
for (String string : result) {
System.out.println(string);
}
System.out.println(result.length);
}
output:
2
tmailnadu
chennai
3

How make java split java with pipe

I have to separate a string into an array that may contain empty spaces, for example,
|maria|joao|fernando||
but it is ignored when you have space at the end of the line
I am using this regex split("\\|")
It should be like it: maria,joao,fernando,null
but stays like this: maria,joao,fernando
You can use:
String str = "|maria|joao|fernando||";
String[] tokens = str.replaceAll("^[|]|[|]$", "").split("[|]", -1));
//=> [maria, joao, fernando, ""]
Steps:
Replace starting and ending | using replaceAll method to get maria|joao|fernando| as input to split.
Then split it using split method with 2nd parameter as -1 to return empty tokens as well.
You only need to add double backslashes to you split string
String s = "|maria|joao|fernando||";
String [] st =s.split("\\|");
for (String string : st) {
System.out.print(string+",");
}
Java 8 solution
List<String> params = Pattern
.compile("\\|")
.splitAsStream("|maria|joao|fernando||")
.filter(e -> e.trim().length() > 0) // Remove spaces only or empty strings
.collect(Collectors.toList());

How to split a string by comma followed by a colon in java?

I'm a java newbie and I'm curious to know how to split a string that starts with a comma and gets followed by a colon towards the end.
An example of such string would be?
-10,3,15,4:38
5,15,8,2:8
Could it be like this?
sections = line.split(",");
tokens = sections[3].split(":");
or is it even possible to split line which the file is read into twice?
tokens = line.split(",");
tokens = line.split(":");
I also tried this but it gave me an ArrayOutOfBound error
tokens = line.split("[,:]");
Any contribution would be appreciated.
use a regular expression in the split section such as
line.split(",|;");
Haven't tested it but I think you get the idea.
You can also do it this way, if you want it for a general case, the method basically takes in the string array, splits each string at each index in the array and adds them to an ArrayList. You can try it, it works.
public static void splitStrings(String[] str){
String[] temp1 =null;//initialize temp array
List<String> itemList = new ArrayList<String>();
for(int i=0;i<str.length;i++){
temp1=str[i].split(",|:");
for (String item : temp1) {
itemList.add(item);
}
//Only print the final result of collection once iteration has ended
if(i==str.length-1){
System.out.println(itemList);
}
}
I am not sure if I totally understand your question correctly. But if you first want to split by , and then by :, you can call split() function twice
String[] str = {"-10,3,15,4:38", "5,15,8,2:8"};
for (String s: str) {
String[] temp = s.split(",")[3].split(":");
System.out.println(temp[0] + " " + temp[1]);
}
Output:
4 38
2 8

How can I parse a "key1:value1, value, key2:value3" string into ArrayLists?

I have a string
String line = "abc:xyz uvw, def:ghi, mno:rst, ijk:efg, abc opq";
I want to parse this string into two lists:
ArrayList<String> tags;
ArrayList<String> values;
where the tags are the words before the colon (in my example: abc, def, ijk and mno). That is I want
tags = Arrays.asList("abc", "def", "mno", "ijk");
values = Arrays.asList("xyz uvw", "ghi", "rst", "efg, abc opq");
Note that the values can have spaces and commas in them and are not just one word.
Since your values can contain commas, you need to split when you find a key.
A key is defined as a word preceding a :.
So, your split pattern will be ", (?=[a-zA-z]+:)"
This checks for a comma space chars colon in the specified order, looking ahead the chars and colon.
Checks for a key, and splits with lookahead (thus leaving the key intact). This will give you an array of keyValue pairs
Then you can split with : to get the keys and values.
String str = "Your string";
String[] keyValuePairs = str.split(", (?=[a-zA-z]+:)");
for (String keyValuePair : keyValuePairs) {
String[] keyvalue = keyValuePair.split(":");
keysArray.add(keyvalue[0]);
valuesArray.add(keyvalue[1]);
}
I would go with a regex. I am not sure how to do this in Java but in python that would be:
(\w+):([ ,\w]+)(,|$)
Tested on pythex with input abc:xy z uvw, def:g,hi, mno:rst. The result is:
Match 1
1. abc
2. xy z uvw
3. ,
Match 2
1. def
2. g,hi
3. ,
Match 3
1. mno
2. rst
3. Empty
So for each match you get the key in position 1 and the value in 2. The separator is stored in 3
First obtain your string from the file
List<String> tags = new ArrayList<String>();
List<String> values = new ArrayList<String>;
String lineThatWasRead = ...
Then we split it by commas to obtain the pairs, and for each pari split by :
List<String> separatedStringList = Arrays.asList(lineThatWasRead.split(","));
for (String str : separatedStringList)
{
//Since it is possible values also contain commas, we have to check
//if the current string is a new pair of tag:value or just adding to the previous value
if (!str.contains(":"))
{
if (values.size > 0)
{
values.set(values.size() - 1, values.get(values.size()-1) + ", " + str);
continue; //And go to next str since this one will not have new keys
}
}
String[] keyValArray = str.split(:);
String key = keyValArray[0];
String val = keyValArray[1];
tags.add(key);
values.add(val);
}
Note that you are not forced to use a list but I just like the flexibility they give. Some might argue String[] would perform better for the first split by ,.
You get your line as string.
//your code here
String line = //your code here
String[] stuff = line.split(":")// this will split your line by ":" symbol.
stuff[0] //this is your key
stuff[1] //this is your value

Categories