How can I split a String using combine special characters?
For example, if the combine Special characters is {#}:
String str = "This is test string1.{#}This is test string2#(#*$ ((##{}";
StringTokenizer stoken = new StringTokenizer(str, "\\{\\#\\}");
while (stoken.hasMoreElements()) {
System.out.println(stoken.nextElement());
}
What I expect from above program is :
This is test string1.
This is test string2#(#*$ ((##{}
You can not use characters with special meanings like : \ ^ $ . | ? * + ( ) [ { }, i think there are 12 of them. therefore change your character to split the string to something like "/-/"
the code to do so looks like the one underneath:
public class SplitString {
public static void main(String[] args) {
String s = "This is test string1./This is test string2#(#*$ ((##{}";
String[] fragments = s.split("/");
String fragment1 = fragments[0];
String fragment2 = fragments[1];
System.out.println(fragment1);
System.out.println(fragment2);
}
}
this solution is based on the answer in this thread:
Stackoverflow Split String
Related
I am trying to add prefix and suffix to a particular occurrence of the word in the string in java. Can anyone help me and tell me where am i going wrong? Below is my code.
public static void main(String[] args) {
String str = "Hello world. welcome world java.";
String arr[] = str.split("[. ]");
if(str.contains("world")) {
System.out.println("PREFIX_"+str+"_SUFFIX");
}
}
output expected :
Hello PREFIX_world_SUFFIX. welcome PREFIX_world_SUFFIX java
output getting:
PREFIX_Hello world. welcome world java_SUFFIX
String replaced = str.replaceAll("world", "PREFIX_world_SUFFIX");
System.out.println(replaced);
Your code is wrong since you're not changing the str variable while calling the split() function.
Also, from what I can gather, you also want to add prefix and suffix to those words containing "world".
Like if your string is something like this: Hello worldJava! welcome to java world, you'd want to display something like this: Hello PREFIX_worldJava_SUFFIX! welcome to java PREFIX_world_SUFFIX. (Note, the previous answers wouldn't be able to do this kind of substitution).
String str = "Hello world. welcome world java.";
String[] wordArr = str.split("[. ]");
Set<String> words = new HashSet<>(Arrays.asList(wordArr));
for (String w: words) {
if(w.toLowerCase().contains("world")){
str = str.replace(w, "PREFIX_"+ w +"_SUFFIX");
}
}
System.out.println(str);
Note here that I am using java Set to parse unique words from the input string and then replacing them in the original string with the added prefix/suffix.
Just do this simply :
public class Example {
public static void main(String[] args){
String str = "Hello world. welcome world java.";
System.out.println(str.replace("world", "PREFIX_world_SUFFIX"));
}
}
Output :
Hello PREFIX_world_SUFFIX. welcome PREFIX_world_SUFFIX java.
You are doing it wrong on the print side. You can do this.
String stringToCheck = "world";
if(str.contains(stringToCheck)) {
str = str.replaceAll(stringToCheck , "PREFIX_"+stringToCheck+"_SUFFIX");
System.out.println(str);
}
I need to make a method that will retrieve words from the text without anything (punctuation etc.) except lowercase words themselves.
BUT I've struggled for 2 hours with regex pattern and faced such a problem.
There are words like "50-year" in the text.
And with my regex, output will be like:
-year
Instead of a normal
year
But I cannot replace dash symbol "-" cause there is another words with hyphen that should be left.
Here is a code:
public List<String> retrieveWordsFromFile() {
List<String> wordsFromText = new ArrayList<>();
scanner.useDelimiter("\\n+|\\s+|'");
while (scanner.hasNext()) {
wordsFromText.add(scanner.next()
.toLowerCase()
.replaceAll("^s$", "is")
.replaceAll("[^\\p{Lower}\\-]", "")
);
}
wordsFromText.removeIf(word -> word.equals(""));
return wordsFromText;
}
So how can I say that I need to replace everything except text and words with dash starting only with a letter/s. So this regex string should probably be such a "merged" into one sequence?
Use the regex, \\b[\\p{Lower}]+\\-[\\p{Lower}]+\\b|\\b[\\p{Lower}]+\\b
Demo:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
// Test strings
String[] arr = { "Hello world", "Hello world 123", "HELLO world", "50-year", "stack-overflow" };
// Define regex pattern
Pattern pattern = Pattern.compile("\\b[\\p{Lower}]+\\-[\\p{Lower}]+\\b|\\b[\\p{Lower}]+\\b");
for (String s : arr) {
// The string to be matched
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
// Matched string
String matchedStr = matcher.group();
// Display the matched string
System.out.println(matchedStr);
}
}
}
}
Output:
world
world
world
year
stack-overflow
Explanation of regex:
\b species the word boundary.
+ specifies one or more characters.
| specifies OR
This is how you can discard the non-matching text:
public class Main {
public static void main(String[] args) {
// Test strings
String[] arr = { "Hello world", "Hello world 123", "HELLO world", "50-year", "stack-overflow", "HELLO",
"HELLO WORLD", "&^*%", "hello", "123", "1w23" };
// Regex pattern
String regex = ".*?(\\b[\\p{Lower}]+\\-[\\p{Lower}]+\\b|\\b[\\p{Lower}]+\\b).*";
for (String s : arr) {
// Replace the string with group(1)
String str = s.replaceAll(regex, "$1");
// If the replaced string does not match the regex pattern, replace it with
// empty string
s = !str.matches(regex) ? "" : str;
// Display the replaced string if it is not empty
if (!s.isEmpty()) {
System.out.println(s);
}
}
}
}
Output:
world
world
world
year
stack-overflow
hello
Explanation of replacement:
.*? matches everything reluctantly i.e. before it yields to the next pattern.
s.replaceAll(regex, "$1") will replace s with group(1)
I usually don't ask for help but here I really need it.
I have the following code example:
String text = "aa aab aa aab";
text = text.replace("aa", "--");
System.out.println(text);
Console output: -- --b -- --b
I have a question, how do I only replace aa parts of the string not aab included.
So the console output is:
-- aab -- aab
I have another example:
String text = "111111111 1";
text = text.replace("1", "-");
System.out.println(text);
Console output: --------- -
I only want to replace a single character, not all the same ones who are placed together.
So the console output is:
111111111 -
Are there any Java shortcuts for situations like these? I can't figure it out, how to only replace specific part of the string. Any help would be appreciated :)
You could use a regular expression with String.replaceAll(String, String). By using word boundaries (\b), something like
String[] texts = { "aa aab aa aab", "111111111 1" };
String[] toReplace = { "aa", "1" };
String[] toReplaceWith = { "--", "-" };
for (int i = 0; i < texts.length; i++) {
String text = texts[i];
text = text.replaceAll("\\b" + toReplace[i] + "\\b", toReplaceWith[i]);
System.out.println(text);
}
Outputs (as requested)
-- aab -- aab
111111111 -
You can use a regex
String text = "111111111 1";
text = text.replaceAll("1(?=[^1]*$)", "");
System.out.println(text);
Explanation:
String.replaceAll takes a regex contrarily to String.replace which takes a litteral to replace
(?=reg) the right part of the regex must be followed by a string matching the regex reg, but only the right part will be captured
[^1]* means a sequence from 0 to any number of characters different from '1'
$ means the end of the string is reached
In plain english, this means: Please replace by an empty string all the occurrences of the '1' character followed by any number of characters different from '1' until the end of the string.
We can use the StringTokenizer present in Java to acheive the solution for any kind of input. Below is the sample solution,
public class StringTokenizerExample {
/**
* #param args
*/
public static void main(String[] args) {
String input = "aa aab aa aab";
String output = "";
String replaceWord = "aa";
String replaceWith = "--";
StringTokenizer st = new StringTokenizer(input," ");
System.out.println("Before Replace: "+input);
while (st.hasMoreElements()) {
String word = st.nextElement().toString();
if(word.equals(replaceWord)){
word = replaceWith;
if(st.hasMoreElements()){
word = " "+word+" ";
}else{
word = " "+word;
}
}
output = output+word;
}
System.out.println("After Replace: "+output);
}
I want to add escape character "'"(single quote) in string in java but only when there is odd number of occurrence using Regular Expression
For Ex:
if string is like "string's property" then output should be "string''s property"
if string is like "string''s property" then output should be "string''s property"
Try this :
\'(\')?
Demo (replacing with ')
http://regexr.com?38eeh
Try this code (even count).
public static void main(String[] args) {
String str = "a''''''b";
str = str.replaceAll("[^']'('')*[^']", "###");
System.out.println(str);
}
Then try this one (odd count).
public static void main(String[] args) {
String str = "a'''''''b";
str = str.replaceAll("[^']'('')*[^']", "###");
System.out.println(str);
}
Try this:
// input that will be replaced
String replace = "string's property";
// input that won't be replaced
String noReplace = "string''s property";
// String representation of the Pattern for both inputs
// |no single quote before...
// | |single quote
// | | |... no single quote after
String pattern = "(?<!')'(?!')";
// Will replace found text with main group twice --> found
System.out.println(replace.replaceAll(pattern, "$0$0"));
// Will replace found text with main group twice --> not found, no replacement
System.out.println(noReplace.replaceAll(pattern, "$0$0"));
Output:
string''s property
string''s property
Suppose I have following string:
String asd = "this is test ass this is test"
and I want to split the string using "ass" character sequence.
I used:
asd.split("ass");
It doesn't work. What do I need to do?
It seems to work fine for me:
public class Test
{
public static void main(String[] args) {
String asd = "this is test ass this is test";
String[] bits = asd.split("ass");
for (String bit : bits) {
System.out.println("'" + bit + "'");
}
}
}
Result:
'this is test '
' this is test'
Is your real delimiter different perhaps? Don't forget that split uses its parameter as a regular expression...
String asd = "this is test foo this is test";
String[] parts = asd.split("foo");
Try this it will work
public class Splitter {
public static void main(final String[] args) {
final String asd = "this is test ass this is test";
final String[] parts = asd.split("ass");
for (final String part : parts) {
System.out.println(part);
}
}
}
Prints:
this is test
this is test
Under Java 6. What output were you expecting?