String formulation in Java - java

I need to compose an URL string in javaME (which doesn't allow me to work with many libraries which would do this in a no time) like this
url = "http://helloworld.com/index.php?data={\"Word\":"+wordX+",\"RS\":20/04/13-2008:31:44,\"SER\":"+net2+"}";
This is producion a string like this:
http://helloworld.com/index.php?data={"Word":358741051173885,"RS":20/04/13-2008:31:44,"SER":53543d303b44523d4e616fd}
The thing is I need the values to also be inside commas. I have tried everything but I'm not being able to understand how to do it.
Can someone explain this?
Thanks

Like this?
String url = String.format("http://helloworld.com/index.php?data={\"Word\":\"%s\",\"RS\":20/04/13-2008:31:44,\"SER\":\"%s\"}", wordX, net2);

Create a MessageFormat:
final MessageFormat urlFormat = new MessageFormat("http://helloworld.com/index.php?data={\"Word\":\"{0}\",\"RS\":20/04/13-2008:31:44,\"SER\":\"{1}\"}");
Then simply apply your values to the format:
final String url = urlFormat.format(new Object[]{wordX, net2});
Or, create a format String:
final String urlFormat = "http://helloworld.com/index.php?data={\"Word\":\"%s\",\"RS\":20/04/13-2008:31:44,\"SER\":\"%s\"}";
And use String.format:
final String url = String.format(urlFormat, wordX, net2);
Or, just change you String to:
url = "http://helloworld.com/index.php?data={\"Word\":\""+wordX+"\",\"RS\":20/04/13-2008:31:44,\"SER\":\""+net2+"\"}";

String val1 = "";
String val2 = "";
String str = "http://helloworld.com/index.php?data={\"Word\":\""+val1+"\",\"RS\":\"20/04/13-2008:31:44\",\"SER\":\""+val2+"\"}";

Related

Replace multiple substrings within a string

I have string such as
String url = "www.test.com/blabla/?p1=v1?p2=v2?p3=v3"
I would like to replace the "substrings" "v1","v2" and "v3" with other values. How can I achieve this?
Does something like this work for your case?
String url = "www.test.com/blabla/?p1=v1?p2=v2?p3=v3";
String result = String.format(url.replaceAll("v[0-9]", "%s"), "arg1", "arg2", "arg3");
System.out.println(result); //www.test.com/blabla/?p1=arg1?p2=arg2?p3=arg3
Edit:
Just a brief explanation of what this does, it replaces all the v1,v2,v3,v4,v5,v6,v7,v8,v9,v0 in the original url for %s and then uses this in the format method so you can attribute what you want it to be.

Query regarding regular expression

hi I have a string like this:
String s = "#Path(\"/BankDBRestService/customerExist(String")\";
I want to make that string as
#Path("/BankDBRestService/customerExist")
I tried this:
String foo = s.replaceAll("[(](.*)", "");
i am getting output as
#Path
can anyone please provide me the reqular expression to make this work
Try this one :
String s = "#Path(\"/BankDBRestService/customerExist(String\")";
String res = s.replaceAll("[(]([a-zA-Z_])+", "");
System.out.println(res);
Output : #Path("/BankDBRestService/customerExist")
If your string is "#Path(\"/BankDBRestService/customerExist(String)\")" i.e. with closed parenthesis then use regex [(]([a-zA-Z_])+[)]
try this
String foo = s.replaceAll("(.*)\\(.*\\)", "$1");

How to extract a url from a string in Java?

I have a string containing a short-code which looks like the one below:
some text...
[video url="http://www.example.com/path/to/my/video.ext"]
...some more text...
I want to be able to first check if the string contains that short-code and second extract the URL from it in Java (specifically Android).
use this regex for checking and grabbing url:
\[\w+\s+url="(?<urllink>)[^"]*"\s*]
and get gorup named urllink
try as:
String str = "[video url=\"http://www.example.com/path/to/my/video.ext\"]";
if (str.contains("url=\""))
{
int indexoff = str.indexOf("url=\"");
int indexofff = str.indexOf("\"]");
String strurl = str.substring(indexoff, indexofff - indexoff);
strurl = strurl.Replace("url=\"", ""); //get url string here
}
Android provides several function for this purpose. SOme of this are:
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html
http://developer.android.com/reference/org/apache/http/client/utils/URLEncodedUtils.html
String url = "whatever"
Boolean myBool = url.contains("ate");
String contains. Not sure what extract url means, but the string class has lots of useful functions.
A powerful and maintainable manner it to Java URL.class, then, you can mix with Regex

Need to Trim Java String

I need help in trimming a string url.
Let's say the String is http://myurl.com/users/232222232/pageid
What i would like returned would be /232222232/pageid
Now the 'myurl.com' can change but the /users/ will always be the same.
I suggest you use substring and indexOf("/users/").
String url = "http://myurl.com/users/232222232/pageid";
String lastPart = url.substring(url.indexOf("/users/") + 6);
System.out.println(lastPart); // prints "/232222232/pageid"
A slightly more sophisticated variant would be to let the URL class parse the url for you:
URL url = new URL("http://myurl.com/users/232222232/pageid");
String lastPart = url.getPath().substring(6);
System.out.println(lastPart); // prints "/232222232/pageid"
And, a third approach, using regular expressions:
String url = "http://myurl.com/users/232222232/pageid";
String lastPart = url.replaceAll(".*/users", "");
System.out.println(lastPart); // prints "/232222232/pageid"
string.replaceAll(".*/users(/.*/.*)", "$1");
String rest = url.substring(url.indexOf("/users/") + 6);
You can use split(String regex,int limit) which will split the string around the pattern in regex at most limit times, so...
String url="http://myurl.com/users/232222232/pageid";
String[] parts=url.split("/users",1);
//parts={"http://myurl.com","/232222232/pageid"}
String rest=parts[1];
//rest="/232222232/pageid"
The limit is there to prevent strings like "http://myurl.com/users/232222232/users/pageid" giving answers like "/232222232".
You can use String.indexOf() and String.substring() in order to achieve this:
String pattern = "/users/";
String url = "http://myurl.com/users/232222232/pageid";
System.out.println(url.substring(url.indexOf(pattern)+pattern.length()-1);

Problem in replacing special characters in a String in java

I got a String as response from server which is like the below:
hsb:\\\10.217.111.33\javap\Coventry\
Now I want to parse this string in such a way that I need to replace all \ with /.
Also I need to remove the first part of the String which is hsb:\\\
So, my resultant string should be of like this :
10.217.111.33/javap/coventry/
Can anyone help me by providing sample java code for this problem.
Here you have a "dirty" startup "solution":
String s = "hsb:\\\\\\10.217.111.33\\javap\\Coventry\\";
String w = s.replace('\\', '/');
String x = w.replace("hsb:///", "");
String result = yourString.substring(7);
result = result.replaceAll("\\\\", "/");

Categories

Resources