need a way to print a string in binary format? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string. It contains some blank space. I don't know what's these blank space. They might be a space, or be a tab.. or sth else.
So I just want to print a string in binary format.
Also, how to use String.replace() to filter out some characters in binary format? Or I need to use some other kind of API to filter out the special characters in binary format.
for example, how to filter out chr(10) in a string. ( chr(10) means a Chang Line)
Just find out its reason:
I use these line to print what's this whitespace:
for (char ch : MyString.toCharArray()) {
System.out.format("%H ", ch);
}
I got it's result. It's "3000".
I checked UTF-8 table and found 3000 really looks a blank space.
Then I just use this code to remove this blank space:
MyString = MyString.replaceAll("\u3000", "");
Now everything become ok.
Thanks a lot for everyone's help!!

Use regex. \s represents whitespace.
str.replace( "\\s+", "" );

Use String.toCharArray(), loop over all the char, use String.toString((int) char), 16) to get a hex value. Separate with " " or "," for readability.

Related

Splitting a string with advanced regex [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm trying to split a string of the following form using a regex:
"/say hello world (hello there) (how (are you))"
This should split into:
/say
hello
world
(hello there)
(how (are you))
Splitting on " "(space) obviously doesn't work, as I don't want to split strings inside brackets. How can something like this be achieved?
Also, worth noting: I'm using PCRE for parsing.
You can create your own parser for this:
1. Iterate over string characters to find " ", if you find ( increase one counter (create in your method) and when you find ) decrease it.
2. If you find " " and your counter is 0 then take substring till that point and add in an array, (reset internal variables to if any to work in loop).
3. if you find " " and counter is still not zero then ignore and go to step 1 again.

Regex to remove prefix and suffix in a string Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to remove prefix and suffix in a String and extract the middle portion of the string.
For eg: Consider the Strings - "www.hello.com" and "www.test.com"
Here prefix - "www." and suffix - ".com". I want to extract the middle words - hello and test.
Currently i have achieved this using String.replace() method in Java.
str.replace("www.","").replace(".com","");
I want know is there any regular expression available to achieve it in a single method in java.
You could use a regex for that, it would work in the same way. Your regex would simply contain a capture group with both the prefix and the suffix in an OR operation.
(www\.|\.com)
You could then use this like you did with the replace.
String test = "www.test.com";
String output = test.replaceAll("(www\\.|\\.com)","")
P.S. this code is untested. Please don't just copy and paste it expecting everything to work.
(?<=www.)(.*)(?=.com)
This uses the lookbehind and lookahead feature of regex

Is there any way to perform a lossless conversion from a String to a java class name? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to be able to encode any string into a valid java class name and then decode that class name back into the provided string. I want to be able to do this is a lossless manner, i.e., no two strings can be encoded to the same java class name.
Is this possible?
The answer to this is clearly no. There are only a finite number of possible Java strings, and not all of them are valid class names. Therefore, you're asking for a bijection between two sets of unequal cardinalities - which naturally doesn't exist.
This is certainly possible.
In any situation where you need to convert arbitrary strings to use a limited set of characters, you simply need to invent an escape sequence.
For example, pick _ as your escape character, then replace any invalid character, or any underscore, in the source string with an underscore followed by 8 hex digits of the character's Unicode codepoint.

how to represent complement of a character(eg A bar) in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am creating a project "test" Which takes questions set by teacher and student has to answer them.What if teacher has to set boolean expression questions The question appear in label in swing.That is why I have told swing That is why I want to know how to represent A bar(complement of A) in label in frame.is there a way to do it?
You can use Unicode, the numbering of all kind of characters in the World. Under Windows or Linux there is a char map utility which which to look after characters:
You will find U+XXXX codes show, where XXXX is a hexadecimal number (base 16: 0-9A-F).
You can use this number in Java as \uXXXX.
Ā = "\u0100" U+0100
ā = "\u0101" U+0101
The bar, also called macron, also exists separately as "combining diacritical mark", a zero-width accent:
̄ = "\u0304"
In Char Map you can find other combining diacritical marks by selecting the thus named "Unicode subrange."
Hence
Ā == "\u0100" == "A\u304"
However is some fonts that may not look to good. But it would mean little work, to replace any letter with its bar-variant. The teacher types in "X-bar" and you replace it.
String s = ".... a-bar ... z-bar ...";
s = s.replaceAll("-bar\\b", "\u304");
The regex "\\b" is a word boundary marker, so you won't translate "milk-bars."
Maybe best would be to edit in Unicode, say UTF-8. As ≤ ≠ ≈ ∞ probably are also desired. This means setting the editor and java compiler to use UTF-8. For separate text editing, there exist free NotePad++ and JEdit.
Labels in Swing support HTML, and the overline character entity can be used in the HTML:
label.setText("<html>A̅");
See a HtmlDemo screenshot

How do I extract the following patterns in java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have string in following format:
String s = " some text....
[[Category:Anarchism| ]]
[[Category:Political culture]]
[[Category:Political ideologies]]
[[Category:Far-left politics]]
... some more text"
I want to extract all the categories from this text. [Anarchism,Political culture ....,Far-left politics]
Also, is there a good tutorial where I can learn about this regex pattern matching stuff..
Thanks
You can use the following regex to get categories:
\[\[Category:(.+)\]\]
Then you can access to your groups to get the category values.
Remember to add backslash to backslashes if you use on java strings:
\\[\\[Category:(.+)\\]\\]
You can see it working:
Assuming you don't want to select the word "Category" itself, the regex would be:
(?<=Category:).*?(?=])
I'll break this down a bit for you.
The first bit in brackets looks for Category, without actually selecting it.
Next .+? looks for 1-infinity characters (other than a newline), but stops as soon as the next part is matched:
The final brackets tells it to look for a ], but without actually selecting it.
The results would be the bits below highlighted in blue.

Categories