Appending text to the middle of a Strring in EditText - java

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"

Related

Using Gson to serialize strings with \n in them

\n in a string is effective in printing the text following \n in next line. However, if the same string is serialized using Gson, then \n is no more effective in printing it in next line. How can we fix this ? Sample program given below.
In the program below, output of toString on map is printing text in next line due to presence of \n. However, the json string serialized using Gson is not able to show the same behaviour. In serialized string i.e. gsonOutput variable, '\' and 'n' are getting treated as separate characters, due to which the text after \n is not getting printed in next line. How can we fix this in gson serialization ?
Program:
Map<String, String> map = new HashMap<String,String>();
map.put("x", "First_Line\nWant_This_To_Be_Printed_In_Next_Line");
final String gsonOutput = new Gson().toJson(map);
final String toStringOutput = map.toString();
System.out.println("gsonOutput:" + gsonOutput);
System.out.println("toStringOutput:" + toStringOutput);
Output:
gsonOutput:{"x":"First_Line\nWant_This_To_Be_Printed_In_Next_Line"}
toStringOutput:{x=First_Line
Want_This_To_Be_Printed_In_Next_Line}
I am guessing that the gsonOutput has escaped the new line so if you change the line
final String gsonOutput = new Gson().toJson(map);
to (to unescape it):
final String gsonOutput = new Gson().toJson(map).replace("\\n", "\n");
you will get the output
gsonOutput:{"x":"First_Line
Want_This_To_Be_Printed_In_Next_Line_With_A_Tab_Before_It"}
toStringOutput:{x=First_Line
Want_This_To_Be_Printed_In_Next_Line_With_A_Tab_Before_It}
There probably is a better way of doing this :-)

Parsing A Text File, So That Every Line Is Stored As An Array Value

Basically, I want to parse, line by line, a Text file so that every line is in it's own array value.
E.g.
Hi There,
My Name's Aiden,
Not Really.
Array[0] = "Hi There"
Array[1] = "My Name's Aiden"
Array[2] = "Not Really"
But all the examples I have read already just confuse me and lead me to get frustrated. Maybe it's the way I approach it.
I don't know how to go about it, a point in the right direction would be most satisfying.
My suggestion is to use List<String> instead of String[] as arrays have fixed size, and that size is unknown before reading. Afterward one could make an array out of it, but to no real purpose.
For reading one has to know the encoding of the file.
Path path = Paths.get("C:/Users/Me/list.txt");
//Charset encoding = StandardCharsets.UTF_8;
Charset encoding = Charset.defaultCharset();
List<String> lines = Files.readAllLines(path, encoding);
for (String line : lines) {
...
}
for (int i = 0; i < lines.size(); ++i) {
String line = lines.get(i);
lines.set(i, "-- " + line;
}

Write JTextArea Text as String

NOTE: Thanks to user2602219 and Andrew Thompson I've solved my problem. I wish I could approve both answers right and vote up.
I've searched a lot. I've found something but they were not suitable for me.
All I want is write
Hello
World
as seen. But in my txt file it shows HelloWorld
try{
JTextArea area = new JTextArea();
String path = folder+"/"+name+".txt";
BufferedWriter output = new BufferedWriter(new FileWriter(path));
area.write(output);
output.close();
}
catch(IOException ex){}
The code above works fine. However, I've to do something before writing.
I've an encryption method (called enc). It takes a string and replaces letters wit another letters.
For example:
String text = "ABC";
String enc_text = enc(text);
//enc_text is now "ZXW";
But here is the thing. JTextArea.write looks for a "Writer" but I have to write a string because my encrypter returns a string.
Long story short. How to make
Hello
World
to this
Gteeu
Wuazx
Take the unencrypted string from the first text area.
Encrypt it.
Put the encrypted string in a second text area that is not immediately visible to the end user.
Use the JTextCompnent.write(..) method on the encrypted text area.
Try to use this:
String text = "Hello" + "\nWorld";
\n is a new line.
Did you try to read each line separately and then write it on the desired txt file? This way you could use "\n" to write on a new line. Another solution that came to my mind is using the following method:
String s = "";
s = s.format(something + "\n");
This way you would have lines in the string itself :)
static void writeStringToFile(File file, String data)
which allows you to write text to a file in one method call.
or use
FileUtils.writeStringToFile(new File("test.txt"), "Hello File");

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];
}

String Overlay in Android

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();

Categories

Resources