Replace String and check for uniqueness within String - java

I am trying to replace specific string with empty value with in string. But below snippet is not removing spaces. Also is there easy approach to look for uniqueness with in strings separated my delimiter?
String str = "||MGR||RAI MGR||PRE RAI MGR||PRE RAI SPR||PRE SPR||";
String newStr = str.replaceAll("RAI", "");
System.out.println("Updates String is::"+newStr);
Output I am looking for is ||MGR||PRE MGR||PRE SPR||
Thanks

Try:
String newStr = str.replaceAll("RAI ", "").replaceAll(" RAI", "").replaceAll("RAI", "");

--- Edit Update ---
Wait a second, you are doing a number of things here. You're not just doing string replacements, you are also compacting the fields between the || delimiters such that you don't have duplicate fields with the same content.
If you were just stripping the "RAI" then you would have
||MGR||MGR||PRE MGR||PRE SPR||PRE SPR||
So, first, split all your fields into Strings, along the || delimiter. Then strip each string of the undesired "RAI". Add them to a Set<String>, and then rebuild the input string from the items in the Set.
--- original post follows ---
You will get a section with two spaces using the technique you are driving at, that's because "PRE RAI MGR" will compact down to "PRE MGR".
One trick is to replace "RAI " with " ", then replace " RAI" with " " and finally replace "RAI" with ""

Include a space in your replaceAll() regex, then using Java 8, you can remove duplicates. Otherwise, you'd have to manually remove duplicate yourself which is still possible, but why reinvent the wheel (except for learning purposes).
public static void main(String[] args) throws Exception {
String str = "||MGR||RAI MGR||PRE RAI MGR||PRE RAI SPR||PRE SPR||";
// "RAI\\s?" means there may be a single space after "RAI"
String newStr = str.replaceAll("RAI\\s?", "");
System.out.println("Updates String is:: " + newStr);
// Remove duplicates
System.out.println("Duplicates Removed:: " + Arrays.stream(
newStr.split("(?=\\|\\|)"))
.distinct()
.map(s -> (s))
.collect(Collectors.joining()));
}
Results:
Updates String is:: ||MGR||MGR||PRE MGR||PRE SPR||PRE SPR||
Duplicates Removed:: ||MGR||PRE MGR||PRE SPR||

Use
String wordToReplace = "RAI";
String regex = "\\s*\\" + wordToReplace + "\\b\\s*";
str = str.replaceAll(regex, "");

replaceAll method of String takes a regex so you could pass one and use this instead
String newStr = str.replaceAll("\\s?RAI\\s?", "");
[Edit]
You only need to remove the trailing space so get the desired output. Use
String newStr = str.replaceAll("RAI\\s?", "");
Complete code:
String str = "||MGR||RAI MGR||PRE RAI MGR||PRE RAI SPR||PRE SPR||";
String newStr = str.replaceAll("RAI\\s?", "");
Set<String> parts = new LinkedHashSet<String>(Arrays.asList(newStr.split("||")));
StringBuilder sdb = new StringBuilder("||");
for (String part : parts) {
sdb.append(part).append("||");
}
newStr = sdb.toString();
System.out.println("Updates String is::" + newStr);

Replace the string to omit " RAI" or "RAI", split to check the uniqueness of values and create new string with unique values.
String str = "||MGR||RAI MGR||PRE RAI MGR||PRE RAI SPR||PRE SPR";
String newStr = "||";
str = str.replaceAll("( |)RAI", "");
String[] values = str.split("\\|\\|");
//add only unique values to new string
for (int i = 1; i < values.length; i++) {
if(!newStr.contains(values[i].trim())){
newStr += values[i].trim() + "||";
}
}
System.out.println("Updates String is::" + newStr);

Thanks for all your inputs. Based on comments and suggestions I managed to get desired output using below code
String str = "||MGR||RAI MGR||PRE RAI MGR||PRE RAI SPR||PRE SPR||";
String noDupBRole = "||";
String newStr = str.replaceAll("RAI ", "").replaceAll(" RAI", "").replaceAll("RAI", "");
System.out.println("New String is::"+newStr);
Set<String> set = new LinkedHashSet<String>(Arrays.asList(newStr.split(Pattern.quote("||"))));
for(String st : set) {
if(st.isEmpty()) continue;
noDupBRole += st+"||";
}
System.out.println("No Duplicate ::"+noDupBRole);

Related

Why is the replace all method not working?

