Replace substring from string - android

I have a String userId:344556\\ncustomerId:233 .
I want to replace the substring \\n with new line. So i tried
String more = strmoreInfo.replace("\\n", "\n");
My desired output is:
userId:344556
customerId:233
But my current output is:
userId:344556\
customerId:233
What am I doing wrong?

'\' is an escape character. You'll want to change the statement to:
String more = strmoreInfo.replace("\\\\n", "\n");

I will try your code but i could not found any problem its works......
public class HelloWorld
{
public static void main(String[] args)
{
String strmoreInfo="userId:344556\\ncustomerId:233";
String more = strmoreInfo.replace("\\n", "\n");
System.out.println(more);
}
}

Simply put a space e.g "< SPACE >\n" in second argument of replace method.
String more = strmoreInfo.replace("\\n", " \n");

Related

How to pass a string parameter to evaluateJavascript in android

I tried to pass a string parameter to the web app javascript code. It's failed while passing a variable with the string value. But it works when we hardcode the data. Please let me know what's wrong with this.
Working hardcoded code:
mWebview.evaluateJavascript("cm.setData('N051783')", new ValueCallback<String>() {
#Override
public void onReceiveValue(String value3) {
Log.d(" setData Return Value"," setData Return... "+value3);
}
});
Not working code with string variable
mWebview.evaluateJavascript("cm.setData("+sub_data+")", new ValueCallback<String>() {
#Override
public void onReceiveValue(String value3) {
Log.d(" sub_data Return Value"," sub_data Return... "+value3);
}
});
You are probably missing ""
My JavaScript function at com.UserProfile
function setUserName(name){
alert('username is ' + name)
}
How to call it from Java
mWebview.evaluateJavascript("com.UserProfile.setUserName("Rohit")",null);
How to pass the parameter?
You need to concatenate " with parameter. " is a special character. This is how you can concatenate " in a String.
Here is an example:
String username = "Rohit";
String func = com.UserProfile.setUserName(\""+ username + "\")";
mWebview.evaluateJavascript(func, null);
You can use StringBuilder to concatenate as well as"
Let's break it down line by line;
StringBuilder sb = new StringBuilder();
sb.append("com.UserProfile.setUserName") //com.UserProfile.setUserName
.append("(") //com.UserProfile.setUserName(
.append("\"") //com.UserProfile.setUserName("
.append(username) //com.UserProfile.setUserName("Rohit
.append("\"") //com.UserProfile.setUserName("Rohit"
.append(")") //com.UserProfile.setUserName("Rohit")
String func = sb.toString();
mWebview.evaluateJavascript(func, null);

Removal of comma in the end of a string android

i have problem how to , remove the comma in the of my output. I use replaceall but it doesnt remove the comma , this my code
public void onClick(View v) {
String space = "";
String foo = " ";
String foo1 = ",";
String sentences = null;
//Splitting the sentence into words
sentences = multiple.getText().toString().toLowerCase();
String[] splitwords = sentences.trim().split("\\s+");
for (String biyak : splitwords) {
foo = (foo + "'" + biyak + "'" + foo1);
foo.replaceAll(",$", " ");//foo.replaceAll();
wordtv.setText(foo);
My codes Output : 'Hello','world',
My desire output: 'Hello','world'
String instances are immutable. As a result, methods like replaceAll() do not modify the original string but instead return a modified copy of the string. So replace foo.replaceAll(...) with foo = foo.replaceAll(...).
you can use substring method of String class. or You can use deleteCharAt() method of StringBuilder or StringBuffer classStringBuffer sb=new StringBuffer(your_string);sb.deleteCharAt(sb.length()-1);
U can also use a if statement and traverse the whole string .
If a comma(,) is found replace it with a space(" ").

Add a "," in a string where in it there is this: "}{"

I'm working in a Android app and i need to put a "," between "}{" in my jsonstring before i can work with String#split(). For example i have this:
{"field1":"A","field2":"abcbab","field3":5}
{"field1":"B","field2":"fakfef","field3":25}
{"field1":"A","field2":"faefe","field3":12}
how can i do that?
Try this :
String newJsonStr = yourJsonStr.replaceAll("\\}\\s*\\{", "},{");
String text = "To be or,not,to be, that is the question.";
String k=",{}";
String newText = text.replaceAll(",", k); // Modify the string text
System.out.println(newText);
try this it will solve your problem

how to validate cityname,pincode from one string?

Example: I have a EditText and I want to check the first word is the city name and second word is the pincode. These both words are separated by comma(,).
Hey try this if you don't want to use split. YOu need to get string into a variable from edittext and then use the following code for doing yourself able to validate :)
String str = "tim,52250";
StringTokenizer st = new StringTokenizer(str, ",");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
Do this way..
String content="Mehsana,384001";
String[] contentArray=content.split(",");
And you will get
contentArray[0]=Mehsana
contentArray[1]=384001
then you can validate each string content..
Use split() to get the things done.
Ex:
String s= "abc,123"
String s1[]=s.split(",");
String city=s1[0];
String pincode=s1[1];
Try this
String strInput = editText.getText().toString();
String strSplit [] = strInput.split(",");
System.out.println("CityName : " + strSplit[0]);
System.out.println("PinCode : " + strSplit[1]);
String data = "ali,524513"
String []array = data.split(",")
you can validate array[0] and array[1] :)
System.out.println("Name: "+array[0]+" code: "+array[1]);

android function in class throws java.lang.NullPointerException

I've made a class which holds some string and integers, in that class I made a function to convert the data in the class in to a readable string;
public String GetConditions() {
String BigString = null;
String eol = System.getProperty("line.separator");
try {
BigString += "Depth: " + ci(Depth) + eol;
and so on...
Because I have to convert many integers, I made an extra function to convert a integer to a string;
public String ci(Integer i) {
// convert integer to string
if (i != null) {
String a = new Integer(i).toString();
return a;
} else {
return "n/a";
}
}
This throws a NullPointerException exception on return a. I'm quite new to Java, this is probally a noob question... Sorry about, thanks in advance!
There is a much simpler way to convert an Integer to a String: use String#valueOf(int).
public String ci(Integer i)
{
return i == null ? "n/a" : String.valueOf(i);
}
Try converting the Integer you pass in your method to string, instead of instantiating a new one.
You can do it straight forward like:
String a = i.toString();
or
String a = Integer.toString(i.intValue());
Thanks guys, but I found the problem, I've tried to add something to a string which was 'null' , this line:
String BigString = null;

Categories

Resources