I am working on android. In my project I am adding '|' symbol and '#' symbol to string but it is not adding # symbol. I am not
getting where I went wrong. Please help me with this.
String str="";
str = str + "|" + id + "#" + id2 + "#" + id3;
When I print the string "str" it is displaying only id value but it is not printing id1 and id2.
Output:
|13
Use this:
<string name="strings">\#Strings</string>
i have check it like
String id = "A";
String id2 = "B";
String id3 = "C";
String str="";
str = str + "|" + id + "#" + id2 + "#" + id3;
and Output Like
|A#B#C
I tried this,
TextView tvAt = (TextView)findViewById(R.id.tvAtExample);
String id1 = "A";
String id2 = "B";
String id3 = "C";
tvAt.setText(id1 + "|"+ id2 + "#" + id3);
Its working, It produces the output
A|B#C
Related
So my requirement is to display a message showing yours and your friend's initials in lower case (ie. "mf and js are friends").
Here's my code
String myFullName = "Daniel Camarena";
String friendsFullName = "John Smith";
System.out.println( myFullName.toLowerCase().charAt(0)
+ myFullName.toLowerCase().charAt(7)
+ " and "
+ friendsFullName.toLowerCase().charAt(0)
+ friendsFullName.toLowerCase().charAt(5)
+ " are friends." );
The output I get is
199 and js are friends.
myFullName.toLowerCase().charAt(0) + myFullName.toLowerCase().charAt(7)
are working on ascii integer value and hence 199
The reason strings addition works for the second name is because that is part of the string formed due to this:
+ " and "
Quick fix, add an empty string at start
System.out.println("" + myFullName.toLowerCase().charAt(0)
+ myFullName.toLowerCase().charAt(7)
+ " and "
+ friendsFullName.toLowerCase().charAt(0)
+ friendsFullName.toLowerCase().charAt(5)
+ " are friends." );
System.out.println( "" + myFullName.toLowerCase().charAt(0) + myFullName.toLowerCase().charAt(7)
+ " and "
+ friendsFullName.toLowerCase().charAt(0)
+ friendsFullName.toLowerCase().charAt(5)
+ " are friends." );
Append the blank string to convert it to String and then it will start doing concanetation . As '+' is overloaded operator it is doing addition till it encounters String.
You can use following code :
String myFullName = "Daniel Camarena";
String friendsFullName = "John Smith";
String[] arrMyFullName = myFullName.toLowerCase().split(" ");
String[] arrFriendsFullName = friendsFullName.toLowerCase().split(" ");
String message = "";
for(String s : arrMyFullName)
message += s.charAt(0);
message += " and ";
for(String s : arrFriendsFullName)
message += s.charAt(0);
message += " are friends.";
System.out.println( message );
Above code also work if name is more than 2 words.
Try:
System.out.println( "" + myFullName.toLowerCase().charAt(0)
+ myFullName.toLowerCase().charAt(7)
+ " and "
+ friendsFullName.toLowerCase().charAt(0)
+ friendsFullName.toLowerCase().charAt(5)
+ " are friends." );
With this one you can have any name of friends. Instead of correcting the index which differs for each name.
String myFullName = "Daniel Camarena";
String friendsFullName = "John Smith";
String[] myNameSplit = myFullName.split(" ");
String myFirstInitial = String.valueOf(myNameSplit[0].charAt(0));
String myLastInitial = String.valueOf(myNameSplit[1].charAt(0));
String[] myFriendNameSplit = friendsFullName.split(" ");
String myFriendFirstInitial = String.valueOf(myFriendNameSplit[0].charAt(0));
String myFriendLastInitial = String.valueOf(myFriendNameSplit[1].charAt(0));
System.out.println(myFirstInitial+myLastInitial + " and " + myFriendFirstInitial+myFriendLastInitial+ " are friends");
It is adding ASCII value of d and c in output to avoid that do as following.
String myFullName = "Daniel Camarena";
String friendsFullName = "John Smith";
System.out.println( myFullName.toLowerCase().charAt(0)
+""+ myFullName.toLowerCase().charAt(7)
+ " and "
+ friendsFullName.toLowerCase().charAt(0)
+ friendsFullName.toLowerCase().charAt(5)
+ " are friends." );
I have a string data like this
string1 = ["car","house","boat"]["one","two","three","four"]["tiger","cat"]
I want the output to be like this :
first : car,house,boat
second : one,two,three,four
third : tiger,cat
How should I perform manipulation on that string?
This is my current attempt:
result6 = string1.substring(1);
String[] parts = result6.split("\\[");
String part1 = parts[0];
String part2 = parts[1];
String part3 = parts[2];
result3 = part1.replaceAll("[^a-zA-Z0-9,]", "");
result4 = part2.replaceAll("[^a-zA-Z0-9,]", "");
result5 = part3.replaceAll("[^a-zA-Z0-9,]", "");
result1 = "first : " + part1 + "\n" + "second : " + part2 + "\n" + "third : \n" + part3;
But that gets me erroneous output.
You have added part1, part2, part3 instead of result3, 4, 5 in the result1 assignment part. And also i suggest you to split according to ][, i you do the split according to [ only, you would get an empty string in parts[0].
String string1 = "[\"car\",\"house\",\"boat\"][\"one\",\"two\",\"three\",\"four\"][\"tiger\",\"cat\"]";
String parts[] = string1.split("\\]\\[");
String part1 = parts[0];
String part2 = parts[1];
String part3 = parts[2];
String result3 = part1.replaceAll("[^a-zA-Z0-9,]", "");
String result4 = part2.replaceAll("[^a-zA-Z0-9,]", "");
String result5 = part3.replaceAll("[^a-zA-Z0-9,]", "");
String result1 = "first : " + result3 + "\n" + "second : " + result4 + "\n" + "third : " + result5;
System.out.println(result1);
Output:
first : car,house,boat
second : one,two,three,four
third : tiger,cat
I have a string
String str = "line1"+"\n" +
"line2"+"\n" +
"line3"+"\n" +
"line4"+"\n" +
"line5"+"\n" +
"line6"+"\n" +
"line7"+"\n" +
"line8"+"\n" +
"line9"+"\n" +
"line10"+"\n" +
"line11"+"\n" +
"line12"+"\n" +
"line13"+"\n" +
"line14"+"\n" +
"line15"+"\n" +
"line16"+"\n" +
"line17"+"\n";
I want to get out of it an array of strings
String str1 = "line1"+"\n" +
"line2"+"\n" +
"line3"+"\n" +
"line4"+"\n";
String str2 = "line5"+"\n" +
"line6"+"\n" +
"line7"+"\n" +
"line8"+"\n";
String str3 = "line9"+"\n" +
"line10"+"\n" +
"line11"+"\n" +
"line12"+"\n";
String str4 = "line13"+"\n" +
"line14"+"\n" +
"line15"+"\n" +
"line16"+"\n";
String str5 = "line17"+"\n";
if I do so
String[] str1 = str.split("\n");
I get an array of strings, in which only one line, and I need it for a few
instead of the string I will have the file from which I plan to read the text in a row
for splitting string with particular format you need to specify regular expression
so in your case regular expression will be ("\r\n")
Look Here
I have the name of a java variable in a string. I want to replace it with the letter x. How can I do this java, and make sure that other words in the string are not replaced ?
For example, say my variable is res, and my string is "res = res + pres + resd + _res. I want the string to become x = x + pres + resd + _res.
You can use a word boundary to only capture whole words:
String s = "res = res + pres + resd + _res";
String var = "res";
System.out.println(s.replaceAll("\\b" + var + "\\b", "x"));
outputs x = x + pres + resd + _res
You can use the \b metacharacter to match a word boundary. (Bear in mind you'll need to use doule backslashes to escape this in Java.)
So you can do something like the following:
final String searchVar = "res";
final String replacement = "x";
final String in = "res = res + pres + resd + _res";
final String result = in.replaceAll("\\b" + searchVar + "\\b", replacement);
System.out.println(result);
// prints "x = x + pres + resd + _res"
I am using Java and I created obscenity filter that works well except if we have a letter that ends in a it will be replaced.
eg. "I want a banana" --> "I want a bananbleep"
HOWEVER...
If you add punctuation after the 'a' it will show up correctly.
eg. "Would you like a banana?" --> "Would you like a banana?"
Here is what I did:
public String rInString(String theDisplay) {
String a = theDisplay;
String b = word;
String c = Matcher.quoteReplacement(replacement);
if(matchWholeWord != null && matchWholeWord){
b = "([^\\p{Alpha}\\p{Lower}\\p{Space}])" + b +
"([^\\p{Alpha}\\p{Lower}\\p{Space}])";
a = " " + a + " ";
c = "$1" + c + "$2";
}
return message.replaceAll("(?i:" + b + " )", c).trim();
}
public String rInString(String theDisplay, String theB, String replace) {
String c = Matcher.quoteReplacement(replace);
String a = theDisplay;
String b = theB;
if (matchWholeWord != null && matchWholeWord) {
b = "([^\\p{Alpha}\\p{Space}])" + b + "([^\\p{Alpha}\\p{Space}])";
a = " " + a + " ";
c = "$1" + c + "$2";
}
return message.replaceAll("(?i:" + b + ")", c).trim();
}
The issue was that one letter (a) was being blocked because $$ are the terminus in the RegEx statement. This meant that the a was being counted as a bad element. Each $ needed to be escaped in the SQL statement.