putting space between integer and '/' using regex in java - java

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, " ");

Related

how to print white space character using string variable?

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 \\

If I want to append "\" in a string before every white space. how do i do that?

Suppose input String is Abc def ghi.. i want result to be like this Abc\ def\ ghi
Date=122
Shipper\ Id=11
Bill\ No=54433
Weight=431
Shipper\ Last\ Name=aaa
Shipper\ First\ Name=cdcx
You may try like this:
string s = "Abc" + "\\"+ "def" + "\\" + "ghi";
or you may try to replcae your space(" ") with \
string result = s.replaceAll(" ","\\ ");
"Abc def ghi".replaceAll("(?=\\s)", "\\\\")
Try this
String s= " Abc def gh";
System.out.println(s.replace(" ", "\\ "));
EDIT:
String s = " Abc def gh";
// System.out.println(s.replace(" ", "\\ "));
File f = new File("trail.properties");
f.createNewFile();
FileWriter fileWriter = new FileWriter(f);
fileWriter.write(s.replace(" ", "\\ "));
fileWriter.close();
Output:
\ Abc\ def\ gh
The \ need not be escaped in the replace string, thus
output = input.replaceAll("(?=\\s)", "\\")
Will do the trick.
Use Regular expression and replaceAll(String regex,String replacement)
\s matches every white Whitespace characters and+ matches the preceding pattern element one or more times.
So please try this:
public static void main(String[] args){
String s="Abc def ghi";
String output = s.replaceAll("\\s+", "\\\\");
System.out.println(output);// output :Abc\def\ghi
}
Using regex to match all whitespace \s and prepend \.
In the replacement pattern you need to escape the literal \ with another backslash: \\.
String result = searchText.replaceAll("(\\s)", "\\\$1");

Java regex replace values

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");

How to remove spaces in between the String

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

Replacing the string in android?

In my application I am displaying text from database in a textview. The text contains '\r\n'. So I replaced '\r\n' with empty i.e with ' '.
My code:
String myString = listItem.gettextdata().replace("\r\n", " ");
But still the text is displaying with \r\n....where I went wrong? Please help me regarding this....
Thanks in advance
Is this a case where you have to escape the backslash character, i.e.
String myString = listItem.gettextdata().replace("\\\\r\\\\n", " ");
Use code below :
YourString.replaceAll("\r\n", "");
What if you try a .toString() after gettestdate():
String myString = listItem.gettextdata().toString().replace("\r\n", " ");
Simply:
String str;
str = "hello\r\njava\r\nbook";
str = str.replaceAll("(\\r|\\n)", " ");
System.out.println(str);
Or
str = str.replaceAll("\\r\\n", " ");
You should use
listItem.getText().toString().replace...
try this:
String myString = listItem.gettextdata().replaceAll("[\r\n]", "");

Categories

Resources