if String s="\t\n";
then I want to print s and result will be : "\t\n" .
Example:
String s = "\t\n";
System.out.println(s);
Output: \t\n
Is there any way to do this?
Simply use str.replace():
String s = "\n\t";
s = s.replace("\n", "\\n");
s = s.replace("\t", "\\t");
System.out.println(s);
Output: \n\t
you can use "\\n\\t" to do so.Since \n is the new line character and \t is the tab. But if you need to print \ you need to use \\
Related
I have a String S= "grep -iRl 2020062312213461801003087/var/seamless/spool/tdr/Reconciliation"
and i want to add space between last number and "/"
like i want my output to be grep -iRl 2020062312213461801003087 /var/seamless/spool/tdr/Reconciliation
Try this.
String S= "grep -iRl 2020062312213461801003087/var/seamless/spool/tdr/Reconciliation";
String replaced = S.replaceAll("(\\d)(/)", "$1 $2");
System.out.println(replaced);
result:
grep -iRl 2020062312213461801003087 /var/seamless/spool/tdr/Reconciliation
you can find index of first "/" by yourstring.indexOf("/"); . and insert char to string by yourString.insert(index of "/", " ");.
your code will be like that
`
int index = yourString.indexOf("/");
yourString.insert(index, " ");
I'm trying to split a string in Java, but keep the newline characters as elements in the array.
For example, with input: "Hello \n\n\nworld!"
I want the output to be: ["Hello", "\n", "\n", "\n", "world", "!"]
The regex I have in place right now is this:
String[] parsed = input.split(" +|(?=\\p{Punct})|(?<=\\p{Punct})");
This gets me the punctuation separation I want, but its output looks like this:["Hello", "\n\n\nworld", "!"]
Is there a way to unclump the newlines in Java?
You could first replace all \n with \n (newline and a space) and then do a simple split on the space character.
String input = "Hello \n\n\nworld!";
String replacement = input.replace("\n", "\n ");
String[] result = replacement.split(" ");
input: "Hello \n\n\nworld!"
replacement: "Hello \n \n \n world!"
result: ["Hello", "\n", "\n", "\n", "world!"]
Note: my example does not handle the final exclamation mark - but it seems you already know how to handle that.
The trick is to add whitespace after each "\n" and then apply your regex.
String line = "Hello \n\n\nworld!";
line = line.replaceAll("\n", "\n "); // here we replace all "\n" to "\n "
String[] items = line.split(" +|(?=\\p{Punct})|(?<=\\p{Punct})");
or shorter version:
String line = "Hello \n\n\nworld!";
String[] items = line.replaceAll("\n", "\n ").split(" +|(?=\\p{Punct})|(?<=\\p{Punct})");
So, in this context the result is: ["Hello", "\n", "\n", "\n", "world", "!"]
Using the find method makes things easier:
String str = "Hello \n\n\nworld!";
List<String> myList = new ArrayList<String>();
Pattern pat = Pattern.compile("\\w+|\\H");
Matcher m = pat.matcher(str);
while (m.find()) {
myList.add(m.group(0));
}
If you use Java 7, change \\H to [\\S\\n].
Note that using this approach, you obtain a pattern easier to write and to edit since you don't need to use lookarounds.
I am trying to replace certain values from a string using java regex
for example the string looks like
:20:1234
6789
:28G::xyz
|20:3456
1234
|29C:pqr
:20|9876
I want to replace tag 20 value (may be multi line value) for second occurrence
|20:3456
1234
with new value(may be multi line value) 6789 so the final replacement string i am expecting is
:20:1234
6789
:28G::xyz
|20:6789
|29C:pqr
:20|9876
Try this regex:
String str = ":20:1234\n 6789\n:28G::xyz\n|20:3456\n 1234\n|29C:pqr\n:20|9876 \n|20:3456\n :20:1234\n";
str = str.replaceAll("(\\|20:)[\\s\\S]*?(?=[|:])","$1" + "6789\n");
Here it is checking until it reaches to anything other than | or :, so that it doesn't pick all.
This should work (tested):
str.replaceAll("(\\|" + "20" + ":)[^|:]*\n","$1" + "6789" + "\n");
I have below String
string = "Book Your Domain And Get\n \n\n \n \n \n Online Today."
string = str.replace("\\s","").trim();
which returning
str = "Book Your Domain And Get Online Today."
But what is want is
str = "Book Your Domain And Get Online Today."
I have tried Many Regular Expression and also googled but got no luck. and did't find related question, Please Help, Many Thanks in Advance
Use \\s+ instead of \\s as there are two or more consecutive whitespaces in your input.
string = str.replaceAll("\\s+"," ")
You can use replaceAll which takes a regex as parameter. And it seems like you want to replace multiple spaces with a single space. You can do it like this:
string = str.replaceAll("\\s{2,}"," ");
It will replace 2 or more consecutive whitespaces with a single whitespace.
First get rid of multiple spaces:
String after = before.trim().replaceAll(" +", " ");
If you want to just remove the white space between 2 words or characters and not at the end of string
then here is the
regex that i have used,
String s = " N OR 15 2 ";
Pattern pattern = Pattern.compile("[a-zA-Z0-9]\\s+[a-zA-Z0-9]", Pattern.CASE_INSENSITIVE);
Matcher m = pattern.matcher(s);
while(m.find()){
String replacestr = "";
int i = m.start();
while(i<m.end()){
replacestr = replacestr + s.charAt(i);
i++;
}
m = pattern.matcher(s);
}
System.out.println(s);
it will only remove the space between characters or words not spaces at the ends
and the output is
NOR152
Eg. to remove space between words in a string:
String example = "Interactive Resource";
System.out.println("Without space string: "+ example.replaceAll("\\s",""));
Output:
Without space string: InteractiveResource
If you want to print a String without space, just add the argument sep='' to the print function, since this argument's default value is " ".
//user this for removing all the whitespaces from a given string for example a =" 1 2 3 4"
//output: 1234
a.replaceAll("\\s", "")
String s2=" 1 2 3 4 5 ";
String after=s2.replace(" ", "");
this work for me
String string_a = "AAAA BBB";
String actualTooltip_3 = string_a.replaceAll("\\s{2,}"," ");
System.out.println(String actualTooltip_3);
OUTPUT will be:AAA BBB
I am using java replaceAll() method to escape new line characters
String comment = "ddnfa \n \r \tdnfadsf ' \r t ";
comment = comment.replaceAll("(\\n|\\r|\\t)","\\\\$1");
System.out.println(comment);
But the above code is still inserting new line.
Is there a way to output the comment exactly the same (i.e. with \n and \r instead of inserting new line)?
UPDATE:
I ended up using:
comment = comment.replaceAll("\\n","\\\\n")
.replaceAll("\\r","\\\\r")
.replaceAll("\\t","\\\\t");
You'll have to go one-by-one, since the new-line character U+000A has nothing to do with the two-character escape sequence \n:
comment = comment.replaceAll("\n","\\\\n");
comment = comment.replaceAll("\r","\\\\r");
comment = comment.replaceAll("\t","\\\\t");
you will have to do it character by character:
comment = comment.replaceAll("\n","\\\\n");
comment = comment.replaceAll("\r","\\\\r");
comment = comment.replaceAll("\t","\\\\t");
another solution is to escape the String as a Java String using this function:
comment = org.apache.commons.lang.StringEscapeUtils.escapeJava(comment);
This will make the String look exactly like the String in the Java Code, but it will also show other escape sequences (like \\, \" etc).
But maybe thats exactly what you want
Hard way: using Matcher
String comment = "ddnfa \n \r \tdnfadsf ' \r t ";
Map<String,String> sub = new HashMap<String,String>();
sub.put("\n", "\\\\n");
sub.put("\r", "\\\\r");
sub.put("\t", "\\\\t");
StringBuffer result = new StringBuffer();
Pattern regex = Pattern.compile("\\n|\\r|\\t");
Matcher matcher = regex.matcher(comment);
while (matcher.find()) {
matcher.appendReplacement(result, sub.get(matcher.group()));
}
matcher.appendTail(result);
System.out.println(result.toString());
prints
ddnfa \n \r \tdnfadsf ' \r
Why you dont use Matcher.quoteReplacement(stringToBeReplaced);?
It is a \ problem, simplify like this :
comment = comment.replaceAll("(\n|\r|\t)", "");
output :
ddnfa dnfadsf ' t
Try this..
comment.replaceAll("(\n)|(\r)|(\t)", "\n");