What is the best way to remove a hard enter from a String?
Input:
String in= "strengthened columns
with GRPES
";
Expected output: strengthened columns with GRPES
I tried the below code, but it's not working for me.
in = in.replaceAll("\\r\\n","");
System.out.println(in);
Unless you don't have a specific reason to use java-7 today, Here's a solution using java 13 or above
String in= """
strengthened columns
with GRPES
""";
in = in.replaceAll("\\n","");
System.out.println(in);
I have observed the question is tagged with java-7, do let me know if you are looking for a solution specific to the version
Actually you don't escape standard escape sequences when you use regexes. Also you don't want to specify an order of escape sequences - you just want to eliminate any type of line separator, so
in = in.replaceAll("[\r\n]","");
With later versions of Java, that could probably be
in = in.replaceAll("\\R","");
Related
What I want is a class code like ACCT 4838.
I tried
String REGEX = "[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z][\\s][\\d][\\d][\\d][\\d]";
String REGEX = "[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z]\\s\\d\\d\\d\\d"
I apologize if this gets flagged i have been looking around for a while and i cant quite peg what it is im doing wrong. should be a quick one for someone.
You can use a regex like this:
(?i)^[a-z]{4} \d{4}$ // With inline insensitive flag
^[A-Za-z]{4} \d{4}$ // without inline flag
Remember to escape backslashes in java like ^[A-Za-z]{4} \\d{4}$
IdeOne example
Below works. In java the single \ gives an error. I was stupidly feeding in the wrong string in addition to not having the proper code.
String REGEX = "^[a-zA-Z][a-zA-Z][a-zA-Z][a-zA-Z]\s\d\d\d\d";
So, please bear with me as I have a long question here, I have some code in java that is using an array list to implement a stack. I need to be able to enter the command "push" to add stuff to the stack. However my problem is that it has to be in the format pushSTUFF.
Where the "STUFF" is anything, upper case, lower case, string, int, etc.. The way I've been trying to implement this is with the string split method where PUSH is the delimiter. Then the command is passed to a switch case.
I quickly realized that the split gets discarded, at least as far as I can tell, and that the switch case is getting pushSTUFF not push as the case input.
In contemplating this problem I came up with a couple of ways I could do this. I just don't know if they are possible or how to do them.
So,
Is there a way to split a string like pushSTUFF and keep both parts (the push and the STUFF)
Is there a way to split, from a string, something of unknown length or contents (since I don't know what the user will input the STUFF is unknown)
Is there a way to tell the switch case to look for the pushSTUFF as opposed to just push (again because STUFF is unknown).
Are any of these even possible to do? If so what would you recommend?
I'm sure there are better ways but as I'm still learning java these seemed like the best for right now. Also I didn't post any code because I didn't feel it was necessary to the question. I will post some if you need it though. Just ask and I will be happy to oblige.
(tl;dr) Is it possible to do any of 1, 2, or 3 above and if so how?
Thanks in advance.
Instead of splitting the strings, you can use regular expressions with groups and iterate over the matching parts of it (as you saw, the split character(s) get discarded).
For #1, you could do something like (pseudocode):
regex = (push)(.*)
stuff = groups[1]
That should also cover #2 since it will match all characters after the push.
I'm not entirely sure what you're asking in #3.
There is a regex tutorial here if you're not familiar with java regular expressions.
You can also take a look at the StringTokenizer, which has an option to keep delimiters.
If the format will always be push*SOMETHING* why aren't you using String.substring()?
You can do:
String something = "pushSTUFF".substring(4);
This way, you will always get whatever is behind push.
I really don't understand what you are trying to achieve without seeing the actual code, but your problem seems simple enough to be solved this way.
Use .indexOf and find push:
public class splitstring {
public static void main(String[] args){
String tosplit, part1, part2 = new String();
int ind = 0;
tosplit = "push1234";
ind = tosplit.indexOf("push");
part1 = tosplit.substring(ind,ind + 4);
part2 = tosplit.substring(ind + 4, tosplit.length());
}
}
You can search for any Uppercase letter and use String.substring(...)
Find if first character in a string is upper case, Java
I have a number of files and they are all called something like name_version_xyz.ext.
In my Java code I need to extract the name and the version part of the filename. I can accomplish this using lastIndexOf where I look for underscore, but I don't think that's the nicest solution. Can this be done with a regexp somehow?
Note that the "name" part can contain any number of underscores.
If you are guaranteed to having the last part of your files named _xyz.ext, then this is really the cleanest way to do it. (If you aren't guaranteed this, then, you will need to figure out something else, of course.)
As the saying goes with regular expressions:
If you solve you a problem with regular expressions, you now have two problems.
You could use Regex but I think it is a bit overkill in this case. So I personally would stick with your current solution.
It is working, not too complicated and that's why I don't see any reasons to switch to another approach.
If you don't want to use regular expression I think the easiest solution is when you retrieve files and get only part without extension and then:
String file = "blah_blah_version_123";
String[] tmp = file.split("_version_");
System.out.println("name = " + tmp[0]);
System.out.println("version = " + tmp[1]);
Output:
name = blah_blah
version = 123
Yes, the regexp as a Java string would just look something like (untested)
(.+)_(\\d+)_([^_]+)\\.(???)
"name" would be group(1), "version" group(2), xyz is group(3), and ext is group(4).
Use a string tokeniser:
http://download.oracle.com/javase/1.4.2/docs/api/java/util/StringTokenizer.html
Or alternatively, String.split():
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#split%28java.lang.String%29
I'm trying to replace several different characters with different values. For example, if I have: #love hate then I would like to do is get back %23love%20hate
Is it something to do with groups? i tried to understand using groups but i really didn't understand it.
You can try to do this:
String encodedstring = URLEncoder.encode("#love hate","UTF-8");
It will give you the result you want. To revers it you should do this:
String loveHate = URLDecoder.decode(encodedstring);
You don't need RegEx to replace single characters. RegEx is an overkill for such porposes. You can simply use the plain replace method of String class in a loop, for each character that you want to replace.
String output = input.replace("#", "%23");
output = output.replace(" ", "%20");
How many such characters do you want to get replaced?
If you are trying to encode a URL to utf-8 or some encoding using existing classes will be much easier
eg.
commons-httpclient project
URIUtil.encodeWithinQuery(input,"UTF-8");
No, you will need multiple replaces. Another option is to use group to find the next occurrence of one of several strings, inspect what the string is and replace appropriately, perhaps using a map.
i think what you want to achieve is kind of url encoding instead of pure replacement.
see some answers on this thread of SO , especially the one with 7 votes which may be more interesting for you.
HTTP URL Address Encoding in Java
As Mat said, the best way to solve this problem is with URLEncoder. However, if you insist on using regex, then see the sample code in the documentation for java.util.regex.Matcher.appendReplacement:
Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("one cat two cats in the yard");
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
}
m.appendTail(sb);
System.out.println(sb.toString());
Within the loop, you can use m.group() to see what substring matched and then do a custom substitution based on that. This technique can be used for replacing ${variables} by looking them up in a map, etc.
In my Java application I have been passed in a string that looks like this:
"\u00a5123"
When printing that string into the console, I get the same string as the output (as expected).
However, I want to print that out by having the unicode converted into the actual yen symbol (\u00a5 -> yen symbol) - how would I go about doing this?
i.e. so it looks like this: "[yen symbol]123"
I wrote a little program:
public static void main(String[] args) {
System.out.println("\u00a5123");
}
It's output:
¥123
i.e. it output exactly what you stated in your post. I am not sure there is not something else going on. What version of Java are you using?
edit:
In response to your clarification, there are a couple of different techniques. The most straightforward is to look for a "\u" followed by 4 hex-code characters, extract that piece and replace with a unicode version with the hexcode (using the Character class). This of course assumes the string will not have a \u in front of it.
I am not aware of any particular system to parse the String as though it was an encoded Java String.
As has been mentioned before, these strings will have to be parsed to get the desired result.
Tokenize the string by using \u as separator. For example: \u63A5\u53D7 => { "63A5", "53D7" }
Process these strings as follows:
String hex = "63A5";
int intValue = Integer.parseInt(hex, 16);
System.out.println((char)intValue);
You're probably going to have to write a parse for these, unless you can find one in a third party library. There is nothing in the JDK to parse these for you, I know because I fairly recently had an idea to use these kind of escapes as a way to smuggle unicode through a Latin-1-only database. (I ended up doing something else btw)
I will tell you that java.util.Properties escapes and unescapes Unicode characters in this manner when reading and writing files (since the files have to be ASCII). The methods it uses for this are private, so you can't call them, but you could use the JDK source code to inspire your solution.
Could replace the above with this:
System.out.println((char)0x63A5);
Here is the code to print all of the box building unicode characters.
public static void printBox()
{
for (int i=0x2500;i<=0x257F;i++)
{
System.out.printf("0x%x : %c\n",i,(char)i);
}
}