I have lots of editText fields and the user can add info into them.
from these editTexts i want to create one string. im using the stringBuilder at the moment. however if the user does not enter anything to some of the editTexts, i want the stringbuilder to ignore these fields. is this possible? and if so, how can i do it?
this is what im doing at the moment:
String baseString = editText1.getText().toString();
String string2= editText2.getText().toString();
String string3= editText3.getText().toString();
StringBuilder superStringBuilder = new StringBuilder(baseString);
superStringBuilder.append(string2 + string3);
String superString = superStringBuilder.toString();
thank you
You can do something like:
If (string2.equals("")){
//Then do something when the edit text is blank.
superStringBuilder.append(string3);
} else{
superStringBuilder.append(string2 + string3);
}
Hope that helps.
thanks to your help this is an example for others if they have the same problem.
String string1 = editText1.getText().toString();
String string2 = editText2.getText().toString();
String string3 = editText3.getText().toString();
String string4 = editText4.getText().toString();
String string5 = editText5.getText().toString();
StringBuilder superStringBuilder = new StringBuilder(string1);
if (string2.equals("")){
superStringBuilder.append("");
}else { superStringBuilder.append(string2);
}
if (string3.equals("")){
superStringBuilder.append("");
}else { superStringBuilder.append(string3);
}
if (string4.equals("")){
superStringBuilder.append("");
}else { superStringBuilder.append(string4);
}
if (string5.equals("")){
superStringBuilder.append("");
}else { superStringBuilder.append(string5);
}
String superString = superStringBuilder.toString();
this will make the string filter out the editText with no text in them :) so the new string created with stringbuilder is either 4 strings or 2 strings :)
thank you
Related
I want to get a text that it is a part of an string.For example: I have a string like "I am Vahid" and I want to get everything that it's after "am".
The result will be "Vahid"
How can I do it?
Try:
Example1
String[] separated = CurrentString.split("am");
separated[0]; // I am
separated[1]; // Vahid
Example2
StringTokenizer tokens = new StringTokenizer(CurrentString, "am");
String first = tokens.nextToken();// I am
String second = tokens.nextToken(); //Vahid
Try:
String text = "I am Vahid";
String after = "am";
int index = text.indexOf(after);
String result = "";
if(index != -1){
result = text.substring(index + after.length());
}
System.out.print(result);
Just use like this, call the method with your string.
public String trimString(String stringyouwanttoTrim)
{
if(stringyouwanttoTrim.contains("I am")
{
return stringyouwanttoTrim.split("I am")[1].trim();
}
else
{
return stringyouwanttoTrim;
}
}
If you prefer to split your sentence by blank space you could do like this :
String [] myStringElements = "I am Vahid".split(" ");
System.out.println("your name is " + myStringElements[myStringElements.length - 1]);
What I have : text1,text2,text3
What I want : text1 text2 text3
replace comma with space?
final String s = "text1,text2,text3".replace(",", " ");
I tried with both replace and replaceAll. But didn't work
This is because both replace() and replaceAll() don't change the String object, they return you a new one. Strings are immutable in Java.
Try This Way:
String data = "text1,text2,text3";
String temp = data.replace(","," ");
Now You have all
This what you should do
String str = "text1,text2,text3"
str = str.replace(","," ");
I have not found documentation on how I can get the first letter of a value in a TextView?
Very easy,
String strTextview = textView.getText().toString();
strTextView = strTextView.substring(0,1);
Alternatively you can try following way too
char firstCharacter = textView.getText().toString().charAt(0);
To get the first letter you'll have to make this call:
char firstCharacter = myTextView.getText().charAt(0);
Use the method from below. Provide the string from TextView as the parameter.
public String firstStringer(String s) {
String str= s.substring(0, Math.min(s.length(), 1));
return str;
}
You can use this
TextView tv = (TextView) findViewById(R.id.textview);
String frstLetter = tv.getText().substring(0, 1);
it is simple. To retrieve the Text from the TextView you have to use getText().toString();
String textViewContent = textViewInstance.getText().toString();
and the first letter textViewContent.charAt(0)
To fetch the content of the string from TextView:
String content = textView.getText().toString();
To fetch the first character
char first = content.charAt(0);
Try this
String value = text.getText().toString();
String firstTen = value.substring(0, 1);
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]);
Connection conn = null;
String url = "jdbc:jtds:sqlserver://192.168.1.25:1433/";
String dbName = "Demo;Instance=MSSQLSERVER;";
String userName = "BIT";
String password = "1234";
String driver = "net.sourceforge.jtds.jdbc.Driver";
try {
TextView tv1 = (TextView) findViewById(R.id.textView1);
TextView tv2 = (TextView) findViewById(R.id.textView2);
TextView tv3 = (TextView) findViewById(R.id.textView3);
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
Log.w("Connection","open");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT ItemDesc,Qty,NetPrice FROM TrxDetail ");
String a ="";
String b ="";
String c ="";
while (rs.next())
{
a += rs.getString("ItemDesc");
b += rs.getString("Qty");
c += rs.getString("NetPrice");
}
tv1.setText(a);
tv2.setText(b);
tv3.setText(c);
conn.close();
}
xml:
Why not all the information.
Just released the final data.
how to show all data.
how to edit xml Correct.
I do not know who to ask.
while (rs.next())
{
tv1.setText(rs.getString("ItemDesc"));
tv2.setText(rs.getString("Qty"));
tv3.setText(rs.getString("NetPrice"));
}
For every result you get, you set the contents of tv1, tv2 and tv3 to the contents of that result, overwriting the previous contents. If this is not what you want ( you didn't actually ask a question, so its hard to say, but I'm guessing your sparse "Why not all the information" means that you are complaining about seeing only the last entry?), you should not overwrite them.
What you can do is make a String, concatenate the new value to the current value, and then call setText once after the while?
Ok, you can do this. You might be better of using a StringBuffer btw, but that would need even more explaining...
String tv1S = "";
String tv2S = "";
String tv3S = "";
while (rs.next()) {
tv1S += rs.getString("ItemDesc"));
tv2S += rs.getString("Qty"));
tv3S += rs.getString("NetPrice"));
}
tv1.setText(tv1S);
tv2.setText(tv2S);
tv3.setText(tv3S);
Now you also will need some spacing and stuff like that, but I'm not going to write all for you, this should be enough to figure it out....