Android: How to get file extension with multiple dots in filename? - file-extension

How can I get a file extension which has more than one dot in the name?
For example:
image.fromyesterday.hello.jpg
Thanks in advance!

String something = "image.fromyesterday.hello.jpg";
String extension = something.substring(something.lastIndexOf("."));

I think this would be better:
String suffixOf(String name){
if(name == null || name.equals("")){
return "";
}
String suffix = "";
int index = name.lastIndexOf(".");
if (index != -1 ) {
suffix = name.substring(index + 1);
}
return suffix;
}

Related

Program to Sort the String

I want to sort the String s = "eBaDcAfg153E" Such that the sorted string contains All lowercase first and then uppercase letters and then numbers.
The output should be like s = "acefgABDE135"
Can anyone help me with that?
Thanks
Welcome to stackoverflow!
Read how to ask good question, First try to solve, and if fail then first search over Google. and if you don't find answer, then you may ask.
This solution may work for you (just for test).. Still you can improve it a lot..
Use StringBuilder for string modification.
public static void main (String[] args) throws java.lang.Exception
{
String inputString = "eBaDcAfg153E";
String lowerCase = "";
String upperCase = "";
String numberCase = "";
for (int i = 0; i < inputString.length(); i++) {
char c = inputString.charAt(i);
if(Character.isUpperCase(c)) {
upperCase += c;
}else if (Character.isLowerCase(c)) {
lowerCase += c;
}else if(Character.isDigit(c)) {
numberCase += c;
}
}
char upperArray[] = upperCase.toCharArray();
char lowerArray[] = lowerCase.toCharArray();
char numArray[] = numberCase.toCharArray();
Arrays.sort(upperArray);
Arrays.sort(lowerArray);
Arrays.sort(numArray);
System.out.println(new String(lowerArray)+""+new String(upperArray)+""+new String(numArray));
}

XSSFCell in Apache POI encodes certain character sequences as unicode character

XSSFCell seems to encode certain character sequences as unicode characters. How can I prevent this? Do I need to apply some kind of character escaping?
e.g.
cell.setCellValue("LUS_BO_WP_x24B8_AI"); // The cell value now is „LUS_BO_WPⒸAI"
In Unicode Ⓒ is U+24B8
I've already tried setting an ANSI font and setting the cell type to string.
This character conversion is done in XSSFRichTextString.utfDecode()
I have now written a function that basicaly does the same thing in reverse.
private static final Pattern utfPtrn = Pattern.compile("_(x[0-9A-F]{4}_)");
private static final String UNICODE_CHARACTER_LOW_LINE = "_x005F_";
public static String escape(final String value) {
if(value == null) return null;
StringBuffer buf = new StringBuffer();
Matcher m = utfPtrn.matcher(value);
int idx = 0;
while(m.find()) {
int pos = m.start();
if( pos > idx) {
buf.append(value.substring(idx, pos));
}
buf.append(UNICODE_CHARACTER_LOW_LINE + m.group(1));
idx = m.end();
}
buf.append(value.substring(idx));
return buf.toString();
}
Based on what #matthias-gerth suggested with little adaptations:
Create your own XSSFRichTextString class
Adapt XSSFRichTextString.setString like this: st.setT(s); >> st.setT(escape(s));
Adapt the constructor of XSSFRichTextString like this: st.setT(str); >> st.setT(escape(str));
Add this stuff in XSSFRichTextString (which is very near to Matthias suggestion):
private static final Pattern PATTERN = Pattern.compile("_x[a-fA-F0-9]{4}");
private static final String UNICODE_CHARACTER_LOW_LINE = "_x005F";
private String escape(String str) {
if (str!=null) {
Matcher m = PATTERN.matcher(str);
if (m.find()) {
StringBuffer buf = new StringBuffer();
int idx = 0;
do {
int pos = m.start();
if( pos > idx) {
buf.append(str.substring(idx, pos));
}
buf.append(UNICODE_CHARACTER_LOW_LINE + m.group(0));
idx = m.end();
} while (m.find());
buf.append(str.substring(idx));
return buf.toString();
}
}
return str;
}

I have an expression for get property name. I want string of hole Expression as it is

<ReflectionTest>(x => x.ReflectionTestHelperClass1.ReflectionTestHelperClass2.InnerField)
When I am passing this expression, I want return "ReflectionTestHelperClass1.ReflectionTestHelperClass2.InnerField" as a string.
Can anybody help me?
(x => x.ReflectionTestHelperClass1.ReflectionTestHelperClass2.InnerField), this is Expression type you can use this expression to get string as you required.
public string GetExpresionBody(Expression expression)
{
var memberExpression = expression as MemberExpression;
if (memberExpression == null)
return string.Empty;
if (memberExpression.NodeType == ExpressionType.Parameter)
return string.Empty;
var classValue = GetExpresionBody(memberExpression.Expression);
var result = classValue + (string.IsNullOrWhiteSpace(classValue) ? "" : ".") + memberExpression.Member.Name;
return result;
}
try this...
private static string GetExpresion(Expression expression)
{
var memberExpression = expression as MemberExpression;
if (memberExpression == null)
return string.Empty;
if (memberExpression.NodeType == ExpressionType.Parameter)
return string.Empty;
var result = GetExpresion(memberExpression.Expression) + memberExpression.Member.Name;
// remove '.' from result which is comes first
return result;
}

