Android Copy String Like Delphi Copy's Function - android

Example 1:
i Have an Apple
Example 2:
I Love U
i would like to copy 10 string only, start index from first character.
Delphi code would be like this :
copy('I Have an Apple',0,10)
copy('I Love U',0,10)
Result become
i Have an
I Love U
Any same function in Android?
no method how long the string, i just want the first 10 character

You're probably after .substring(int start,int end) which applied to your code, would be something like the code below, however when you say you would like to copy "10 string only" you should say 10 characters from said string:
String apple = "i Have an Apple";
String appleCopy = apple.substring(0,10); // "i Have an "
If you want to handle the IndexOutOfBoundsException inline, you could do this, as suggested here, which would take the first n characters if the length is sufficient, or the whole string if its shorter.
String appleCopy = apple.substring(0, Math.min(apple.length(), 10));

Related

Custom Regular Expression in Java

I have to implement a function that check if a string is compliant to a regular expression, I have wrote a method that parse a list of filename, for each file name I need to check if respect the regexp.
The filename is composed like as follow (just an example):
verbale.pdf.001.001
image.jpg.002.001
The string is always composed by:
extension (only jpg or pdf) "." a group of three number "." a group of three number
With this regexp I need to check if the string in input end as described above, I have currently implemented this:
Pattern rexExp = Pattern.compile("((\\.jpg)|(\\.pdf))\\.[0-9]{3}\\.[0-9]{3}");
But not work properly, is it a good idea implement a regExp to check if a filename end with a certain path ?
Less greedy than the other answer, think it suits you:
\\w+\\.(jpg|pdf)(\\.\\d{3}){2}
file name, only composed of letters, numbers and _
dot
jpg or pdf formats
another dot
three digits
the dot and the three digits repeated
This should work :
.*\\w{3}\\.\\d{3}\\.\\d{3}
.* = any Characters (like "verbale123")
\\w{3} = any 3 alphabetic\numeric characters
\\. = a dot
\\d{3} = any three numeric characters
To check if a string ends with pdf or jpg and two sequences of . and 3 digits, you may use
(?i)(?:jpg|pdf)(?:\.[0-9]{3}){2}$
See the regex demo
Details
(?i) - case insensitive flag
(?:jpg|pdf) - either jpg or pdf
(?:\.[0-9]{3}){2} - 2 repetitions of a . and 3 digits
$ - end of string.
Use with Matcher#find() (as matches() anchors the match at the start and end of the string, while a partial match is required when using this pattern), example demo:
String s = "verbale.pdf.001.001";
Matcher matcher = Pattern.compile("(?i)(?:jpg|pdf)(?:\\.[0-9]{3}){2}$").matcher(s);
if (matcher.find()){
System.out.println("Valid!");
}

How to remove hyphen from TextUtils.split(line, "-")?

I found a dictionary sample in GitHub that I am currently experimenting with. The sample database used hyphen between the searched word and the word's meaning. So something like this.
abbey - n. a monastery ruled by an abbot
I looked into the dictionary database java file and found the following code:
String[] strings = TextUtils.split(line, "-");
I have my own database that translates Korean words to English. However I didn't use hyphen while creating it. So is there a way to not use hyphen or any other symbols but simply spaces? Also this is part of an android app.
Edit- An example of my own dictionary would be something like
abbey a monastery ruled by an abbot
Edit-
The problem here is that the old code only differentiates and recognizes the words and the meaning only if they are separated by hyphen. How do I make this so it works with spaces alone.
To remove a character in a String use String.replace
String newString = line.replace("-","");
To replace with a space simply use
String newString = line.replace("-"," ");
String mystring = mystring 1.replace("_"," "); if you want space give space.
As I understand it, you want to split your String to get the output like
abbey - n. a monastery ruled by an abbot
[abbey][n. a monastery ruled by an abbot]
You can use String.split(String, int) to force the number of split.
The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times
Let's use it like :
String[] array = s.split(" ", 2);
This will split your String on the regex " " but will limit the size of the output to 2 cells. So it will only split once, put the left part on the first cell and the right part on the second cell.
Without this limit argument, the method would keep split the right part again using a bigger array.
Note: this will be a problem if your word is a sentence in the left part.

single line alignment in textview [duplicate]

