Remove specific words in a stringWhat is the difference between String and string in C#?How do I iterate over the words of a string?How do I read / convert an InputStream into a String in Java?Extract filename and extension in BashHow to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?Does Python have a string 'contains' substring method?How do I check if a string contains a specific word?How do I convert a String to an int in Java?Why is char[] preferred over String for passwords?
Why is participating in the European Parliamentary elections used as a threat?
GNU awk ERRNO not set on command failure
Given this phrasing in the lease, when should I pay my rent?
Do cause and effect exist in a non-relativistic, Newtonian Universe in which the speed of light is infinite?
Personal or impersonal in a technical resume
How to get directions in deep space?
"Oh no!" in Latin
PTIJ: Which Dr. Seuss books should one obtain?
Why would five hundred and five be same as one?
What is the Difference Between Formulating the Answer via Quadratic Formula and Factoring?
Typing CO_2 easily
How to convince somebody that he is fit for something else, but not this one?
Echo with obfuscation
In One Punch Man, is King actually weak?
How do I fix the group tension caused by my character stealing and possibly killing without provocation?
Do I have to know the General Relativity theory to understand the concept of inertial frame?
Is there a RAID 0 Equivalent for RAM?
Would this string work as string?
Word for (feeling) a really strong visceral impulse that draws you to something
Can you identify this lizard-like creature I observed in the UK?
Why can't the Brexit deadlock in the UK parliament be solved with a plurality vote?
What is the English pronunciation of pain au chocolat?
Difference between shutdown options
Should I warn new/prospective PhD Student that supervisor is terrible?
Remove specific words in a string
What is the difference between String and string in C#?How do I iterate over the words of a string?How do I read / convert an InputStream into a String in Java?Extract filename and extension in BashHow to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?Does Python have a string 'contains' substring method?How do I check if a string contains a specific word?How do I convert a String to an int in Java?Why is char[] preferred over String for passwords?
Given a string = "test20190906.pdf", how can I get only "test.pdf" such that it removes the date from the string by using string.replace or remove ?
Considering the format will always be filename + date + .extension.
java string
add a comment |
Given a string = "test20190906.pdf", how can I get only "test.pdf" such that it removes the date from the string by using string.replace or remove ?
Considering the format will always be filename + date + .extension.
java string
1
Consider mentioning the exact format of the date/time in the file name. That will help produce better solutions.
– Sibgha
1 hour ago
2
Is it just the date you want to remove or all numbers?
– Popeye
1 hour ago
add a comment |
Given a string = "test20190906.pdf", how can I get only "test.pdf" such that it removes the date from the string by using string.replace or remove ?
Considering the format will always be filename + date + .extension.
java string
Given a string = "test20190906.pdf", how can I get only "test.pdf" such that it removes the date from the string by using string.replace or remove ?
Considering the format will always be filename + date + .extension.
java string
java string
edited 49 mins ago
Viper629
asked 1 hour ago
Viper629Viper629
343
343
1
Consider mentioning the exact format of the date/time in the file name. That will help produce better solutions.
– Sibgha
1 hour ago
2
Is it just the date you want to remove or all numbers?
– Popeye
1 hour ago
add a comment |
1
Consider mentioning the exact format of the date/time in the file name. That will help produce better solutions.
– Sibgha
1 hour ago
2
Is it just the date you want to remove or all numbers?
– Popeye
1 hour ago
1
1
Consider mentioning the exact format of the date/time in the file name. That will help produce better solutions.
– Sibgha
1 hour ago
Consider mentioning the exact format of the date/time in the file name. That will help produce better solutions.
– Sibgha
1 hour ago
2
2
Is it just the date you want to remove or all numbers?
– Popeye
1 hour ago
Is it just the date you want to remove or all numbers?
– Popeye
1 hour ago
add a comment |
8 Answers
8
active
oldest
votes
You can use a regex to remove consecutive digits that resemble a date in any format provided the filename is appended immediately by the date.
"test20190906.pdf".replaceAll("[0-9]8", "");
2
what if the input String is "test2220190906"
– Amit Bera
1 hour ago
1
It works for that as well. Try it for yourself.
– Nicholas K
1 hour ago
The file name can contains numbers. In that case it is a wrong solution..
– Sibgha
1 hour ago
2
Your code will replace all the numbers no matter if it is a part of a date or not. When the input String iste2st220190906output istest.pdfbut I believe OP wants to remove only the date part.
– Amit Bera
1 hour ago
Hope the edit answers all your questions.
– Nicholas K
1 hour ago
add a comment |
I see previous answers and it does not work if you got something like this 01_test20190913.pdf
The solution will be
String file = "01_test20190913.pdf";
System.out.println(file.substring(0, file.length() - 12)+".pdf");
here i take the first part of string without last 12 characters and add ".pdf"
What if the file name is01_test20190913_01.pdf?
– Nicholas K
1 hour ago
By reading the question we can suppose the format is filename+date+.pdf, if is not we have to search "2019" and remove it + next 4 characters but it will work only for 2019 dates and it will not work if filename contains "2019"
– pavelbere
1 hour ago
Then we can also assume that the filename won't contain digits, isn't it?
– Nicholas K
1 hour ago
Maybe we can or maybe not only , but it's less probably, my answer just approach the question based on my assumptions and maybe can help someone in that particular case
– pavelbere
1 hour ago
add a comment |
Assuming the date contains only numbers, you can use regex to replace numbers, e.g.:
String fileNameWithDate = "test20190906.pdf";
String fileName = fileNameWithDate.replaceAll("[0-9]+", ""));
System.out.println(fileName);
2
what if the input String is "test2220190906"
– Amit Bera
1 hour ago
Yep, works fine for that too.
– Darshan Mehta
1 hour ago
he want to remove just a date, not all numbers
– pavelbere
1 hour ago
add a comment |
If the format of date is "yyyyMMdd" then I suggest go for the simplest solution as also given by @pavelbere. But this solution also assumes that the date always appends in the end of the filename.
String file = "test20190906.pdf";
String fileName = file.substring(0, file.length() - 12)+".pdf";
And if the file name istest2019090611.pdf?
– Nicholas K
1 hour ago
But this solution also assumes that the date always appends in the end of the filename.
– Sibgha
1 hour ago
add a comment |
If you want to remove the dates only from the String then you can take a look on the solution (Assuming the date will come just before the .pdf) :
String fileNameWithDate = "te3st320190906.pdf";
StringBuilder sb = new StringBuilder(fileNameWithDate).replace(fileNameWithDate.indexOf(".pdf") - 8,
fileNameWithDate.indexOf(".pdf"), "");
System.out.println(sb.toString());
Output:
te3st3.pdf
What if the file name istest112019090611.pdf? Also, what is the point of all the StringBuilder etc... when you end up doing.replaceAll("[0-9]+", "")in the end?
– Nicholas K
1 hour ago
Sorry I forgot to put that I have the assumption that date will come just before the.pdf.
– Amit Bera
1 hour ago
add a comment |
string name = "test20190906.pdf"
name.replaceAll("[0-9]","");
add a comment |
There are a lot of good answers, but I want present one more. It'll work if filename contains digits not only in date part. I assume that date is always appears before extension and has fixed length.
s.replaceAll("\d8.pdf", ".pdf");
And if the file extension varies then you could do some additional work:
public static String removeDate(String s)
final String extension = s.substring(s.lastIndexOf("."));
final String pattern = "\d8" + extension;
return s.replaceAll(pattern, extension);
public static void main(String args[])
System.out.println(removeDate("test20190101.pdf"));
System.out.println(removeDate("123123test20190101.txt"));
System.out.println(removeDate("123te11st20190101.csv"));
add a comment |
My approach would be to remove all numbers which are 8 digits long and are next to the last dot and replace them with a dot using the regex: (d8)(?!.*d.).
String filename = "filename12345678.pdf";
filename = filename.replaceAll("(\d8)(?!.*\d\.)\.", ".");
You can see this being used and an explanation of what it does here.
If the date can be different lengths then replace the 8 with a *, this enables the date to be any length.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55278652%2fremove-specific-words-in-a-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use a regex to remove consecutive digits that resemble a date in any format provided the filename is appended immediately by the date.
"test20190906.pdf".replaceAll("[0-9]8", "");
2
what if the input String is "test2220190906"
– Amit Bera
1 hour ago
1
It works for that as well. Try it for yourself.
– Nicholas K
1 hour ago
The file name can contains numbers. In that case it is a wrong solution..
– Sibgha
1 hour ago
2
Your code will replace all the numbers no matter if it is a part of a date or not. When the input String iste2st220190906output istest.pdfbut I believe OP wants to remove only the date part.
– Amit Bera
1 hour ago
Hope the edit answers all your questions.
– Nicholas K
1 hour ago
add a comment |
You can use a regex to remove consecutive digits that resemble a date in any format provided the filename is appended immediately by the date.
"test20190906.pdf".replaceAll("[0-9]8", "");
2
what if the input String is "test2220190906"
– Amit Bera
1 hour ago
1
It works for that as well. Try it for yourself.
– Nicholas K
1 hour ago
The file name can contains numbers. In that case it is a wrong solution..
– Sibgha
1 hour ago
2
Your code will replace all the numbers no matter if it is a part of a date or not. When the input String iste2st220190906output istest.pdfbut I believe OP wants to remove only the date part.
– Amit Bera
1 hour ago
Hope the edit answers all your questions.
– Nicholas K
1 hour ago
add a comment |
You can use a regex to remove consecutive digits that resemble a date in any format provided the filename is appended immediately by the date.
"test20190906.pdf".replaceAll("[0-9]8", "");
You can use a regex to remove consecutive digits that resemble a date in any format provided the filename is appended immediately by the date.
"test20190906.pdf".replaceAll("[0-9]8", "");
edited 1 hour ago
answered 1 hour ago
Nicholas KNicholas K
7,98361637
7,98361637
2
what if the input String is "test2220190906"
– Amit Bera
1 hour ago
1
It works for that as well. Try it for yourself.
– Nicholas K
1 hour ago
The file name can contains numbers. In that case it is a wrong solution..
– Sibgha
1 hour ago
2
Your code will replace all the numbers no matter if it is a part of a date or not. When the input String iste2st220190906output istest.pdfbut I believe OP wants to remove only the date part.
– Amit Bera
1 hour ago
Hope the edit answers all your questions.
– Nicholas K
1 hour ago
add a comment |
2
what if the input String is "test2220190906"
– Amit Bera
1 hour ago
1
It works for that as well. Try it for yourself.
– Nicholas K
1 hour ago
The file name can contains numbers. In that case it is a wrong solution..
– Sibgha
1 hour ago
2
Your code will replace all the numbers no matter if it is a part of a date or not. When the input String iste2st220190906output istest.pdfbut I believe OP wants to remove only the date part.
– Amit Bera
1 hour ago
Hope the edit answers all your questions.
– Nicholas K
1 hour ago
2
2
what if the input String is "test2220190906"
– Amit Bera
1 hour ago
what if the input String is "test2220190906"
– Amit Bera
1 hour ago
1
1
It works for that as well. Try it for yourself.
– Nicholas K
1 hour ago
It works for that as well. Try it for yourself.
– Nicholas K
1 hour ago
The file name can contains numbers. In that case it is a wrong solution..
– Sibgha
1 hour ago
The file name can contains numbers. In that case it is a wrong solution..
– Sibgha
1 hour ago
2
2
Your code will replace all the numbers no matter if it is a part of a date or not. When the input String is
te2st220190906 output is test.pdf but I believe OP wants to remove only the date part.– Amit Bera
1 hour ago
Your code will replace all the numbers no matter if it is a part of a date or not. When the input String is
te2st220190906 output is test.pdf but I believe OP wants to remove only the date part.– Amit Bera
1 hour ago
Hope the edit answers all your questions.
– Nicholas K
1 hour ago
Hope the edit answers all your questions.
– Nicholas K
1 hour ago
add a comment |
I see previous answers and it does not work if you got something like this 01_test20190913.pdf
The solution will be
String file = "01_test20190913.pdf";
System.out.println(file.substring(0, file.length() - 12)+".pdf");
here i take the first part of string without last 12 characters and add ".pdf"
What if the file name is01_test20190913_01.pdf?
– Nicholas K
1 hour ago
By reading the question we can suppose the format is filename+date+.pdf, if is not we have to search "2019" and remove it + next 4 characters but it will work only for 2019 dates and it will not work if filename contains "2019"
– pavelbere
1 hour ago
Then we can also assume that the filename won't contain digits, isn't it?
– Nicholas K
1 hour ago
Maybe we can or maybe not only , but it's less probably, my answer just approach the question based on my assumptions and maybe can help someone in that particular case
– pavelbere
1 hour ago
add a comment |
I see previous answers and it does not work if you got something like this 01_test20190913.pdf
The solution will be
String file = "01_test20190913.pdf";
System.out.println(file.substring(0, file.length() - 12)+".pdf");
here i take the first part of string without last 12 characters and add ".pdf"
What if the file name is01_test20190913_01.pdf?
– Nicholas K
1 hour ago
By reading the question we can suppose the format is filename+date+.pdf, if is not we have to search "2019" and remove it + next 4 characters but it will work only for 2019 dates and it will not work if filename contains "2019"
– pavelbere
1 hour ago
Then we can also assume that the filename won't contain digits, isn't it?
– Nicholas K
1 hour ago
Maybe we can or maybe not only , but it's less probably, my answer just approach the question based on my assumptions and maybe can help someone in that particular case
– pavelbere
1 hour ago
add a comment |
I see previous answers and it does not work if you got something like this 01_test20190913.pdf
The solution will be
String file = "01_test20190913.pdf";
System.out.println(file.substring(0, file.length() - 12)+".pdf");
here i take the first part of string without last 12 characters and add ".pdf"
I see previous answers and it does not work if you got something like this 01_test20190913.pdf
The solution will be
String file = "01_test20190913.pdf";
System.out.println(file.substring(0, file.length() - 12)+".pdf");
here i take the first part of string without last 12 characters and add ".pdf"
edited 1 hour ago
answered 1 hour ago
pavelberepavelbere
30719
30719
What if the file name is01_test20190913_01.pdf?
– Nicholas K
1 hour ago
By reading the question we can suppose the format is filename+date+.pdf, if is not we have to search "2019" and remove it + next 4 characters but it will work only for 2019 dates and it will not work if filename contains "2019"
– pavelbere
1 hour ago
Then we can also assume that the filename won't contain digits, isn't it?
– Nicholas K
1 hour ago
Maybe we can or maybe not only , but it's less probably, my answer just approach the question based on my assumptions and maybe can help someone in that particular case
– pavelbere
1 hour ago
add a comment |
What if the file name is01_test20190913_01.pdf?
– Nicholas K
1 hour ago
By reading the question we can suppose the format is filename+date+.pdf, if is not we have to search "2019" and remove it + next 4 characters but it will work only for 2019 dates and it will not work if filename contains "2019"
– pavelbere
1 hour ago
Then we can also assume that the filename won't contain digits, isn't it?
– Nicholas K
1 hour ago
Maybe we can or maybe not only , but it's less probably, my answer just approach the question based on my assumptions and maybe can help someone in that particular case
– pavelbere
1 hour ago
What if the file name is
01_test20190913_01.pdf?– Nicholas K
1 hour ago
What if the file name is
01_test20190913_01.pdf?– Nicholas K
1 hour ago
By reading the question we can suppose the format is filename+date+.pdf, if is not we have to search "2019" and remove it + next 4 characters but it will work only for 2019 dates and it will not work if filename contains "2019"
– pavelbere
1 hour ago
By reading the question we can suppose the format is filename+date+.pdf, if is not we have to search "2019" and remove it + next 4 characters but it will work only for 2019 dates and it will not work if filename contains "2019"
– pavelbere
1 hour ago
Then we can also assume that the filename won't contain digits, isn't it?
– Nicholas K
1 hour ago
Then we can also assume that the filename won't contain digits, isn't it?
– Nicholas K
1 hour ago
Maybe we can or maybe not only , but it's less probably, my answer just approach the question based on my assumptions and maybe can help someone in that particular case
– pavelbere
1 hour ago
Maybe we can or maybe not only , but it's less probably, my answer just approach the question based on my assumptions and maybe can help someone in that particular case
– pavelbere
1 hour ago
add a comment |
Assuming the date contains only numbers, you can use regex to replace numbers, e.g.:
String fileNameWithDate = "test20190906.pdf";
String fileName = fileNameWithDate.replaceAll("[0-9]+", ""));
System.out.println(fileName);
2
what if the input String is "test2220190906"
– Amit Bera
1 hour ago
Yep, works fine for that too.
– Darshan Mehta
1 hour ago
he want to remove just a date, not all numbers
– pavelbere
1 hour ago
add a comment |
Assuming the date contains only numbers, you can use regex to replace numbers, e.g.:
String fileNameWithDate = "test20190906.pdf";
String fileName = fileNameWithDate.replaceAll("[0-9]+", ""));
System.out.println(fileName);
2
what if the input String is "test2220190906"
– Amit Bera
1 hour ago
Yep, works fine for that too.
– Darshan Mehta
1 hour ago
he want to remove just a date, not all numbers
– pavelbere
1 hour ago
add a comment |
Assuming the date contains only numbers, you can use regex to replace numbers, e.g.:
String fileNameWithDate = "test20190906.pdf";
String fileName = fileNameWithDate.replaceAll("[0-9]+", ""));
System.out.println(fileName);
Assuming the date contains only numbers, you can use regex to replace numbers, e.g.:
String fileNameWithDate = "test20190906.pdf";
String fileName = fileNameWithDate.replaceAll("[0-9]+", ""));
System.out.println(fileName);
edited 1 hour ago
answered 1 hour ago
Darshan MehtaDarshan Mehta
23.4k32954
23.4k32954
2
what if the input String is "test2220190906"
– Amit Bera
1 hour ago
Yep, works fine for that too.
– Darshan Mehta
1 hour ago
he want to remove just a date, not all numbers
– pavelbere
1 hour ago
add a comment |
2
what if the input String is "test2220190906"
– Amit Bera
1 hour ago
Yep, works fine for that too.
– Darshan Mehta
1 hour ago
he want to remove just a date, not all numbers
– pavelbere
1 hour ago
2
2
what if the input String is "test2220190906"
– Amit Bera
1 hour ago
what if the input String is "test2220190906"
– Amit Bera
1 hour ago
Yep, works fine for that too.
– Darshan Mehta
1 hour ago
Yep, works fine for that too.
– Darshan Mehta
1 hour ago
he want to remove just a date, not all numbers
– pavelbere
1 hour ago
he want to remove just a date, not all numbers
– pavelbere
1 hour ago
add a comment |
If the format of date is "yyyyMMdd" then I suggest go for the simplest solution as also given by @pavelbere. But this solution also assumes that the date always appends in the end of the filename.
String file = "test20190906.pdf";
String fileName = file.substring(0, file.length() - 12)+".pdf";
And if the file name istest2019090611.pdf?
– Nicholas K
1 hour ago
But this solution also assumes that the date always appends in the end of the filename.
– Sibgha
1 hour ago
add a comment |
If the format of date is "yyyyMMdd" then I suggest go for the simplest solution as also given by @pavelbere. But this solution also assumes that the date always appends in the end of the filename.
String file = "test20190906.pdf";
String fileName = file.substring(0, file.length() - 12)+".pdf";
And if the file name istest2019090611.pdf?
– Nicholas K
1 hour ago
But this solution also assumes that the date always appends in the end of the filename.
– Sibgha
1 hour ago
add a comment |
If the format of date is "yyyyMMdd" then I suggest go for the simplest solution as also given by @pavelbere. But this solution also assumes that the date always appends in the end of the filename.
String file = "test20190906.pdf";
String fileName = file.substring(0, file.length() - 12)+".pdf";
If the format of date is "yyyyMMdd" then I suggest go for the simplest solution as also given by @pavelbere. But this solution also assumes that the date always appends in the end of the filename.
String file = "test20190906.pdf";
String fileName = file.substring(0, file.length() - 12)+".pdf";
answered 1 hour ago
SibghaSibgha
966
966
And if the file name istest2019090611.pdf?
– Nicholas K
1 hour ago
But this solution also assumes that the date always appends in the end of the filename.
– Sibgha
1 hour ago
add a comment |
And if the file name istest2019090611.pdf?
– Nicholas K
1 hour ago
But this solution also assumes that the date always appends in the end of the filename.
– Sibgha
1 hour ago
And if the file name is
test2019090611.pdf?– Nicholas K
1 hour ago
And if the file name is
test2019090611.pdf?– Nicholas K
1 hour ago
But this solution also assumes that the date always appends in the end of the filename.– Sibgha
1 hour ago
But this solution also assumes that the date always appends in the end of the filename.– Sibgha
1 hour ago
add a comment |
If you want to remove the dates only from the String then you can take a look on the solution (Assuming the date will come just before the .pdf) :
String fileNameWithDate = "te3st320190906.pdf";
StringBuilder sb = new StringBuilder(fileNameWithDate).replace(fileNameWithDate.indexOf(".pdf") - 8,
fileNameWithDate.indexOf(".pdf"), "");
System.out.println(sb.toString());
Output:
te3st3.pdf
What if the file name istest112019090611.pdf? Also, what is the point of all the StringBuilder etc... when you end up doing.replaceAll("[0-9]+", "")in the end?
– Nicholas K
1 hour ago
Sorry I forgot to put that I have the assumption that date will come just before the.pdf.
– Amit Bera
1 hour ago
add a comment |
If you want to remove the dates only from the String then you can take a look on the solution (Assuming the date will come just before the .pdf) :
String fileNameWithDate = "te3st320190906.pdf";
StringBuilder sb = new StringBuilder(fileNameWithDate).replace(fileNameWithDate.indexOf(".pdf") - 8,
fileNameWithDate.indexOf(".pdf"), "");
System.out.println(sb.toString());
Output:
te3st3.pdf
What if the file name istest112019090611.pdf? Also, what is the point of all the StringBuilder etc... when you end up doing.replaceAll("[0-9]+", "")in the end?
– Nicholas K
1 hour ago
Sorry I forgot to put that I have the assumption that date will come just before the.pdf.
– Amit Bera
1 hour ago
add a comment |
If you want to remove the dates only from the String then you can take a look on the solution (Assuming the date will come just before the .pdf) :
String fileNameWithDate = "te3st320190906.pdf";
StringBuilder sb = new StringBuilder(fileNameWithDate).replace(fileNameWithDate.indexOf(".pdf") - 8,
fileNameWithDate.indexOf(".pdf"), "");
System.out.println(sb.toString());
Output:
te3st3.pdf
If you want to remove the dates only from the String then you can take a look on the solution (Assuming the date will come just before the .pdf) :
String fileNameWithDate = "te3st320190906.pdf";
StringBuilder sb = new StringBuilder(fileNameWithDate).replace(fileNameWithDate.indexOf(".pdf") - 8,
fileNameWithDate.indexOf(".pdf"), "");
System.out.println(sb.toString());
Output:
te3st3.pdf
edited 1 hour ago
answered 1 hour ago
Amit BeraAmit Bera
4,1761630
4,1761630
What if the file name istest112019090611.pdf? Also, what is the point of all the StringBuilder etc... when you end up doing.replaceAll("[0-9]+", "")in the end?
– Nicholas K
1 hour ago
Sorry I forgot to put that I have the assumption that date will come just before the.pdf.
– Amit Bera
1 hour ago
add a comment |
What if the file name istest112019090611.pdf? Also, what is the point of all the StringBuilder etc... when you end up doing.replaceAll("[0-9]+", "")in the end?
– Nicholas K
1 hour ago
Sorry I forgot to put that I have the assumption that date will come just before the.pdf.
– Amit Bera
1 hour ago
What if the file name is
test112019090611.pdf? Also, what is the point of all the StringBuilder etc... when you end up doing .replaceAll("[0-9]+", "") in the end?– Nicholas K
1 hour ago
What if the file name is
test112019090611.pdf? Also, what is the point of all the StringBuilder etc... when you end up doing .replaceAll("[0-9]+", "") in the end?– Nicholas K
1 hour ago
Sorry I forgot to put that I have the assumption that date will come just before the
.pdf .– Amit Bera
1 hour ago
Sorry I forgot to put that I have the assumption that date will come just before the
.pdf .– Amit Bera
1 hour ago
add a comment |
string name = "test20190906.pdf"
name.replaceAll("[0-9]","");
add a comment |
string name = "test20190906.pdf"
name.replaceAll("[0-9]","");
add a comment |
string name = "test20190906.pdf"
name.replaceAll("[0-9]","");
string name = "test20190906.pdf"
name.replaceAll("[0-9]","");
answered 1 hour ago
pamithapamitha
3715
3715
add a comment |
add a comment |
There are a lot of good answers, but I want present one more. It'll work if filename contains digits not only in date part. I assume that date is always appears before extension and has fixed length.
s.replaceAll("\d8.pdf", ".pdf");
And if the file extension varies then you could do some additional work:
public static String removeDate(String s)
final String extension = s.substring(s.lastIndexOf("."));
final String pattern = "\d8" + extension;
return s.replaceAll(pattern, extension);
public static void main(String args[])
System.out.println(removeDate("test20190101.pdf"));
System.out.println(removeDate("123123test20190101.txt"));
System.out.println(removeDate("123te11st20190101.csv"));
add a comment |
There are a lot of good answers, but I want present one more. It'll work if filename contains digits not only in date part. I assume that date is always appears before extension and has fixed length.
s.replaceAll("\d8.pdf", ".pdf");
And if the file extension varies then you could do some additional work:
public static String removeDate(String s)
final String extension = s.substring(s.lastIndexOf("."));
final String pattern = "\d8" + extension;
return s.replaceAll(pattern, extension);
public static void main(String args[])
System.out.println(removeDate("test20190101.pdf"));
System.out.println(removeDate("123123test20190101.txt"));
System.out.println(removeDate("123te11st20190101.csv"));
add a comment |
There are a lot of good answers, but I want present one more. It'll work if filename contains digits not only in date part. I assume that date is always appears before extension and has fixed length.
s.replaceAll("\d8.pdf", ".pdf");
And if the file extension varies then you could do some additional work:
public static String removeDate(String s)
final String extension = s.substring(s.lastIndexOf("."));
final String pattern = "\d8" + extension;
return s.replaceAll(pattern, extension);
public static void main(String args[])
System.out.println(removeDate("test20190101.pdf"));
System.out.println(removeDate("123123test20190101.txt"));
System.out.println(removeDate("123te11st20190101.csv"));
There are a lot of good answers, but I want present one more. It'll work if filename contains digits not only in date part. I assume that date is always appears before extension and has fixed length.
s.replaceAll("\d8.pdf", ".pdf");
And if the file extension varies then you could do some additional work:
public static String removeDate(String s)
final String extension = s.substring(s.lastIndexOf("."));
final String pattern = "\d8" + extension;
return s.replaceAll(pattern, extension);
public static void main(String args[])
System.out.println(removeDate("test20190101.pdf"));
System.out.println(removeDate("123123test20190101.txt"));
System.out.println(removeDate("123te11st20190101.csv"));
edited 1 hour ago
answered 1 hour ago
Mikhail IlinykhMikhail Ilinykh
37829
37829
add a comment |
add a comment |
My approach would be to remove all numbers which are 8 digits long and are next to the last dot and replace them with a dot using the regex: (d8)(?!.*d.).
String filename = "filename12345678.pdf";
filename = filename.replaceAll("(\d8)(?!.*\d\.)\.", ".");
You can see this being used and an explanation of what it does here.
If the date can be different lengths then replace the 8 with a *, this enables the date to be any length.
add a comment |
My approach would be to remove all numbers which are 8 digits long and are next to the last dot and replace them with a dot using the regex: (d8)(?!.*d.).
String filename = "filename12345678.pdf";
filename = filename.replaceAll("(\d8)(?!.*\d\.)\.", ".");
You can see this being used and an explanation of what it does here.
If the date can be different lengths then replace the 8 with a *, this enables the date to be any length.
add a comment |
My approach would be to remove all numbers which are 8 digits long and are next to the last dot and replace them with a dot using the regex: (d8)(?!.*d.).
String filename = "filename12345678.pdf";
filename = filename.replaceAll("(\d8)(?!.*\d\.)\.", ".");
You can see this being used and an explanation of what it does here.
If the date can be different lengths then replace the 8 with a *, this enables the date to be any length.
My approach would be to remove all numbers which are 8 digits long and are next to the last dot and replace them with a dot using the regex: (d8)(?!.*d.).
String filename = "filename12345678.pdf";
filename = filename.replaceAll("(\d8)(?!.*\d\.)\.", ".");
You can see this being used and an explanation of what it does here.
If the date can be different lengths then replace the 8 with a *, this enables the date to be any length.
edited 34 mins ago
answered 39 mins ago
Toby SmithToby Smith
4181715
4181715
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55278652%2fremove-specific-words-in-a-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Consider mentioning the exact format of the date/time in the file name. That will help produce better solutions.
– Sibgha
1 hour ago
2
Is it just the date you want to remove or all numbers?
– Popeye
1 hour ago