Java: Strip everything before third slash - java

I'm currently in the progress of making a java application, one of the functions is showing related emails and documents.
But the full path to the email (on a sharepoint server) is displayed in the application, for obvious reasons the number of characters depends on the title of the email and the location.
But they all have the same in common, there are ALWAYS 3 slashes in the title.
Like this: Myserver/client/caseID/Title of Email here
Is it possible to get Java to "count" the number of slashes and just delete everything before the third slash?

Use the Split function to achieve this.
String value="Myserver/client/caseID/Title of Email here";
value=value.split("\\/")[3];
System.out.println("your value is "+value);

Here is an example:
String s = "Myserver/client/caseID/Title of Email here";
int i = s.lastIndexOf('/');
if (i != -1)
System.out.println(s.substring(i));
else
System.out.println("no slashes");

Using the replaceFirst function is one way to go:
String yourString = "Myserver/client/caseID/Title of Email here";
System.out.println(yourString.replaceFirst("([^/]+/){3}", ""));

try this
s = s.replaceFirst(".*?/.*?/.*?(?=/)", "");

Related

I want Bold text for my java coded Telegram bot, how?

I want to send bold text via a bot.
To send it as a normal person you would have to type 2 stars in front and behind the message, but this doesn't work for the bot. I have searched for a solution here but most bots are developed in PHP or Python.
`String a = emoji+"**dump alert**\n";
String b = "Date and time: ";
String c = month+" "+date.format(format1)+" / "+date.format(format2)+"\n";
String d = "Exchange: "+exc;
return a+b+c+d;`
For work with html tags in Text you need ON this options.
You can do it edit this flag:
message.enableHtml(true);
After this you can set bold text use this example:
String text = "<b>Bold text</b>";
When you work with Markdown you should use only one star *bold*
It should work with html tags. So instead of
String a = emoji+"**dump alert**\n";
try using this
String a = emoji+"<b>dump alert</b>\n";

Regular expression string in java

I want to explain my problem about a command string submitted from the client to the server. Through this specific command I have to login into the database managed by the server. According to the project's guide lines, command's formatting is: login#username;password. The string is sent to the server through a socket. User inserts his credentials and these are sent to the server putting them into the command. The server has to split the user and the password from the command and has to check, in the database, if the user already exists. Here is my problem, given the fact that in the user and in the password can be present ; (the same character of the separator), I don't understand (server side) how can i divide user and psw from the command so that user and psw are the same of the ones inserted in the client.
Have you got some ideas about? I was thinking to use regular expression, is this the right way?
I would just split the string into a user/pass string like this:
String userPass = "User;Pass";
String[] parts = userPass.split(";");
String user = parts[0];
String pass = parts[1];
If you really have to split a string by a separator included in both substrings there is no way to make sure the string is always split correctly!
I think you can use the unicode unit separator character 0x001F to separate you strings safely, as the user will have some difficulties entering such a character!
But depending on your application and string processing this could cause damage, too, as there are some issues concerning incompatibilities (e.g. xml 1.0 doesn't support it at all).
Otherwise if only one or none of the substrings may contain such a character you can easily use one of the already presented methods to split up the string and safely extract the data.
This will work only if User/password doesn't contain # or ;
String loginStr = "login#User;Password";
String []splittedLogin = loginStr.split("#");
String []loginCredentials = splittedLogin[1].split(";");
String user = loginCredentials[0];
String password = loginCredentials[1];
System.out.println(user);

Split string point delimited

I'm developing an app for M2M communication via SMS between the mobile phone and an electronic device which has a SIM card.
When I send a command from the mobile to the device, this device returns a confirmation string to the mobile which has this structure.
Confirmation OK:
DEV33456, 15, Date: yy/mm/dd-hh:mm:ss. OK
Confirmation ERROR:
DEV33456, 15, Date: yy/mm/dd-hh:mm:ss. ERROR
Now in my app I have to manage this message to get the relevant information. For example, the first part of the message is the identify code (DEV33456) so to get it I split the message:
String[] separated = message.split(",");
String ident = separated[0]; //DEV33456
But now, I need to get the last word (OK or ERROR) to determine if the message is OK or not. To do that I thought that the easiest way should be to split the message using the point before the OK or ERROR word. Is the only point in the entire message, so it should split it like this:
String[] separated = message.split(".");
String unused = separated[0]; //DEV33456, 15, Date: yy/mm/dd-hh:mm:ss
String error = separated[1]; //OK or ERROR
But it is throwing an ArrayIndexOutOfBoundsException and debuging it I see that after the first line where the separated string array should have a lenght of 2, it has a lenght of 0.
Is there any problem on spliting the string using a "."? I have done the same procedure to split it using a "," and it has done int correctly.
Java split uses regular expressions, . is a special character in regular expressions.
You need to escape it by using the string "\\.".
why not just use
message.endsWith("OK")
Check if this is want you want:
if(message.substring(message.length()-2, message.length())
.toLowerCase().contains("ok"))
Log.d("yes");
else
Log.d("no");
Hopefully it is.

Remove Invalid characters in an e-mail address

I'm trying to find out how to remove all invalid characters in an email address.
Ex: email="taeo͝';st#yy.com"(. is an email character) and the result should be: email = "taest#yy.com"
I'm using the following email pattern:
String email_pattern = "^[^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*#"+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$]";
String modifiedEmail = email.replaceAll(email_pattern,"");
But the above code is giving the result: email = "aest#yy.com" but expected "taest#yy.com"
Any suggestions or a better approach would be appreciated.
Here is a nice blog post of why you shouldn't filter your email adresses:
http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/
TL;DR: Check if there is an # (optionally a period) and send a test mail.
David suggests to use this regular expression:
/.+#.+\..+/
I got it resolved by using pattern matcher.
email = "testo͝';#.com.my"
String EMAIl_PATTERN = "[^a-zA-Z0-9!#$%&#'*+-/=?^_`{|}~.]+";
modifiedEmail = email.replaceAll(EMAIl_PATTERN, "");
Further thinking:
You could also be testing for know providers of email adresses used without authentication (e.g. http://trashmail.com/).

How to extract part of a domain from a string in Java?

If my domain is 31.example.appspot.com (App Engine prepends a version number), I can retrieve the domain from the Request object like this:
String domain = request.getServerName();
// domain == 31.example.appspot.com
What I want is to extract everything except the version number so I end up with two values:
String fullDomain; // example.appspot.com
String appName; // example
Since the domain could be anything from:
1.example.appspot.com
to:
31.example.appspot.com
How do I extract the fullDomain and appName values in Java?
Would a regex be appropriate here?
If you are always sure of this pattern, then just find the first dot and start from there.
fullDomain = domain.subString(domain.indexOf('.'));
UPDATE: after James and Sean comments, here is the full correct code:
int dotIndex = domain.indexOf(".")+1;
fullDomain = domain.substring(dotIndex);
appName = domain.substring(dotIndex,domain.indexOf(".",dotIndex));
Take a look at split method on java.lang.String.

Categories