I have a very weird problem. After writing this:
for (File f : currentFile.listFiles()) {
if (f.isDirectory()){
System.out.println(f.getName()+"\t"+"Dir\t"+Command.getpremission(f)+"\t"+f.getTotalSpace());
}
else{
System.out.println(f.getName()+"\t"+"File\t"+Command.getpremission(f)+"\t"+f.getTotalSpace());
}
I see this printed:
see.txt File rw 267642728448
see1.txt File rw 267642728456
see2.txt File rw 267642728448
Why is there a problem with the tabs?
Building on this question, I use the following code to indent my messages:
String prefix1 = "short text:";
String prefix2 = "looooooooooooooong text:";
String msg = "indented";
/*
* The second string begins after 40 characters. The dash means that the
* first string is left-justified.
*/
String format = "%-40s%s%n";
System.out.printf(format, prefix1, msg);
System.out.printf(format, prefix2, msg);
This is the output:
short text: indented
looooooooooooooong text: indented
This is documented in section "Flag characters" in man 3 printf.
The "problem" with the tabs is that they indent the text to fixed tab positions, typically multiples of 4 or 8 characters (depending on the console or editor displaying them). Your first filename is 7 chars, so the next tab stop after its end is at position 8. Your subsequent filenames however are 8 chars long, so the next tab stop is at position 12.
If you want to ensure that columns get nicely indented at the same position, you need to take into account the actual length of previous columns, and either modify the number of following tabs, or pad with the required number of spaces instead. The latter can be achieved using e.g. System.out.printf with an appropriate format specification (e.g. "%1$13s" specifies a minimum width of 13 characters for displaying the first argument as a string).
The length of the text that you are providing in each line is different, this is the problem, so if the second word is too long (see2.txt is long 8 char which corresponds to a single tab lenght) it prints out a tab which goes to the next tabulation point.
One way to solve it is to programmatically add a pad to the f.getName() text so each text generated: see.txt or see2.txt has the same lenght (for example see.txt_ and see2.txt) so each tab automatically goes to the same tabulation point.
If you are developing with JDK 1.5 you can solve this using java.util.Formatter:
String format = "%-20s %5d\n";
System.out.format(format, "test", 1);
System.out.format(format, "test2", 20);
System.out.format(format, "test3", 5000);
this example will give you this print:
test 1
test2 20
test3 5000
In continuation of the comments by Péter and duncan, I normally use a quick padding method, something like -
public String rpad(String inStr, int finalLength)
{
return (inStr + " " // typically a sufficient length spaces string.
).substring(0, finalLength);
}
similarly you can have a lpad() as well
As mentioned by other folks, the variable length of the string is the issue.
Rather than reinventing the wheel, Apache Commons has a nice, clean solution for this in StringUtils.
StringUtils.rightPad("String to extend",100); //100 is the length you want to pad out to.
The problem is the length of the filenames. The first filename is only 7 chars long, so the tab occurs at char 8 (doing a tab after every 4 characters). However the next filenames are 8 chars long, so the next tab won't be until char 12. And if you had filenames longer than 11 chars, you'd run into the same problem again.
You can use this example to handle your problem:
System.out.printf( "%-15s %15s %n", "name", "lastname");
System.out.printf( "%-15s %15s %n", "Bill", "Smith");
You can play with the "%" until you find the right alignment to satisfy your needs
You can also pad a string to the required length using Guava's Strings.padEnd(String input, int minLength, char padding)

replace everything up to the last hyphen in android

How is it possible to replace everything of the string 20-30-55, so that I can get 55?
I tried var.replace("\\*-","")) but it didn't work.
I just need this in Android.
It sounds like you should look at the .split() function on a String. If you use
String arr[] = s.split("x");
then it will return you an array of all the bits of s, using x as the expression to use for splitting it up.
In your case, if you split on a hyphen, and look at the last element of the array, it'll give what you need.
You could also get what you want with
s.substring(s.lastIndexOf("-")+1);
This finds the last occurrence of a hyphen, and gives you the rest of the String from just after that point through to the end.

How to insert a new line in strings in Android

I'm creating an android application and within it there is a button that will send some information in an email, and I don't want to have everything all in one paragraph.
Here's what my app is doing for the putExtra for the email's body:
I am the first part of the info being emailed. I am the second part. I am the third part.
Here's what I want it to do:
I am the first part of the info being emailed.
I am the second part.
I am the third part.
How would I put a new line into a string or with the putExtra method to accomplish that?
Try:
String str = "my string \n my other string";
When printed you will get:
my string
my other string
Try using System.getProperty("line.separator") to get a new line.
I would personally prefer using "\n". This just puts a line break in Linux or Android.
For example,
String str = "I am the first part of the info being emailed.\nI am the second part.\n\nI am the third part.";
Output
I am the first part of the info being emailed.
I am the second part.
I am the third part.
A more generalized way would be to use,
System.getProperty("line.separator")
For example,
String str = "I am the first part of the info being emailed." + System.getProperty("line.separator") + "I am the second part." + System.getProperty("line.separator") + System.getProperty("line.separator") + "I am the third part.";
brings the same output as above. Here, the static getProperty() method of the System class can be used to get the "line.seperator" for the particular OS.
But this is not necessary at all, as the OS here is fixed, that is, Android. So, calling a method every time is a heavy and unnecessary operation.
Moreover, this also increases your code length and makes it look kind of messy. A "\n" is sweet and simple.
I use <br> in a CDATA tag.
As an example, my strings.xml file contains an item like this:
<item><![CDATA[<b>My name is John</b><br>Nice to meet you]]></item>
and prints
My name is John
Nice to meet you
If you want to add line break at runtime into a String from same string you are deriving the value then this Kotlin code works for me:
str = "<br>"+str?.replace("," , "</br><br>")+"</br>"
value = HtmlCompat.fromHtml(${skill_et_1}",Html.FROM_HTML_MODE_LEGACY)
tv.text = value

Categories

Resources