Java remove unwanted spaces in a text file and replace with character - java

I have the following text file. I want to remove the lines and spaces so that the text file has a clear delimter to process. I cannot think of any way to remove the gaps between lines, is there a way?
Student+James Smith+Status: Current Student+Student+James Fits+Status: Not a current Student
Textfile
Student
James Smith
Status: Current Student
Student
James Fits
Status: Not a current Student

I know that this
a.replaceAll("\\s+","");
removes whitespaces.
You could remove end of line characters in a similar fashion
a.replaceAll("\n","");
Where 'a' is a String.

use a regex take the whole text in to a string and
string txt = "whole String";
String formatted = txt.replaceAll("[^A-Za-z0-9]", "-");
this will result in changing + sign and " " to replace with "-" sign. so now you have a specific deleimeter.

Something like find \s*\r?\n\s* replace +
Trims whitespace and adds delimiter '+'
Result:
Student+James Smith+Status: Current Student+Student+James Fits+Status: Not a current Student

Try using this one.
\n+\s*
just use it like this :
yourStrVar.replaceAll("\n+ *", "+")

Related

How can i make that everything after "," in .txt file will be checked separetly :)?

I need to improve this code that will read from a text file named file.txt
sergy,many,mani,kserder
I would like to use this form :
file = login.getText();
if (file.equals("sergy"))
I just need to do something that will read everything in the text file separately and ignoring "," sign, or something else other than "," sign.
You could split the string by the , character and check if any of the array's elements are equal to the value you're looking for:
file = login.getText();
if (Arrays.asList(file.split(",")).contains("sergy")) {
// do something...

Replace empty string with Non-Disclosed

I am a novice in Java so please pardon my inexperience. I have a column (source) like below which has empty strings and I am trying to replace it with Non-Disclosed.
Source
Website
Drive-by
Realtor
Social Media
Billboard
Word of Mouth
Visitor
I tried:
String replacedString = Source.replace("", "Non-Disclosed");
After running the above snippet, everything gets replaced by Non-Disclosed:
Non-Disclosed
Non-Disclosed
Non-Disclosed
............
How can I tackle this issue? Any assistance would be appreciated.
I think you simply have to do : Source.replace("\n\n", "\nNon-Disclosed\n")
I am assuming that your entire column is stored in one string.
In that case you can use ^$ regex to represent empty line (with MULTILINE flag (?m) which will allow ^ and $ to represent start and end of lines).
This approach
will work for many line separators \r \n \r\n
will not consume those line separators so we don't need to add them back in replacement part.
To use regex while replacing we can use replaceAll(regex, replacement) method
DEMO:
String text = "Source\r\n" +
"\r\n" +
"\r\n" +
"\r\n" +
"Website\r\n" +
"Drive-by\r\n" +
"Realtor\r\n" +
"Social Media\r\n" +
"\r\n" +
"Billboard\r\n" +
"\r\n" +
"Word of Mouth\r\n" +
"\r\n" +
"Visitor";
text = text.replaceAll("(?m)^$", "Non-Disclosed");
System.out.println(text);
Output:
Source
Non-Disclosed
Non-Disclosed
Non-Disclosed
Website
Drive-by
Realtor
Social Media
Non-Disclosed
Billboard
Non-Disclosed
Word of Mouth
Non-Disclosed
Visitor
You can use
String replacedString = Source.trim().isEmpty() ? "Non-Disclosed" : Source;
to replace only the "blank" String.

conditional replaceAll java

I have html code with img src tags pointing to urls. Some have mysite.com/myimage.png as src others have mysite.com/1234/12/12/myimage.png. I want to replace these urls with a cache file path. Im looking for something like this.
String website = "mysite.com"
String text = webContent.replaceAll(website+ "\\d{4}\\/\\d{2}\\/\\d{2}", String.valueOf(cacheDir));
This code however does not work when the url does not have the extra date stamp at the end. Does anyone know how i might achieve this? Thanks!
Try this one
mysite\.com/(\d{4}/\d{2}/\d{2}/)?
here ? means zero or more occurance
Note: use escape character \. for dot match because .(dot) is already used in regex
Sample code :
String[] webContents = new String[] { "mysite.com/myimage.png",
"mysite.com/1234/12/12/myimage.png" };
for (String webContent : webContents) {
String text = webContent.replaceAll("mysite\\.com/(\\d{4}/\\d{2}/\\d{2}/)?",
String.valueOf("mysite.com/abc/"));
System.out.println(text);
}
output:
mysite.com/abc/myimage.png
mysite.com/abc/myimage.png
You are missing a forward slash between the website.com and the first 4 digits.
String text = webContent.replaceAll(Pattern.quote(website) + "/\\d{4}\\/\\d{2}\\/\\d{2}", String.valueOf(cacheDir));
I'd also recommend using a literal for your website.com value (the Pattern.quote part).
Finally you are also missing the last forward slash after the last two digits so it won't be replaced, but that may be on purpose...
Try:
String text = webContent.replaceAll("(?<="+website+")(.*)(?=\\/)",
String.valueOf(cacheDir));

Replacing special character from a String in Android

I have a String as folder/File Name. I am creating folder , file with that string. This string may or may not contain some charters which may not allow to create desired folder or file
e.g
String folder = "ArslanFolder 20/01/2013";
So I want to remove these characters with "_"
Here are characters
private static final String ReservedChars = "|\?*<\":>+[]/'";
What will be the regular expression for that? I know replaceAll(); but I want to create a regular expression for that.
Use this code:
String folder = "ArslanFolder 20/01/2013 ? / '";
String result = folder.replaceAll("[|?*<\":>+\\[\\]/']", "_");
And the result would be:
ArslanFolder 20_01_2013 _ _ _
you didn't say that space should be replaced, so spaces are there... you could add it if it is necessary to be done.
I used one of this:
String alphaOnly = input.replaceAll("[^\\p{Alpha}]+","");
String alphaAndDigits = input.replaceAll("[^\\p{Alpha}\\p{Digit}]+","");
See this link:
Replace special characters
Try this :
replaceAll("[\\W]", "_");
It will replace all non alphanumeric characters with underscore
This is correct solution:
String result = inputString.replaceAll("[\\\\|?\u0000*<\":>+\\[\\]/']", "_");
Kent answer is good, but he isnt include characters NUL and \.
Also, this is a secure solution for replacing/renaming text of user-input file names, for example.

Replace backslashes in a string using Java/Groovy

Trying to get a simple string replace to work using a Groovy script. Tried various things, including escaping strings in various ways, but can't figure it out.
String file ="C:\\Test\\Test1\\Test2\\Test3\\"
String afile = file.toString() println
"original string: " + afile
afile.replace("\\\\", "/")
afile.replaceAll("\\\\", "/") println
"replaced string: " + afile
This code results in:
original string: C:\Test\Test1\Test2\Test3\
replaced string: C:\Test\Test1\Test2\Test3\
----------------------------
The answer, as inspired by Sorrow, looks like this:
// first, replace backslashes
String afile = file.toString().replaceAll("\\\\", "/")
// then, convert backslash to forward slash
String fixed = afile.replaceAll("//", "/")
replace returns a different string. In Java Strings cannot be modified, so you need to assign the result of replacing to something, and print that out.
String other = afile.replaceAll("\\\\", "/")
println "replaced string: " + other
Edited: as Neftas pointed in the comment, \ is a special character in regex and thus have to be escaped twice.
In Groovy you can't even write \\ - it is "an unsupported escape sequence". So, all answers I see here are incorrect.
If you mean one backslash, you should write \\\\. So, changing backslashes to normal slashes will look as:
scriptPath = scriptPath.replaceAll("\\\\", "/")
If you want to replace pair backslashes, you should double the effort:
scriptPath = scriptPath.replaceAll("\\\\\\\\", "/")
Those lines are successfully used in the Gradle/Groovy script I have intentionally launched just now once more - just to be sure.
What is even more funny, to show these necessary eight backslashes "\\\\\\\\" in the normal text here on StackOverflow, I have to use sixteen of them! Sorry, I won't show you these sixteen, for I would need 32! And it will never end...
If you're working with paths, you're better off using the java.io.File object. It will automatically convert the given path to the correct operating-system dependant path.
For example, (on Windows):
String path = "C:\\Test\\Test1\\Test2\\Test3\\";
// Prints C:\Test\Test1\Test2\Test3
System.out.println(new File(path).getAbsolutePath());
path = "/Test/Test1/Test2/Test3/";
// Prints C:\Test\Test1\Test2\Test3
System.out.println(new File(path).getAbsolutePath());
1) afile.replace(...) doesn't modify the string you're calling it on, it just returns a new string.
2) The input strings (String file ="C:\\Test\\Test1\\Test2\Test3\\";), from Java's perspective, only contain single backslashes. The first backslash is the escape character, then the second backslash tells it that you actually want a backslash.
so
afile.replace("\\\\", "/");
afile.replaceAll("\\\\", "/");
should be...
afile = afile.replace("\\", "/");
afile = afile.replaceAll("\\", "/");
In Groovy you can use regex in this way as well:
afile = afile.replaceAll(/(\\)/, "/")
println("replaced string: "+ afile)
Note that (as Sorrow said) replaceAll returns the result, doesn't modify the string. So you need to assign to a var before printing.
String Object is immutable so if you call a method on string object that modifies it. It will always return a new string object(modified). So you need to store the result return by replaceAll() method into a String object.
As found here, the best candidate might be the static Matcher method:
Matcher.quoteReplacement( ... )
According to my experiments this doubles single backslashes. Despite the method name... and despite the slightly cryptic Javadoc: "Slashes ('\') and dollar signs ('$') will be given no special meaning"

Categories

Resources