String Overlay in Android - java

is there anyway of creating a String overlay? Because I'm working on a project which I show the spots in the map, but I wanna also show some String identifing the spot, like an id, for example
att,
Andre Mariano

Use StringBuilder() instead of String. Example..
StringBuilder sb = new StringBuilder();
sb.append("some string");
sb.append("more string");
Toast.makeText(this, sb, Toast.LENGTH_LONG).show();

Related

How to create text into Html using Java

How can i convert the sentence into html using java program. Suppose if i have bold character or underline or superscript words while creating those how can i add html tag conditionally before and after that particular words.
What i think is read the excel file and then using string builder trying to append the whole sentence into p tag.
As i am very new into it . I dont understand how to iteratre through each word and check for various conditions
what i tried is this it cjust add the p tag in start and end , but does convert help in bold and specific in bold and underline
public String getGeneralValue(Cell currentCell){
switch (currentCell.getCellType()){
case STRING:
return currentCell.getStringCellValue();
case NUMERIC:
return String.valueOf((int) currentCell.getNumericCellValue());
case BOOLEAN:
return String.valueOf(currentCell.getBooleanCellValue()).toUpperCase();
default:system.out.println("other than numeric and String");
}
return "";
}
public String setPType(Cell currentCell){
StringBuilder sb = new StringBuilder();
sb.append("<p>");
sb.append(getGeneralValue(currentCell));
sb.append("</p>");
return sb.toString();
}
Here is the image an example

Csv: search for String and replace with another string

I have a .csv file that contains:
scenario, custom, master_data
1, ${CUSTOM}, A_1
I have a string:
a, b, c
and I want to replace 'custom' with 'a, b, c'. How can I do that and save to the existing .csv file?
Probably the easiest way is to read in one file and output to another file as you go, modifying it on a per-line basis
You could try something with tokenizers, this may not be completely correct for your output/input, but you can adapt it to your CSV file formatting
BufferedReader reader = new BufferedReader(new FileReader("input.csv"));
BufferedWriter writer = new BufferedWriter(new FileWriter("output.csv"));
String custom = "custom";
String replace = "a, b, c";
for(String line = reader.readLine(); line != null; line = reader.readLine())
{
String output = "";
StringTokenizer tokenizer = new StringTokenizer(line, ",");
for(String token = tokenizer.nextToken(); tokenizer.hasMoreTokens(); token = tokenizer.nextToken())
if(token.equals(custom)
output = "," + replace;
else
output = "," + token;
}
readInventory.close();
If this is for a one off thing, it also has the benefit of not having to research regular expressions (which are quite powerful and useful, good to know, but maybe for a later date?)
Have a look at Can you recommend a Java library for reading (and possibly writing) CSV files?
And once the values have been read, search for strings / value that start with ${ and end with }. Use Java Regular Expressions like \$\{(\w)\}. Then use some map for looking up the found key, and the related value. Java Properties would be a good candidate.
Then write a new csv file.
Since your replacement string is quite unique you can do it quickly without complicated parsing by just reading your file into a buffer, and then converting that buffer into a string. Replace all occurrences of the text you wish to replace with your target text. Then convert the string to a buffer and write that back to the file...
Pattern.quote is required because your string is a regular expression. If you don't quote it you may run into unexpected results.
Also it's generally not smart to overwrite your source file. Best is to create a new file then delete the old and rename the new to the old. Any error halfway will then not delete all your data.
final Path yourPath = Paths.get("Your path");
byte[] buff = Files.readAllBytes(yourPath);
String s = new String(buff, Charset.defaultCharset());
s = s.replaceAll(Pattern.quote("${CUSTOM}"), "a, b, c");
Files.write(yourPath, s.getBytes());

convert string ["string"] to string array

I have a kind of strange problem, I am receiving from server side an compressed text that is a string array, for exemple ["str1","str2"] or just ["str"]
Can I convert it to an normal string array? like:
String[] array;
array[1] = "str";
I know that is not a big deal to convert an simple string but not this one...Any ideas?
This text can be treated as JSON so you could try using JSON parser of your choice. For gson your code could look like.
String text = "[\"str1\",\"str2\"]"; // represents ["str1","str2"]
Gson gson = new Gson();
String[] array = gson.fromJson(text, String[].class);
System.out.println(array[0]); //str1
System.out.println(array[1]); //str2
If you are able to change the way server is sending you informations you can consider sending array object, instead of text representing array content. More info at
https://docs.oracle.com/javase/tutorial/jndi/objects/serial.html
http://www.tutorialspoint.com/java/java_serialization.htm
or many other Java tutorials under serialization/deserialization.
This may help you:
// cleanup the line from [ and ]
String regx = "[]";
char[] ca = regx.toCharArray();
for (char c : ca) {
line = line.replace("" + c, "");
}
String[] strings = line.split("\\s*,\\s*");
// now you have your string array

Appending text to the middle of a Strring in EditText

Please have a look at the following code:
ArrayList<String> text = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
int cursorPosition = edtTEXT.getSelectionStart();
String currentString = edtTEXT.getText().toString().trim();
voiceEdt.append(text.get(0), cursorPosition, cursorPosition+1)
Toast.makeText(VoiceNotes.this, "Status: "+writeInMiddle, Toast.LENGTH_LONG).show();
My attempt is to append the text to the place where the cursor is located. Most probbly it will be at the middle of a sentence. For an example, take the text "Welcome to abc". If the text is at the beginning of the text abc then the new text should get appened at the beginning of the abc.
My above code doesn't work, it appends nothing. Anyway, the edtTEXT is an EditText.
What is wrong here?
use insert() method instead of append(). see example,
String s = "This a String";
StringBuffer buffer = new StringBuffer(s);
buffer.insert(5, "is ");
System.out.println(buffer.toString());
// will give "This is a String"

Dynamic String get dynamic image name in java

i have a dynamic String like
age/data/images/four_seasons1.jpg
from above string i need to get the image name alone (i.e.) four_seasons1.jpg
the path and the image will be a dynamic one(any image format will occure)
Please let me know how to do this in java?
thanks in advance
Use the File Object.
new File("/path/to/file").getName()
You could also use String.split().
"/path/to/file/sdf.png".split("/")
This will give you an array in which you pick the last element. But the File Object is better suited.
String text = "age/data/images/four_seasons1.jpg";
String name = text.substring(text.lastIndexOf("/") + 1);
String path = text.substring(0, text.lastIndexOf("/"));
System.out.println(name);
System.out.println(path);
Outputs
four_seasons1.jpg
age/data/images
Take some time and become familiar with the java.lang.String API. You'll be doing this kind of stuff a lot
You can go for regex but if you find the pattern is fixed, A very crude solution can be a straight forward approach
String url = "age/data/images/four_seasons1.jpg";
String imageName = url.substring(url.lastIndexOf( "/" )+1, url.length()) ;
You can parse this path. As a delimiter you must take '/' symbol. After that you can take last parsed element.
String phrase = "age/data/images/four_seasons1.jpg";
String delims = "/";
String[] tokens = phrase.split(delims);
About String.split you can read more here.
String s = "age/data/images/four_seasons1.jpg";
String fileName = new String();
String[] arr = s.split("/");
fileName = arr[arr.length-1];
}

Categories

Resources