Reputation: 4150
I have a File that COntains Strings in This Format:
ACHMU)][2:s,161,(ACH Payment Sys Menus - Online Services)][3:c,1,(M)][4:c,1,(N)]
ACLSICZ)][2:s,161,(Report for Auto Closure)][3:c,1,(U)][4:c,1,(N)]
ACMPS)][2:s,161,(Account Maintenance-Pre-shipment Account)][3:c,1,(U)][4:c,1,(N)]
ACNPAINT)][2:s,161,(Interest Run For NPA Accounts)][3:c,1,(U)][4:c,1,(N)]
I need to Split the String so that I have the data in this Format:
ACHMU (ACH Payment Sys Menus - Online Services)
ACLSICZ (Report for Auto Closure)......
Basically, I want to remove the ")[2:s,161," part and the "][3:c,1,(M)][4:c,1,(N)]" at the end. Will Splitting the String Help Me? The following Method has already failed:
FileInputStream fs = new FileInputStream(C:/Test.txt);
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
String str;
while((str = br.readLine()) != null){
String[] split = str.Split(")[2:s,161,")
}
Please Help me get the Junk in the middle and at the end.
Upvotes: 0
Views: 153
Reputation: 117607
The straight-forward way, use substring()
and indexOf()
:
String oldString = "ACHMU)][2:s,161,(ACH Payment Sys Menus - Online Services)][3:c,1,(M)][4:c,1,(N)]";
String firstPart = oldString.substring(0, oldString.indexOf(")")); // ACHMU
String secondPart = oldString.substring(oldString.indexOf("(")); // (ACH Payment Sys Menus - Online Services)][3:c,1,(M)]
String newString = firstPart + " " + secondPart.substring(0, secondPart.indexOf(")") + 1); // ACHMU (ACH Payment Sys Menus - Online Services)
System.out.print(newString);
OUTPUT:
ACHMU (ACH Payment Sys Menus - Online Services)
Upvotes: 3
Reputation:
FileInputStream fs = new FileInputStream(C:/Test.txt);
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
String str;
String newStr;
String completeNewStr="";
while((str = br.readLine()) != null)
{
newStr = str.replace(")][2:s,161,"," ");
newStr = str.replace("][3:c,1,(M)][4:c,1,(N)]","");
completeNewStr+=newStr;
}
// completeNewStr is your final string
Upvotes: 2
Reputation: 56819
You can use
str.replaceFirst("(.*?)\\)\\].*?(\\(.*?\\))\\].*", "$1 $2");
Upvotes: 2
Reputation: 4827
If the string that you want to replace is always "[2:s,161," , replace it with a empty string or space if that's acceptable. Similarly, for the other string as well.
str.replace("[2:s,161,", '');
Upvotes: 1