0

I have a

String name = " abc_efg_the_xyz_24_Mar_2016.csv "; 

I would like to replace 24_Mar_2016 with "18_Jan_2016". So that the new string becomes:

"abc_efg_the_xyz_18_Jan_2016.csv" 

How can I do it?

Also the problem is that the date part in the string name will always keep changing so I can't do pattern search and replace with new date.

Please help

Nurjan
  • 5,184
  • 5
  • 30
  • 47
Sonam Varma
  • 59
  • 1
  • 4
  • Maybe you should substring the file like always take the X first char then add your new date. `var actualFileName = "xxx_xxx_date.csv"; var tmp = actualFileName.substring(0,7); //not sure if it's like that var final = tmp + "yournewDate" + ".csv";` – Jerome Dec 22 '16 at 10:09
  • What do you mean with " will always keep changing ", will it always be on the last part of the file name or not ? – AMB Dec 22 '16 at 10:10
  • Do u hear about `regular expressions`? Although, in your case when you know a substring position from start or end of a string you can just replace it by indices. – Шах Dec 22 '16 at 10:14
  • I dont get your request. Of course you don't **have** to put your strings hardcoded into patterns. You can build a pattern from a string that you pulled to together at runtime. – GhostCat Dec 22 '16 at 10:18
  • And then, the other thing: what have you tried so far yourself? – GhostCat Dec 22 '16 at 10:18
  • In my case, I will be reading the string name from a file and replace it with a value which comes from another file. So I cant hardcode anything in my code. Finally , this is what I wront and its working for me for now. – Sonam Varma Dec 23 '16 at 03:13
  • In my case, I will be reading the string name from a file and replace it with a value which comes from another file. So I cant hardcode anything in my code. Finally , this is what I wrote and its working for me for now. String checkDate = "23_DEC_2016"; String separate = StringUtils.substringBefore(name, "."); String inProcess = separate.substring(0,separate.length()-11); String finalName = inProcess+checkDate+".csv"; – Sonam Varma Dec 23 '16 at 03:14

5 Answers5

3

You're asking two things here. First question is answered by:

String name = " abc_efg_the_xyz_24_Mar_2016.csv ";
String olddate = "24_Mar_2016";
String newdate = "18_Jan_2016";

// use replaceAll() if there will be multiple occurences the date
String replaced = name.replace(olddate, newdate);

The second question requires a regular expression matching the date format used by your naming convention.

oschlueter
  • 2,248
  • 1
  • 16
  • 38
2

This could be done using Pattern and Matcher to cut off the old date.

But I think you will create a new file anyway and then you will also know the prefix to use before the date. So IMHO there is no need to "change" an old file name...

Timothy Truckle
  • 12,232
  • 2
  • 22
  • 44
  • Offtopic: thanks for your edit on that junit answer of mine. I further enhanced it, but I like your idea there. – GhostCat Dec 22 '16 at 10:31
2

Assuming your date has the following convention all the time: dd_mmm_yyyy and that the name of the file has ONE date only. You can use String.replaceAll(..) or String.replaceFirst(..) and regex to solve this problem. Read more about regex here.

String replaceDate = "12_Feb_2017";
String name = "abc_efg_the_xyz_24_Mar_2016.csv ";

name = name.replaceAll("\\d{2}_[a-zA-Z]{3}_\\d{4}", replaceDate);

Here is an ideone demo

Community
  • 1
  • 1
denvercoder9
  • 2,764
  • 3
  • 24
  • 38
1

One of the ways:

String name = " abc_efg_the_xyz_24_Mar_2016.csv ";
name = name.replace("24_Mar_2016", "18_Jan_2016"); 
Nurjan
  • 5,184
  • 5
  • 30
  • 47
1

I assume that you have always same date format and the location of the date string in your name, but the date value in your name could be different.

String name = "abc_efg_the_xyz_24_Mar_2016.csv";
System.out.println(name.replaceFirst(".{11}(?=[.][^.]+$)","Happy_NEW_YEAR"));

This will print:

abc_efg_the_xyz_Happy_NEW_YEAR.csv
  • The ".{11}(?=[.][^.]+$)" is regex, to match the 11 chars before the last period (.), in your case, it matches the date format xx_xxx_xxxx (length:11)

  • the replaceFirst will only do substitution once. I didn't use pattern \d{2}_\w{3}_\d{4}, because I see you have other abc_efg... in your string, I am not sure if there could be the same matched pattern, which you don't want to replace.

Kent
  • 173,042
  • 30
  • 210
  • 270