I'm trying to split a string. For example "Castle"
String text = "Castle";
String[] word = text.split("c");
Log.d(Constants.RESULT, "1:" + word[word.length-1]);
I expect the result to be (1:astle). Instead i get (1:Castle)
I hope you guys cal help me out. Thanks!
Use String[] word = text.split("C");
Simple thing is that the Alphabet 'c' is not same as 'C',You are using 'c',better use 'C',
Try this
String text = "Castle";
String[] word = text.split("C");
Log.d(Constants.RESULT, "1:" + word[word.length-1]);
use this:
String text = "Castle";
String[] word = text.split("C");
Log.d(Constants.RESULT, "1:" + word[1]);
you got "astle" from this
Use
String text = "Castle";
String sub =text.substring(1,text.length);
Log.d( "Answer", "1:" + sub);
Related
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(" ").
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'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
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]);
I am splitting a string with some sample text in the middle. But it is not splitting. Please help me where i went wrong
String[] str;
parts[1] = "loopstWatch out this is testingloopstThis makes the difference";
str = parts[1].trim().split("loopst");
String[] arr;
arr = "loopstWatch out this is testingloopstThis makes the difference".trim().split("loopst");
Log.e("Data arr[0]",":"+arr[0]);
Log.e("Data arr[1]",":"+arr[1]);
Log.e("Data arr[2]",":"+arr[2]);
Output
arr[0] :""
arr[1] :"Watch out this is testing"
arr[2] :"This makes the difference"
I think you're doing it backwards.
You want to first split, then get the piece at index 1, then trim, then set str equal to the result:
String s = "loopstWatch out this is testingloopstThis makes the difference";
String str = s.split("loopst")[1].trim();
// str should be equal to "Watch out this is testing"