I am testing out the replaceAll() method of the String class and I am having problems with it.
I do not understand why my code does not replace whitespaces with an empty string.
Here's my code:
public static void main(String[] args) {
String str = " I like pie!#!#! It's one of my favorite things !1!!!1111";
str = str.toLowerCase();
str = str.replaceAll("\\p{Punct}", "");
str = str.replaceAll("[^a-zA-Z]", "");
str = str.replaceAll("\\s+", " ");
System.out.print(str);
}
Output:
ilikepieitsoneofmyfavoritethings
The problem is there are no whitespaces in your String after this:
str = str.replaceAll("[^a-zA-Z]", "");
which replaces all characters that are not letters, which includes whitespaces, with a blank (effectively deleting it).
Add whitespace to that character class so they don't get nuked:
str = str.replaceAll("[^a-zA-Z\\s]", "");
And this line may be deleted:
str = str.replaceAll("\\p{Punct}", "");
because it's redundant.
Final code:
String str = " I like pie!#!#! It's one of my favorite things !1!!!1111";
str = str.toLowerCase();
str = str.replaceAll("[^a-zA-Z\\s]", "");
str = str.replaceAll("\\s+", " ");
System.out.print(str);
Output:
i like pie its one of my favorite things
You may want to add str = str.trim(); to remove the leading space.

Replace the words in String without using String replace

