I am trying to take a text from a file, and take the a's and b's out using split function.
String inStr = in.readLine();
// for example "a1a1a1a1b"
String lettersStr = letters.readLine();
// for example "ab"
Then i'm doing this trying to split all the letters i want.
Why is this not working?
String outFinal = "\"\\\\s*["+ lettersStr +"]\\\\s*\"";
String[] inSplit = inStr.split(outFinal);
What i'm trying to accomplish is
inStr.split("\\s*[ab]\\s*"));
Which works fine but the problem is that since i'm using a BufferedReader (fileread) the letters to cut out keep changing, hence why i can't just use the line above.
Thanks in advance
Regards
Change
String outFinal = "\"\\\\s*["+ lettersStr +"]\\\\s*\"";
to
String outFinal = "\\s*["+ lettersStr +"]\\s*";
Related
how do you split a string and get the sentence only after the question mark. For example say you have the line : hello?myNameIs...
how could you only get what's after the question mark
many thanks
If all your use cases will be similar to the simple example you've stated, you could use a simple split.
The code:
String delim = "\\?";
String s = "hello?myNameIs";
String token[] = s.split(delim);
System.out.println(token[1]);
Gives the output:
myNameIs
Of course, you can tinker with it to solve your specific problems.
try this one
String sArray[] = src.split(Pattern.quote("?"));
I have a string like this:
String str="\"myValue\".\"Folder\".\"FolderCentury\"";
Is it possible to split the above string by . but instead of getting three resulting strings only two like:
columnArray[0]= "myValue"."Folder";
columnArray[1]= "FolderCentury";
Or do I have to use an other java method to get it done?
Try this.
String s = "myValue.Folder.FolderCentury";
String[] a = s.split(java.util.regex.Pattern.quote("."));
Hi programmer/Yannish,
First of all the split(".") will not work and this will not return any result. I think java String split method not work for . delimiter, so please try java.util.regex.Pattern.quote(".") instead of split(".")
As I posted on the original Post (here), the next code:
String input = "myValue.Folder.FolderCentury";
String regex = "(?!(.+\\.))\\.";
String[] result=input.split(regex);
System.out.println("result: "+Arrays.toString(result));
Produces the required output (an array with two values):
result: [myValue.Folder, FolderCentury]
If the problem you're trying to solve is really that specific, you could do it even without using regular expression matches at all:
int lastDot = str.lastIndexOf(".");
columnArray[0] = str.substring(0, lastDot);
columnArray[1] = str.substring(lastDot + 1);
I got problem with my TextArea
String A contain text a,b,c,d
I converted String to textarea using method TextArea.setText(A);
My problem is that textarea print out abcd instead of it I want it printed in lines example
A
B
C
D
I did read book and tried google but I can't find solution to my problem ;(
Sounds like you need to follow the javadoc that JB Nizet linked to above, and take advantage of the String.replace() method. It takes two CharSequences, first the characters to match, the second the characters to replace it with. Find the ", " and replace with "\n". So
CharSequence theseChars = new CharSequence(", ");
CharSequence withTheseChars = new CharSequence("\n");
String newString = A.replace(theseChars, withTheseChars);
And that should get the job done.
I have used the most basic stuff of Java.
I think this is easy to understand
String s = "a,b,c,d";
String s1 =s.replace(",", "");
String s2 = s1.replace("", "\n").toUpperCase();
What I want to do is to measure the data of a line on a large string. I am not sure if any has tried this but I have a string which looks like this.
String a =
"This is
kinda
my String"
which would display on android textview as
This is
kinda
my String
Now what I want to achieve is being able get the length of the second line "kinda".
The purpose for this is to be able to set my paging for a book project.
I hope I was clear enough. Thanks for any advice or ideas shared.
Should just be:
a.split("\n")[1].length()
You can use the String function split(String regex)
To split on a "\n"(newline) then use it as a tuple/array and call for any word you want.
Split based on new line indicator.
String lines[] = a.split("\\r?\\n");
int length =0;
if(lines.length >1)
{
length = lines[1].length();
}
I haven't used java in years that being said I'd imagine something like this
String[] temp; //Let's make an array of strings
temp = a.split("\n"); //Split the large string by carriage return
int length = temp[1].length(); //get the length of the 2nd string
Assuming those are \n separating your lines...
I am getting response for some images in json format within this tag:
"xmlImageIds":"57948916||57948917||57948918||57948919||57948920||57948921||57948 922||57948923||57948924||57948925||57948926||5794892"
What i want to do is to separate each image id using .split("||") of the string class. Then append url with this image id and display it.
I have tried .replace("\"|\"|","\"|"); but its not working for me. Please help.
EDIT: Shabbir, I tried to update your question according to your comments below. Please edit it again, if I didn't get it right.
Use
.replace("||", "|");
| is no special char.
However, if you are using split() or replaceAll instead of replace(), beware that you need to escape the pipe symbol as \\|, because these methods take a regex as parameter.
For example:
public static void main(String[] args) {
String in = "\"xmlImageIds\":\"57948916||57948917||57948918||57948919||57948920||57948921||57948922||57948923||57948924||57948925||57948926||5794892\"".replace("||", "|");
String[] q = in.split("\"");
String[] ids = q[3].split("\\|");
for (String id : ids) {
System.out.println("http://test/" + id);
}
}
I think I know what your problem is. You need to assign the result of replace(), not just call it.
String s = "foo||bar||baz";
s = s.replace("||", "|");
System.out.println(s);
I tested it, and just calling s.replace("||", "|"); doesn't seem to modify the string; you have to assign that result back to s.
Edit: The Java 6 spec says "Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar." (the emphasis is mine).
According to http://docs.oracle.com/javase/6/docs/api/java/lang/String.html, replace() takes chars instead of Strings. Perhaps you should try replaceAll(String, String) instead? Either that, or try changing your String ("") quotation marks into char ('') quotation marks.
Edit: I just noticed the overload for replace() that takes a CharSequence. I'd still give replaceAll() a try though.
String pipe="pipes||";
System.out.println("Old Pipe:::"+pipe);
System.out.println("Updated Pipe:::"+pipe.replace("||", "|"));
i dont remember how it works that method... but you can make your own:
String withTwoPipes = "helloTwo||pipes";
for(int i=0; i<withTwoPipes.lenght;i++){
char a = withTwoPipes.charAt(i);
if(a=='|' && i<withTwoPipes.lenght+1){
char b = withTwoPipes.charAt(i+1);
if(b=='|' && i<withTwoPipes.lenght){
withTwoPipes.charAt(i)='';
withTwoPipes.charAt(i+1)='|';
}
}
}
I think that some code like this should work... its not a perfect answer but can help...