I have this string "nullRobert Luongo 431-232-1233 Canada"
and I'd like to get rid of the null character. I don't know how it got there, but I would like to get rid of it.
It's likely that you get it because you did something like this:
String s = null;
.
.
.
s+="Robert Luongo 431-232-1233 Canada"; //mind the +=
One way to correctly do it is by:
String s= "";
You're concatenating a String that is null -- don't do that. Initialize the String at least with ""
So rather than
String myString;
When you do this and later do:
myString += "something";
you'll get `nullsomething
So instead assign an empty String to the String of interest when you declare it.
String myString = "";
Option 1: Recommended
I would suggest you to look into the issue from where the string is contains 'null' and fix it at the source rather than using hacks.
Option 2: Not recommended
But still if you want to replace the null from it then you can use the following. But it is not recommended as it can replace a proper word containing 'null' in it and make the string junk.
The following replaces the first instance of 'null' with empty String.
string.replace("null", "");
You can also replace all the instances of 'null' with empty String using replaceAll.
string.replaceAll("null", "");
You can use this.
String stringWithoutNull = stringWithNull.replaceAll("null", "");
Related
I need to substract an specific parameter (urlReturn) from a URL like this :
http://somedomain.mx/th=6048000&campus=A&matric=L01425785&serv=GRAD&count=2&per=TEST&passwd=69786e720c0f0e&mount=1000.00&urlReturn=http://somedomain.mx:7003/SP/app/login.xhtml?id=1234&mat=2323&fh=05012014124755&store=TESO
My final string should look like this:
String urlReturn = http://somedomain.mx:7003/SP/app/login.xhtml?id=1234&mat=2323;
And the rest of the string should look like this:
String urlReturn2 = http://somedomain.mx/th=6048000&campus=A&matric=L01425785&serv=GRAD&count=2&per=TEST&passwd=69786e720c0f0e&mount=1000.00&fh=05012014124755&store=TESO
I currently have this :
String string = string.toString().split("\\&")[0];
But the urlReturn parameter should always come as the first one.
Try this (s is your original String):
urlReturn = s.substring(0, s.indexOf("&urlReturn=")).replace("&urlReturn=", "");
urlReturn2 = s.substring(s.indexOf("&urlReturn=")).replace("&urlReturn=", "");
Definitely not elegant at all, but working. I really need some sleep now so take my anser carefully :) You may alswo wanto to check if the parameters is in the String s via the contains method to avoid index out of bounds exceptions.
use string#split
url.split("\\?")[1];
So I'm looking to get the first numbers of an IP. Let's say I have something like 255.35.54.34. I want to get the first part of numbers up until the first period. How would I do this in Java? So it'd leave me with 255.
Take a look at the String class. You can use a couple of methods to accomplish this:
the indexof(...) method will give you the offset of the "."
the substring(...) method will allow you to get a string using the above offset
Or another option is to use the split(...) method to get an array of all four IP values.
As #camickr says, you can use indexOf and substring thus:
String ipAddress = "192.168.1.9";
System.out.println(ipAddress.substring(0, ipAddress.indexOf('.')));
This will print "192"
You can use the split() method with a period as the argument. This will split the string along periods and give you a String[].
Then get the values just as you would from a normal array by using a subscript. In your case index 0 will get you the value
String ip = "255.255.255.255";
String[] splitIP = ip.split(".");
String required = splitIP[0];
You can do it in two ways :
One is that you use String.split() method, and other is to using StringTokenizer class.
Using String.split() :
String ip = "255.1.2.3";
String[] splitIP = ip.split("\\.");
String required = splitIP[0];
System.out.println(required);
Here \\ is required,oherwise it will throw an exception
Using StringTokenizer :
String ip = "255.1.2.3";
StringTokenizer tk=new StringTokenizer(ip,".");
while (tk.hasMoreTokens())
{
System.out.println(tk.nextToken());
break;
}
Hope this will help you..
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);
Im having this really weird issue that i haven't been able to figure out for a few hours. Basically im trying to split this getInterfaceBounds-client.ry, what im doing is this
final String className = line.split(".")[0];
im getting a arrayindexoutofbounds exception. I really have no idea why, do you?
Exception in thread "Thread-3" java.lang.ArrayIndexOutOfBoundsException: 0
split uses a regular expression. In regex . means any character, so you need to escape it.
Try:
final String className = line.split("\\.")[0];
See http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html#sum
The change required is :
final String className = line.split("\\.")[0];
Check this example for more details.
String s="getInterfaceBounds-client.ry";
String[] arr = s.split("\\.");
for(String str : arr)
{
System.out.println(str);
}
Ideone link.
Use this instead and it will work. I just tested it.
String line = "getInterfaceBounds-client.ry";
String className = line.split("[.]")[0];
System.out.println(className);
The . is a special character in regex which represent any character.
You can learn more about the different special characters in regex here:
http://www.fon.hum.uva.nl/praat/manual/Regular_expressions_1__Special_characters.html
Use Array variable for split() beacuse we may get more than a value from splitting so it would be helpful if u use array it would avoid confusion of accesing the values for example:
String line = "getInterfaceBounds-client.ry.test";
String test[] = line.split("[.]");
System.out.println(test[0]+test[1]+test[2]);
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...