'Strings' does not exist in the current context

I converted the following function from vb.net to c# but I cannot figure this out.
Error 4 The name 'Strings' does not exist in the current context
public string GetBetween(string StringText)
{
string functionReturnValue = null;
string TMP = null;
string FromS = null;
string ToS = null;
FromS = "<Modulus>";
ToS = "</Modulus>";
TMP = Strings.Mid(StringText, Strings.InStr(StringText, FromS) + Strings.Len(FromS), Strings.Len(StringText));
TMP = Strings.Left(TMP, Strings.InStr(TMP, ToS) - 1);
functionReturnValue = TMP;
return functionReturnValue;
}
Strings is a VB.net class. You'd have to reference the Microsoft.VisualBasic.dll assembly and use the Microsoft.VisualBasic namespace if you'd want to be able to use it.
It would be better if you just avoided using VB.net methods whenever possible.
public string GetBetween(string str, string start = "<Modulus>", string end = "</Modulus>")
{
var startIndex = str.IndexOf(start);
var endIndex = str.LastIndexOf(end);
if (startIndex == -1 || endIndex == -1 || startIndex > endIndex)
return str;
return str.Substring(startIndex + start.Length,
str.Length - start.Length - end.Length);
}
Add using Microsoft.VisualBasic; in the header

JavaME: Convert String to camelCase

What would be a simple implementation of a method to convert a String like "Hello there everyone" to "helloThereEveryone". In JavaME support for String and StringBuffer utility operations are quite limited.
Quick primitive implementation. I have no idea of restrictions of J2ME, so I hope it fits or it gives some ideas...
String str = "Hello, there, everyone?";
StringBuffer result = new StringBuffer(str.length());
String strl = str.toLowerCase();
boolean bMustCapitalize = false;
for (int i = 0; i < strl.length(); i++)
{
char c = strl.charAt(i);
if (c >= 'a' && c <= 'z')
{
if (bMustCapitalize)
{
result.append(strl.substring(i, i+1).toUpperCase());
bMustCapitalize = false;
}
else
{
result.append(c);
}
}
else
{
bMustCapitalize = true;
}
}
System.out.println(result);
You can replace the convoluted uppercase append with:
result.append((char) (c - 0x20));
although it might seem more hackish.
With CDC, you have:
String.getBytes();//to convert the string to an array of bytes
String.indexOf(int ch); //for locating the beginning of the words
String.trim();//to remove spaces
For lower/uppercase you need to add(subtract) 32.
With these elements, you can build your own method.
private static String toCamelCase(String s) {
String result = "";
String[] tokens = s.split("_"); // or whatever the divider is
for (int i = 0, L = tokens.length; i<L; i++) {
String token = tokens[i];
if (i==0) result = token.toLowerCase();
else
result += token.substring(0, 1).toUpperCase() +
token.substring(1, token.length()).toLowerCase();
}
return result;
}
Suggestion:
May be if you can port one regexp library on J2ME, you could use it to strip spaces in your String...
Try following code
public static String toCamel(String str) {
String rtn = str;
rtn = rtn.toLowerCase();
Matcher m = Pattern.compile("_([a-z]{1})").matcher(rtn);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, m.group(1).toUpperCase());
}
m.appendTail(sb);
rtn = sb.toString();
return rtn;
}
I would suggest the following simple code:
String camelCased = "";
String[] tokens = inputString.split("\\s");
for (int i = 0; i < tokens.length; i++) {
String token = tokens[i];
camelCased = camelCased + token.substring(0, 1).toUpperCase() + token.substring(1, token.length());
}
return camelCased;
I would do it like this:
private String toCamelCase(String s) {
StringBuffer sb = new StringBuffer();
String[] x = s.replaceAll("[^A-Za-z]", " ").replaceAll("\\s+", " ")
.trim().split(" ");
for (int i = 0; i < x.length; i++) {
if (i == 0) {
x[i] = x[i].toLowerCase();
} else {
String r = x[i].substring(1);
x[i] = String.valueOf(x[i].charAt(0)).toUpperCase() + r;
}
sb.append(x[i]);
}
return sb.toString();
}
check this
import org.apache.commons.lang.WordUtils;
String camel = WordUtils.capitalizeFully('I WANT TO BE A CAMEL', new char[]{' '});
return camel.replaceAll(" ", "");

Resources