Is there any solution on how to replace words in string without using String replace?
As you all can see this is like hard coded it. Is there any method to make it dynamically? I heard that there is some library file able to make it dynamically but I am not very sure.
Any expert out there able to give me some solutions? Thank you so much and have a nice day.
for (int i = 0; i < results.size(); ++i) {
// To remove the unwanted words in the query
test = results.toString();
String testresults = test.replace("numFound=2,start=0,docs=[","");
testresults = testresults.replace("numFound=1,start=0,docs=[","");
testresults = testresults.replace("{","");
testresults = testresults.replace("SolrDocument","");
testresults = testresults.replace("numFound=4,start=0,docs=[","");
testresults = testresults.replace("SolrDocument{", "");
testresults = testresults.replace("content=[", "");
testresults = testresults.replace("id=", "");
testresults = testresults.replace("]}]}", "");
testresults = testresults.replace("]}", "");
testresults = testresults.replace("}", "");
In this case, you will need learn regular expression and a built-in String function String.replaceAll() to capture all possible unwanted words.
For example:
test.replaceAll("SolrDocument|id=|content=\\[", "");
Simply create and use a custom String.replace() method which happens to use the String.replace() method within it:
public static String customReplace(String inputString, String replaceWith, String... stringsToReplace) {
if (inputString.equals("")) { return replaceWith; }
if (stringsToReplace.length == 0) { return inputString; }
for (int i = 0; i < stringsToReplace.length; i++) {
inputString = inputString.replace(stringsToReplace[i], replaceWith);
}
return inputString;
}
In the example method above you can supply as many strings as you like to be replaced within the stringsToReplace parameter as long as they are delimited with a comma (,). They will all be replaced with what you supply for the replaceWith parameter.
Here is an example of how it can be used:
String test = "This is a string which contains numFound=2,start=0,docs=[ crap and it may also "
+ "have numFound=1,start=0,docs=[ junk in it along with open curly bracket { and "
+ "the SolrDocument word which might also have ]}]} other crap in there too.";
testResult = customReplace(strg, "", "numFound=2,start=0,docs=[ ", "numFound=1,start=0,docs=[ ",
+ "{ ", "SolrDocument ", "]}]} ");
System.out.println(testResult);
You can also pass a single String Array which contains all your unwanted strings within its elements and pass that array to the stringsToReplace parameter, for example:
String test = "This is a string which contains numFound=2,start=0,docs=[ crap and it may also "
+ "have numFound=1,start=0,docs=[ junk in it along with open curly bracket { and "
+ "the SolrDocument word which might also have ]}]} other crap in there too.";
String[] unwantedStrings = {"numFound=2,start=0,docs=[ ", "numFound=1,start=0,docs=[ ",
"{ ", "SolrDocument ", "]}]} "};
String testResult = customReplace(test, "", unwantedStrings);
System.out.println(testResult);

Need to remove extra square brackets in the string

static ArrayList<String> coordinates = new ArrayList<String>();
static String str = "";
static ArrayList scribbles = new ArrayList();
coordinates.add("String to be placed, String not to be placed");
String codChange = coordinates.toString().replaceAll(", ", "");
StringBuffer sb = new StringBuffer(codChange);
sb.insert(1,"m ");
ArrayList aListNumbers = new ArrayList(Arrays.asList(sb.toString()));
System.out.println("Coordinates: " + aListNumbers.toString().replaceAll("\\[|\\]", ""));
scribbles.add(aListNumbers);
str = scribbles.toString();
System.out.println("String: " + str);
OUTPUT:
Coordinates: m String to be placedString not to be placed
String: [[m String to be placedString not to be placed]]
I want the String: to appear with single square brackets like:
String: [m String to be placedString not to be placed]
Since there are two different replacement required.
Use below code
String s = "[[m String to be placedString not to be placed]]";
System.out.println(s.replaceAll("[[","[").replaceAll("]]","]");
If you are sure about always the exact position of [[ is at the beginning and ]] is at end, just use substring as suggested in the other answer in the same SO answer thread.

Cut ':' && " " from a String with a tokenizer

right now I am a little bit confused. I want to manipulate this string with a tokenizer:
Bob:23456:12345 Carl:09876:54321
However, I use a Tokenizer, but when I try:
String signature1 = tok.nextToken(":");
tok.nextToken(" ")
I get:
12345 Carl
However I want to have the first int and the second int into a var.
Any ideas?
You have two different patterns, maybe you should handle both separated.
Fist you should split the space separated values. Only use the string split(" "). That will return a String[].
Then for each String use tokenizer.
I believe will works.
Code:
String input = "Bob:23456:12345 Carl:09876:54321";
String[] words = input.split(" ")
for (String word : words) {
String[] token = each.split(":");
String name = token[0];
int value0 = Integer.parseInt(token[1]);
int value1 = Integer.parseInt(token[2]);
}
Following code should do:
String input = "Bob:23456:12345 Carl:09876:54321";
StringTokenizer st = new StringTokenizer(input, ": ");
while(st.hasMoreTokens())
{
String name = st.nextToken();
String val1 = st.nextToken();
String val2 = st.nextToken();
}
Seeing as you have multiple patterns, you cannot handle them with only one tokenizer.
You need to first split it based on whitespace, then split based on the colon.
Something like this should help:
String[] s = "Bob:23456:12345 Carl:09876:54321".split(" ");
System.out.println(Arrays.toString(s ));
String[] so = s[0].split(":", 2);
System.out.println(Arrays.toString(so));
And you'd get this:
[Bob:23456:12345, Carl:09876:54321]
[Bob, 23456:12345]
If you must use tokeniser then I tink you need to use it twice
String str = "Bob:23456:12345 Carl:09876:54321";
StringTokenizer spaceTokenizer = new StringTokenizer(str, " ");
while (spaceTokenizer.hasMoreTokens()) {
StringTokenizer colonTokenizer = new StringTokenizer(spaceTokenizer.nextToken(), ":");
colonTokenizer.nextToken();//to igore Bob and Carl
while (colonTokenizer.hasMoreTokens()) {
System.out.println(colonTokenizer.nextToken());
}
}
outputs
23456
12345
09876
54321
Personally though I would not use tokenizer here and use Claudio's answer which splits the strings.

String split() over white spaces and "(" and ")"

I have a String
String testString = "IN NEWYORK AND (OUT FLORIDA)" ;
I want to split out this string in array Like :
String testArray[] = testString.split("\\s()");
I would like the result to be:
testArray[0] = "IN";
testArray[1] = "NEWYORK";
testArray[2] = "AND";
testArray[3] = "(";
testArray[4] = "OUT";
testArray[5] = "FLORIDA";
testArray[6] = ")";
However, the output I get is:
testArray[0] = "IN";
testArray[1] = "NEWYORK";
testArray[2] = "AND";
testArray[3] = "(OUT";
testArray[4] = "FLORIDA)";
It is splitting on white spaces but not on "(" and ")" , I want "(" and ")" to be as seperate strings .
Try the below:
String testArray[] = testString.split("\\s|(?<=\\()|(?=\\))");
split() requires a deleimeter to remove. Use StringTokenizer and instruct it to keep the delimiters.
StringTokenizer st = new StringTokenizer("IN NEWYORK AND (OUT FLORIDA)", " ()", true);
while (st.hasMoreTokens()) {
String t = st.nextToken();
if (!t.trim().equals("")) {
System.out.println(t);
}
}
If you want to do it with string split, then monstrous regexes like \s+|((?<=\()|(?=\())|((?<=\))|(?=\))) are pretty much inevitable. This regex is based on this question, btw, and it almost works.
Easiest way is to either surround parentheses with spaces as suggested by #acerisara or use StringTokenizer as suggested by #user1030723
String test = "IN NEWYORK AND (OUT FLORIDA)";
// this can for sure be done better, hope you get the idea
String a = test.replaceAll("(", "( ");
String b = a.replaceAll(")", " )";
String array[] = b.split("\\s");

Categories