I am converting between "\n" to ":" in file .txt. And here, this is my paragraph before convert:
You can see that, between string "Hoa" and string "Đàm", it have one character " " and two character "\n". And this is my convert function:
private String convertData(){
String different = "~`!##$%^&*()-_=+*/\\\"'|]}{[:;?/.><,\n ";
StringBuilder data = new StringBuilder(tvData.getText().toString().trim());
for (int j = 0; j < data.length(); j++){
if(different.contains("" + data.charAt(j))) data.setCharAt(j, ':');
}
String convertData = data.toString().trim();
return convertData;
}
And this is result:
You can see behind character "\n" have a character, and it is not in string different.
Can anyone tell me what to do?
1 - Proceed like you're currently doing, but the \n (and the other escaped sequences).
So, use
String different = "~`!##$%^&*()-_=+*/'|]}{[:;?/.><, ";
2 - After the loop, use string.replace() to replace only the substring "\n" and the other sequences, i.e.: "\\" and "\"".
You could use a loop to replace the sequences, but you must replace 2 characters instead of one.
Basically, in the first loop you replace all the single characters.
Then, you replace the character sequences.
Notepad only supports Windows line endings (\r\n), so that must be what your file contains. Convert the file to Unix line endings (\n) using literally any program that is not Notepad, or add \r to your search pattern.
Related
I am fetching number from contact book and sending it to server. i get number like this (+91)942 80-60 135 but i want result like this +9428060135.+ must be first character of string number.
Given your example you want to replace the prefix with a single + character. You also want to remove other non-numeric characters from the number string. Here's how you can do that:
String number = "(+91)942 80-60 135";
number = "+" + number.replaceAll("\\(\\+\\d+\\)|[^\\d]", "");
The regex matches any prefix (left paren followed by a + followed by one or more digits, followed by a right paren) or any non digit character, and removes them. This is concatenated to a leading + as required. This code will also handle + characters within the number string, e.g. +9428060135+++ and +(+91)9428060135+++.
If you simply wanted to remove any character that is not a digit nor a +, the code would be:
String number = "(+91)942 80-60 135";
number = number.replaceAll("[^\\d+]", "");
but be aware that this will retain the digits in the prefix, which is not the same as your example.
You can use String.replace(oldChar, newChar). Use the code below
String phone = "(+91)942 80-60 135"; // fetched string
String trimmedPhone = phone.replace("(","").replace(")","").replace("-","").trim();
I hope it will work for you.
check this. Pass your string to this function or use as per code goes
String inputString = "(+91)942 80-60 135";
public void removeSpecialCharacter(String inputString) {
String replaced = inputString.replaceAll("[(\\-)]", "");
String finalString = replaced.replaceAll(" ", "");
Log.e("String Output", " " + replaced + " " + second);
}
I was wondering how I could programmatically edit strings in android. I am displaying strings from my device to my website, and the apostrophes ruin the PHP output. so in order to fix this, I needed to add character breaks, ie: the backslash '\'.
For example, if I have this string: I love filiberto's!
I need android to edit it to: I love filiberto\'s!
However, each string is going to be different, and there will also be other characters that I have to escape from . How can I do this?
I was wondering how I could programmatically edit strings in android. I am displaying strings from my device to my website, and the apostrophes ruin the PHP output. so in order to fix this, I needed to add character breaks, ie: the backslash '\'.
This is what I have so far, thanks to ANJ for base code...:
if(title.contains("'")) {
int i;
int len = title.length();
char[] temp = new char[len + 1]; //plus one because gotta add new
int k = title.indexOf("'"); //location of apostrophe
for (i = 0; i < k; i++) { //all the letters before the apostrophe
temp[i] = title.charAt(i); //assign letters to array based on index
}
temp[k] = 'L'; // the L is for testing purposes
for (i = k+1; i == len; i++) { //all the letters after apostrophe, to end
temp[i] = title.charAt(i); //finish the original string, same array
}
title = temp.toString(); //output array to string (?)
Log.d("this is", title); //outputs gibberish
}
Which outputs random characters.. not even similar to my starting string. Does anyone know what could be causing this? For example, the string "Lol'ok" turns into >> "%5BC%4042ed0380"
I am assuming you are storing the string somewhere. Lets say the string is: str.
You can use a temporary array to add the '/'. For a single string:
int len = str.length();
char [] temp = new char[len+1]; //Temporary Array
int k = str.indexOf("'"), i; //Finding index of "'"
for(i=0; i<k-1; i++)
{
temp[i] = str.charAt(i); //Copying the string before '
}
temp[k] = '/'; //Placing "/" before '
for(i=k; j<len; j++)
{
temp[i+1] = str.charAt(i); //Copying rest of the string
}
String newstr = temp.toString(); //Converting array to string
You can use the same for multiple strings. Just make it as a function and call it whenever you want.
The String API has a number of API calls that could help, for example String.replaceAll. But...
apostrophes ruin the PHP output
Then fix the PHP code rather than require "clean" input. Best option would be to select a well supported transport format (say JSON or XML) and let the Json API on each end handle escape code.
I am working on android. I have a string containing huge data. In that string I want to replace a particular character to another character. I got the index of the character which I want to replace. But I am unable to replace that character.
How can I do that?
String str = "data1data2mdata2test1test2test3dd"
int ind = str.indexOf("m");
System.out.println("the index of m" + ind);
Now in the above string I want to replace the character "m"(after data2) to "#".
Now how can I replace the m to #. Please help me in this reagard.
You can use substring:
String newStr = str.substring(0, ind) + '#' + str.substring(ind + 1);
Try this:
str = str.replaceFirst("m", "#");
It will replace the first m to #
String str1 = "data1data2mdata2test1test2test3dd"
String str = str1.replace("m", "#");
System.out.println(str);
So you are getting 10 as system out,
so this way you can replace it like,
Str.replace('m', '#')--->when you want all occurrences of it to replace it,
Or if you want only first occurrence to be replaced by # then you can do following trick,
StringBuffer buff=new StringBuffer();
buff.append(Str.substring(0,ind)).append("#").append(Str.substring(ind+1));
i hope it would help
I have a string like this:
_id:2 thread_id:189 address:0292 m_size:null person:0 date:1372494272447 date_sent:0 protocol:0 read:1 status:-1 type:1 reply_path_present:0 subject:null body:Ok. Reply message. service_center:051108 locked:0 sim_id:0 error_code:0 seen:1 _id:1 thread_id:189 address:292 m_size:null person:0 date:1372493695831 date_sent:0 protocol:null read:1 status:-1 type:2 reply_path_present:null subject:null body:Test message service_center:null locked:0 sim_id:0 error_code:0 seen:0
I want to retrieve only parts of this string, for example the address:0292 and the body:xyz from the entire string. I want all instances of these two from a very large String (above is just a sample). Let's assume its more than 20000 characters.
How can I achieve this?
Looks like every address is followed by m_size, so use the string.split() function, to split over the keyword address then select the string.substring() (from each string in the resulting array) until reaching the keyword m_size. And repeat the entire thing for the keywords body and service_center. I can't think of any other way.
You are right, it doesn't seem pretty. But it works :)
String[] splitString = string.split(" ");
for (int i = 0; i < splitString.length; i++) {
if (splitString[i].startsWith("body") || splitString[i].startsWith("address"))
Log.i(TAG, "Found: " + splitString[i]);
// Do whatever you need to do
}
For my app I have created a QR Code, then took that bitmap and added text to the bitmap, however I need the text not to extend longer then the bitmap is. So what I want to do is create an Array of the text by taking 25 characters then find the last index of (" ") in that 25 character section. at that space I want to be able to replace that space that was located with \n to start a new line.
So the plan is if I have a String that looks like "Hello this is my name and I am longer than 25 charters and I have lots of spaces so that this example will work well."
I want it to out up this
Hello this is my name and
I am longer than 25
charters and I have lots
of spaces so that this
example will work well.
To make this I counted 25 characters then went back to the most resent space, at that point I hit enter, I want my app to do this for me.
I am not very good at English so if something doesn't make sense tell me and I will try to explain it. Thanks
I haven't tested this but you can try it and tweak as necessary
String fullText = "your text here";
String withBreaks = "";
while( fullText.length() > 25 ){
String line = fullText.substring(0,24);
int breakPoint = line.lastIndexOf( " ");
withBreaks += fullText.substring(0,breakPoint ) + "\n";
fullText = fullText.substring( breakPoint );
withBreaks += fullText;
char [] way (more C like):
public static String reduceLength(String s, int len){
char [] c = s.toCharArray();
int i=len, j=0, k;
while(true){
for(k=j; k<=i; k++){
if (k >= s.length()) return new String(c);
if (c[k] == ' ') j=k;
}
c[j] = '\n';
i= j+ len;
}
}
This isn't safe, just something i threw together.