getting number value from string - java

If I have the string
thisIsSomthing=4891\r\n
thisIsSomthingElse=27398472\r\n
thisIsNumber1=1\r\n
how would I find
thisIsNumber1
and then return 1 using regex

This assumes you really have that posted content in a string, and not in a file. As you're dealing with properties, you should use Properties and not a regex:
String yourString = ...
Properties prop = new Properties();
try {
prop.load(new StringReader(yourString));
String result = prop.getProperty("thisIsNumber1");
System.out.println(result);
} catch (IOException e) {
System.out.println("Error loading properties:");
e.printStackTrace();
}

/thisIsNumber1=(\d+).*/
It'll be in capture group 1.

String line="thisIsNumber1=1\r\n";
String temp=line.split("\\r?\\n")[0].split("=")[1];
System.out.println("Value="+temp+"*"); // 1* "*" shows nothing is concatenated after
the character in the output

Related

How to display backslash in json object value in java - Illegal escape JSONException

How to display a backslash in json value in java. I get a org.json.JSONException: Illegal escape. at 9 with the below sample code.
I'm using json 1.0.0 jar - org.json
String s1 = "{'Hi':'\\ksdfdsfsdfdfg'}";
int i = (int) '/';
System.out.println(s1);
try
{
JSONObject json = new JSONObject(s1);
}
catch (JSONException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
You need two backslashes to produce one backslash in a Java string literal "\\", and you need to double the backslash to get a backslash in the JSON string (since JavaScript has similar rules about backslash escapes and string literals as Java), thus, you need four backslashes:
String s1 = "{'Hi':'\\\\ksdfdsfsdfdfg'}";
If you do this:
String s1 = "{'Hi':'\\\\ksdfdsfsdfdfg'}";
try {
JSONObject json = new JSONObject(s1);
System.out.println(json.get("Hi"));
} catch (JSONException e) {
e.printStackTrace();
}
It prints:
\ksdfdsfsdfdfg
I think, you use wrong quotation marks, use double quotation mark in JSON:
String s1 = "{\"Hi\":\"\\ksdfdsfsdfdfg\"}"
That should work.
You have to add 4 backslashes for this to work. If you just print the parsed json object value you will see 2 backslashes. But if you get the value from the JSONObject you will see only one.
String s1 = "{'Hi':'\\\\ksdfdsfsdfdfg'}";
int i = (int) '/';
System.out.println(s1);
try {
JSONObject json = new JSONObject(s1);
System.out.println(json);//this will print two backslashes
String s = (String) json.get("Hi");
System.out.println(s);//this will print only one
} catch (JSONException e) {
e.printStackTrace();
}

Check if a string contain characters with bad encoding

I receive a XML file with a tag whose value is "97ò00430 ò" while this tag initially contains only numbers. The encoding use is "ISO-8859-1".
How to detect the bad characters (ò...) in java, please ?
LNA
I guess you could use a Regex to check the format of your tag (here, "\d+" if you want numbers only).
public static String encode(String chr) {
try {
byte[] bytes = chr.getBytes("ISO-8859-1");
if (!validUTF8(bytes))
return chr;
return new String(bytes, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("No char" + e.getMessage());
}
}

How to ignore backslashes appearing in the properties file for characters like : and =

private static void createPropertiesFile() {
Properties prop = new Properties();
OutputStream output = null;
try {
output = new FileOutputStream(
"c://properties//xyz.properties");
// set the properties value
prop.setProperty("URL", hostName);
prop.store(output, null);
} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Sample data in properties file looks as below.
#Tue Oct 06 15:26:55 IST 2015
URL=jdbc\:sqlserver\://abc.xyz.net
My understand is that anything before first "=" is treated as key and anything after first "=" as treated as value. In the process, when characters like : and = are encountered,they are escaped with backslash, '\'.
Can anyone please help me on how to remove or restrict '\' from appearing in first place in properties file when encountered with : and =
This is by design. Properties files will treat = and : as key/value delimiters.
To make it explicit as to which part is the key and which is the value the '=' and ':' characters, if included in either part, must be escaped.
Consider the following:
Key: somepassword
Value: Xj993a==
Your properties file will look like:
somepassword=Xj993a==
Unfortunately, where is the key and where is the value? The key could be:
somepassword with value Xj993a==
somepassword=Xj993a with value =
somepassword=Xj993a== with empty value
The parsing of this would be ambiguous at best. Now if we escape the '=' characters:
somepassword=Xj993a\=\=
This is now EXPLICITLY clear as to which is the key and which is the value.
This could also easily have been written as:
somepassword:Xj993a\=\=
Please read the documentation of java.util.Properties.load(java.io.Reader) for more information on the escapes allowed and parsing semantics of properties files.

Java Problems encoding UTF8

I think the easiest way to explain my problem is with a little example:
My string at the beginning is: Pâtes, and the result should be: Pâtes. What I get as result is still Pâtes How can I fix this?
Here the code:
private String encode(String string) {
try {
byte ptext[] =string.getBytes("UTF8");
string = new String(ptext, "UTF8");
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return string;
}
There are two problems with your code. The first is that you're using UTF8, but the correct character set is UTF-8.
The second is that you're essentially performing a no op. By calling byte ptext[] =string.getBytes("UTF-8"); you are saying that this string is UTF-8. Then you convert it to UTF-8 which it already is.
What I think you mean is that the input is ISO-8859-1 and you want to convert it to UTF-8. (This fits with the example input and output you've given).
Try:
private String encode(String string) {
try {
byte ptext[] = string.getBytes("ISO-8859-1");
string = new String(ptext, "UTF-8");
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return string;
}
This assumes that your initial string was originally read from somewhere and only contains ISO-8859-1 characters. As mentioned in a comment you should try to ensure the data is loaded in correctly from the source (i.e. when it is still just an array of bytes).

java concatenate two strings error

I have one function that returns me String :
public String getString(String password){
......
try {
.......
encodedPassword = Base64.encodeToString(msgDigest,1 );
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return encodedPassword;
}
I want to add (concatenate) "=" String to returning string from function
I try using this:
encrptdPassword = getString("1234");
encrptdPassword = encrptdPassword+"=";
Or:
encrptdPassword = encrptdPassword .concat("=");
but I get result like two different objects (space or brake between)
I think problem is in Base64.encodeToString , but I must use 64 based string
Function getString returns me:
A6xnQhbz4Vx2HuGl4lXwZ5U2I8iziLRFnhP5eNfIRvQ
I want to add = to the returning string as:
A6xnQhbz4Vx2HuGl4lXwZ5U2I8iziLRFnhP5eNfIRvQ=
but I receive this on output
A6xnQhbz4Vx2HuGl4lXwZ5U2I8iziLRFnhP5eNfIRvQ =
Or:
A6xnQhbz4Vx2HuGl4lXwZ5U2I8iziLRFnhP5eNfIRvQ
=
...like 2 different strings.
Where I'm wrong?
I assume you're using Base64 from Apache Commons Codec.
The default constructor for this class uses "\r\n" as a line separator, which it adds to the end of every encoded line. If you don't want this, construct the object as:
new Base64(76, '');
If this isn't the class you're calling (it looks like from your code sample you're calling a static method), check the API and see if you can set a line separator for the conversion.
Isn't the 1 in Base64.encodeToString(msgDigest,1 ) padding?
If it's not, then you could just trim() the string to remove the whitespace.

Categories

Resources