I'm trying to find a Java sub-string and then delete it without deleting the rest of the string.
I am taking XML as input and would like to delete a deprecated tag, so for instance:
public class whatever {
public static void main(String[] args) {
String uploadedXML = "<someStuff>Bats!</someStuff> <name></name>";
CharSequence deleteRaise = "<name>";
// If an Addenda exists we continue with the process
if (xml_in.contains(deleteRaise)){
// delete
} else {
// Carry on
}
}
In there I would like to delete the <name> and </name> tags if they are included in the string while leaving <someStuff> and </someStuff>.
I already parsed the XML to a String so there's no problem there. I need to know how to find the specific strings and delete them.
You can use replaceAll(regex, str) to do this. If you're not familiar with regex, the ? just means there can be 0 or 1 occurrences of / in the string, so it covers <name> and </name>
String uploadedXML = "<someStuff>Bats!</someStuff> <name></name>";
String filter = "</?name>";
uploadedXML = uploadedXML.replaceAll(filter, "");
System.out.println(uploadedXML);
<someStuff>Bats!</someStuff>
String uploadedXML = "<someStuff>Bats!</someStuff> <name></name>";
String deleteRaise = "<name>";
String closeName = "</name>"
// If an Addenda exists we continue with the process
if (xml_in.contains(deleteRaise)){
uploadedXML.replace(uploadedXML.substring(uploadedXML.indexOf(deleteRaise),uploadedXML.indexOf(closeName)+1),"");
} else {
// Carry on
}enter code here
Related
Below is my code to take a somewhat simple user input of hello or mouse and search a String(I will later use this on much larger text files).
I use it to create a Java line of code that uses the contains method. After, I need to evaluate it. I found some help using the Script engine, but I keep getting this error: Exception in thread "main" javax.script.ScriptException: ReferenceError: "name" is not defined in <eval> at line number 1 The output that comes from the code that I want to evaluate is name.contains("hello") || name.contains("mouse").
Any help would be great. Thanks
public static void main(String args[]) throws ScriptException {
String searchString = "name";
String terms = "(hello) || (mouse)";
String command = "";
//multiple commands
if(terms.contains("(") && terms.contains(")") && terms.contains("[") && terms.contains("]")){
command = terms.replace(")","\")");
command=command.replace("(", searchString + ".contains(\"");
command = command.replace("[","(");
command = command.replace("]",")");
}
//only ands/ only ors
else {
command = terms.replace("(", searchString + ".contains(\"");
command = command.replace(")", "\")");
}
System.out.println(command);
String name = "mousehellonamepe";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Object result = engine.eval(command);
System.out.println(result);
What are you trying to do exactly ?
======= Update ======
First, String terms should be a list.
Make it:
ArrayList<String> searchTerms = new ArrayList();
Add as many words as you like to that list as follows:
// using add() to initialize values
arr.add("Hello");
arr.add("Mouse");
arr.add("Bike");
arr.add("House");
// use contains() to check if the element
// 2 exits or not
boolean mouseExists = arr.contains("Mouse");
if (ans)
System.out.println("Our list contains the word mouse");
else
System.out.println("Our list does not contain the word mouse");
This should output this:
Our list contains the word mouse
Code should look like this:
public static void main(String args[]) throws ScriptException {
ArrayList<String> searchTerms = new ArrayList();
arr.add("Hello");
arr.add("Mouse");
arr.add("Bike");
arr.add("House");
boolean mouseExists = arr.contains("Mouse");
if (ans)
System.out.println("Our list contains the word mouse");
else
System.out.println("Our list does not contain the word mouse");
}
Interpreter i = new Interpreter();
i.set("searchThis", searchString);
i.set("answer", i.eval(command));
Boolean answer2 = (Boolean) (i.get("answer"));
http://www.beanshell.org/manual/quickstart.html#Calling_BeanShell_From_Your_Application
I used this to evaluate the command.
the link above provides all the explanation!
I have a string message to display which contains special characters but it doesnt print all. For example if I give message like "The P & A company does the work".
It prints only "The P".
public void setOutageMsg(String outageMsg) {
//outage msg issue
if(outageMsg==null){
this.outageMsg = outageMsg;
}
else{
outageMsg=outageMsg.replaceAll("&","&").replaceAll("&","%26");
this.outageMsg = outageMsg;
}
}
Similarly, I need to have a single code for all the special characters.
Try this, It may help you
outageMsg=outageMsg.replaceAll("&","&").replaceAll("&","\u0026");
or
outageMsg=outageMsg.replaceAll("&(?!amp;)","&").replaceAll("&","\u0026");
You can write something along these lines
public void setOutageMsg(String outageMsg) {
//outage msg issue
if(outageMsg==null){
this.outageMsg = outageMsg;
}
else{
this.outageMsg = getFormattedString(outageMsg);
}
}
private String getFormattedString(String outageMsg){
outageMsg.replaceAll("&","&");
outageMsg.replaceAll("a","b");
//...
return outageMsg;
}
Is it possible to parse a delimited file and find column datatypes? e.g
Delimited file:
Email,FirstName,DOB,Age,CreateDate
test#test1.com,Test User1,20/01/2001,24,23/02/2015 14:06:45
test#test2.com,Test User2,14/02/2001,24,23/02/2015 14:06:45
test#test3.com,Test User3,15/01/2001,24,23/02/2015 14:06:45
test#test4.com,Test User4,23/05/2001,24,23/02/2015 14:06:45
Output:
Email datatype: email
FirstName datatype: Text
DOB datatype: date
Age datatype: int
CreateDate datatype: Timestamp
The purpose of this is to read a delimited file and construct a table creation query on the fly and insert data into that table.
I tried using apache validator, I believe we need to parse the complete file in order to determine each column data type.
EDIT: The code that I've tried:
CSVReader csvReader = new CSVReader(new FileReader(fileName),',');
String[] row = null;
int[] colLength=(int[]) null;
int colCount = 0;
String[] colDataType = null;
String[] colHeaders = null;
String[] header = csvReader.readNext();
if (header != null) {
colCount = header.length;
}
colLength = new int[colCount];
colDataType = new String[colCount];
colHeaders = new String[colCount];
for (int i=0;i<colCount;i++){
colHeaders[i]=header[i];
}
int templength=0;
String tempType = null;
IntegerValidator intValidator = new IntegerValidator();
DateValidator dateValidator = new DateValidator();
TimeValidator timeValidator = new TimeValidator();
while((row = csvReader.readNext()) != null) {
for(int i=0;i<colCount;i++) {
templength = row[i].length();
colLength[i] = templength > colLength[i] ? templength : colLength[i];
if(colHeaders[i].equalsIgnoreCase("email")){
logger.info("Col "+i+" is Email");
} else if(intValidator.isValid(row[i])){
tempType="Integer";
logger.info("Col "+i+" is Integer");
} else if(timeValidator.isValid(row[i])){
tempType="Time";
logger.info("Col "+i+" is Time");
} else if(dateValidator.isValid(row[i])){
tempType="Date";
logger.info("Col "+i+" is Date");
} else {
tempType="Text";
logger.info("Col "+i+" is Text");
}
logger.info(row[i].length()+"");
}
Not sure if this is the best way of doing this, any pointers in the right direction would be of help
If you wish to write this yourself rather than use a third party library then probably the easiest mechanism is to define a regular expression for each data type and then check if all fields satisfy it. Here's some sample code to get you started (using Java 8).
public enum DataType {
DATETIME("dd/dd/dddd dd:dd:dd"),
DATE("dd/dd/dddd",
EMAIL("\\w+#\\w+"),
TEXT(".*");
private final Predicate<String> tester;
DateType(String regexp) {
tester = Pattern.compile(regexp).asPredicate();
}
public static Optional<DataType> getTypeOfField(String[] fieldValues) {
return Arrays.stream(values())
.filter(dt -> Arrays.stream(fieldValues).allMatch(dt.tester)
.findFirst();
}
}
Note that this relies on the order of the enum values (e.g. testing for datetime before date).
Yes it is possible and you do have to parse the entire file first. Have a set of rules for each data type. Iterate over every row in the column. Start of with every column having every data type and cancel of data types if a row in that column violates a rule of that data type. After iterating the column check what data type is left for the column. Eg. Lets say we have two data types integer and text... rules for integer... well it must only contain numbers 0-9 and may begin with '-'. Text can be anything.
Our column:
345
-1ab
123
The integer data type would be removed by the second row so it would be text. If row two was just -1 then you would be left with integer and text so it would be integer because text would never be removed as our rule says text can be anything... you dont have to check for text basically if you left with no other data type the answer is text. Hope this answers your question
I have slight similar kind of logic needed for my project. Searched lot but did not get right solution. For me i need to pass string object to the method that should return datatype of the obj. finally i found post from #sprinter, it looks similar to my logic but i need to pass string instead of string array.
Modified the code for my need and posted below.
public enum DataType {
DATE("dd/dd/dddd"),
EMAIL("#gmail"),
NUMBER("[0-9]+"),
STRING("^[A-Za-z0-9? ,_-]+$");
private final String regEx;
public String getRegEx() {
return regEx;
}
DataType(String regEx) {
this.regEx = regEx;
}
public static Optional<DataType> getTypeOfField(String str) {
return Arrays.stream(DataType.values())
.filter(dt -> {
return Pattern.compile(dt.getRegEx()).matcher(str).matches();
})
.findFirst();
}
}
For example:
Optional<DataType> dataType = getTypeOfField("Bharathiraja");
System.out.println(dataType);
System.out.println(dataType .get());
Output:
Optional[STRING]
STRING
Please note, regular exp pattern is vary based on requirements, so modify the pattern as per your need don't take as it is.
Happy Coding !
This question already has answers here:
How do I trim a file extension from a String in Java?
(23 answers)
Closed 9 years ago.
I got confused with substring in android. in my database i have file pdf like this DOGMATIKA-3.pdf and i want to select the "pdf". ho to do it in android? i just want to select 3 last letters , anyone please help me, thank you. i already try with this code but got force close.
package mobile.download;
public class DownloadText extends Activity{
public Koneksi linkurl;
public Kondownload linkurl2;
String url;
String SERVER_URL;
String SERVER_URL2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linkdownload);
TextView mTextLink = (TextView) findViewById(R.id.LinkDownload);
Bundle bundle = this.getIntent().getExtras();
String param1 = bundle.getString("keyIdc");
String param2 = bundle.getString("keyReference");
if(param2.substring(-3, 0).equals("pdf"))
{
linkurl = new Koneksi(this);
SERVER_URL = linkurl.getUrl();
SERVER_URL += "/moodledata/"+param1+"/"+param2;
mTextLink.setText(SERVER_URL);
Pattern pattern = Pattern.compile(SERVER_URL);
Linkify.addLinks(mTextLink, pattern, "");
}
else
{
linkurl2 = new Kondownload(param2);
SERVER_URL2 = linkurl2.getUrl();
mTextLink.setText(SERVER_URL2);
Pattern pattern = Pattern.compile(SERVER_URL2);
Linkify.addLinks(mTextLink, pattern, "");
}
}
}
last 3 letters are length() - 3 to length() (the second parameter is implicitely length(), so it is not necessary)
param2.substring(params2.length() - 3)
however, you could use endsWith which is clearer :
param2.endsWith("pdf")
which does exactly that.
try param2.substring(param2.indexOf("."), param2.length()).equals("pdf") instead..
If you are intent on using .substring(), use string.substring(string.length()-3).
However, you can also use the .split() method like so:
String [] split = string.split(".");
This will create a new array excluding all instances of "." and using them as the array separators. In other words, if you called this .split() on your above string, you would get
{"DOGMATIKA-3","pdf"}
The latter method will work for file extensions that are not three characters.
i have file pdf like this DOGMATIKA-3.pdf and i want to select the
"pdf"
String test = "myPdf.pdf";
String extension = test.substring(test.lastIndexOf(".")+1, test.length());
Or you can just do :
String extension = test.substring(test.lastIndexOf(".")+1);
just use like that
String substr = param2.substring(param2.length() - 3);
if("pdf".equals(substr))
{
// use what you want
}
i have a list of url's i need to filter specific domain and subdomain. say i have some domains like
http://www.example.com
http://test.example.com
http://test2.example.com
I need to extract urls which from domain example.com.
Working on project that required me to determine if two URLs are from the same sub domain (even when there are nested domains). I worked up a modification from the guide above. This holds out pretty well thus far:
public static boolean isOneSubdomainOfTheOther(String a, String b) {
try {
URL first = new URL(a);
String firstHost = first.getHost();
firstHost = firstHost.startsWith("www.") ? firstHost.substring(4) : firstHost;
URL second = new URL(b);
String secondHost = second.getHost();
secondHost = secondHost.startsWith("www.") ? secondHost.substring(4) : secondHost;
/*
Test if one is a substring of the other
*/
if (firstHost.contains(secondHost) || secondHost.contains(firstHost)) {
String[] firstPieces = firstHost.split("\\.");
String[] secondPieces = secondHost.split("\\.");
String[] longerHost = {""};
String[] shorterHost = {""};
if (firstPieces.length >= secondPieces.length) {
longerHost = firstPieces;
shorterHost = secondPieces;
} else {
longerHost = secondPieces;
shorterHost = firstPieces;
}
//int longLength = longURL.length;
int minLength = shorterHost.length;
int i = 1;
/*
Compare from the tail of both host and work backwards
*/
while (minLength > 0) {
String tail1 = longerHost[longerHost.length - i];
String tail2 = shorterHost[shorterHost.length - i];
if (tail1.equalsIgnoreCase(tail2)) {
//move up one place to the left
minLength--;
} else {
//domains do not match
return false;
}
i++;
}
if (minLength == 0) //shorter host exhausted. Is a sub domain
return true;
}
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
return false;
}
Figure I'd leave it here for future reference of a similar problem.
I understand you are probably looking for a fancy solution using URL class or something but it is not required. Simply think of a way to extract "example.com" from each of the urls.
Note: example.com is essentially a different domain than say example.net. Thus extracting just "example" is technically the wrong thing to do.
We can divide a sample url say:
http://sub.example.com/page1.html
Step 1: Split the url with delimiter " / " to extract the part containing the domain.
Each such part may be looked at in form of the following blocks (which may be empty)
[www][subdomain][basedomain]
Step 2: Discard "www" (if present). We are left with [subdomain][basedomain]
Step 3: Split the string with delimiter " . "
Step 4: Find the total number of strings generated from the split. If there are 2 strings, both of them are the target domain (example and com). If there are >=3 strings, get the last 3 strings. If the length of last string is 3, then the last 2 strings comprise the domain (example and com). If the length of last string is 2, then the last 3 strings comprise the domain (example and co and uk)
I think this should do the trick (I do hope this wasn't a homework :D )
//You may clean this method to make it more optimum / better
private String getRootDomain(String url){
String[] domainKeys = url.split("/")[2].split("\\.");
int length = domainKeys.length;
int dummy = domainKeys[0].equals("www")?1:0;
if(length-dummy == 2)
return domainKeys[length-2] + "." + domainKeys[length-1];
else{
if(domainKeys[length-1].length == 2) {
return domainKeys[length-3] + "." + domainKeys[length-2] + "." + domainKeys[length-1];
}
else{
return domainKeys[length-2] + "." + domainKeys[length-1];
}
}
}