How can I read this string in Java? [closed] - java

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 7 years ago.
Improve this question
I have a string such as this: "xxxxxxx , yyyyyyy - zzzzzz"
and sometimes it happens to be "xxxxxxx - zzzzzz"
the length of x may vary.
What I want to get, is always the x word.
Is there an easy way to do this?
What I thought was:
Iterate through the string, append to a stringbuilder every char I read until I read a ",", then break the iteration and I get the word, but this looks pretty messy.
So maybe there is an easier way to do this.

Here are several ways to get the first character:
String str = "x , y - zzzzzz";
System.out.println(str.charAt(0));
System.out.println(str.substring(0, 1));
System.out.println(str.replaceAll("^(.).*", "$1"));
See IDEONE demo
Choose the one you like more :)
UPDATE:
To find the first word, you may use the following regex pattern:
String pattern = "[\\s\\p{P}]+";
\s stands for whitespace, and \p{P} stands for punctuation.
You can use split with this as in
String str = "xxxxxxx , yyyyyyy - zzzzzz";
System.out.println(str.split(pattern)[0]);
str = "xxxxxxx - zzzzzz";
System.out.println(str.split(pattern)[0]);
str = "xxxxxxx, zzzzzz";
System.out.println(str.split(pattern)[0]);
str = "xxxxxxx-zzzzzz";
System.out.println(str.split(pattern)[0]);
All will output xxxxxxx. However, if you need to restrict the punctuation to some smaller subset, either exclude them in the &&[^-] negated subtraction:
[\\s\\p{P}&&[^-]]+
Or just define your own range:
[\\s.,;:]+
Just another demo

Related

String a[] = s.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)"); 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 5 years ago.
Improve this question
I am confuse in the logic behind the code (?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)")
it is separating numbers and alphabets like input String abc12dc23 then it is spliting it as output abc 12 dc 23.
I just want the explanation how the above code is working?
This regex:
(?<=\D)(?=\d)|(?<=\d)(?=\D)
matches 2 kinds of patterns, as suggested by the | character:
This pattern:
(?<=\D)(?=\d)
and this pattern:
(?<=\d)(?=\D)
The former looks for a position in the string where there is a non-digit (\D) character before that position and a digit (\d) after it. The latter looks for a position where the reverse happens, a digit before and a non-digit after.
To say this in a more abstract way, the regex is looking for digit-non-digit boundaries.
The split method looks for all occurrences of the pattern and splits the string when it finds one.

Splitting a comma-separated string but ignoring commas in whole word [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 7 years ago.
Improve this question
i have a string like:
String test = "firstName:a,lastName:b,addressOne:line 1,line 2,city:other";
i am trying to do test.replaceAll(",","\",\"").I mean replace , with ",". I want to this only for the whole strings. For eg addressOne is a single string with comma seperated i dont want to replace that. Is there any regex or someother way i can do this?
i should get a string like
"firstName":"a","lastName":"b","addressOne":"line 1 , line 2","city":"other"
after replace , but i am getting
"firstName":"a","lastName":"b","addressOne":"line 1 "," line 2","city":"other".
You can split your string with commas that are followed by a word which has been followed by :. And for this aim you can use a positive look-ahead and for matching the leading word use a negated character class [^:,]+ which will match any string except : and ,:
test.split(",(?=[^:,]+:)")

Blank space in array with String splitter [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 7 years ago.
Improve this question
My objective is to separate the numbers from the string, but in my array's first position i get a blank space. So i need help for that not to happen.
str1 = "Y=9x1+29x2";
String[] split2 = str2.split("[^-?.?0-9]+");
Blank space at the start is due to presence of non-digit character at the start of your input.
You can remove all non-digits at start before splitting:
String linha = "Y=9x1+29x2";
String[] split = linha.replaceFirst("[^-.\\d]+", "").split("[^-.\\d]+");
for (String tok: split2)
System.out.println(tok);
Output:
9
1
29
2
I think your question is rather vague, but after looking at it, I'm guessing that you want to extract the numbers out of the string, where a "number" has this format: an optional minus sign, followed by an optional decimal point, followed by one or more digits. I suspect you also want to include numbers that have digits followed by a decimal point followed by more digits.
I'm guessing this is what you want, because of the ? you put in your regex. The problem is that inside square brackets, ? doesn't mean "optional", and it doesn't mean "zero or one of something". It means a question mark. The regex [^-?.?0-9] means "match one character that is not a digit, a period, a hyphen, or a question mark". A pattern in square brackets always matches one character, and you tell it what characters are OK (or, if you begin with ^, what characters are not OK). This kind of "character set" pattern never matches a sequence of characters. It just looks at one character at a time. If you put + after the pattern, it still looks at one character at a time; it just does so repeatedly.
I think what you're trying to do is to take a pattern that represents a number, and then say "look for something that doesn't look like that pattern", and you tried to do it by using [^...]. That simply will not work.
In fact, split() is the wrong tool for this job. The purpose of split is to break up a string whose delimiters match a given pattern. Using it when the strings you want to keep in the array match a given pattern doesn't work very well, unless the pattern is extremely simple. I recommend that you create a Matcher and use the find() method in a loop. find() is set up so that it can find all matching substrings of a string if you call it repeatedly. This is what you want to accomplish, so it's the right tool.

Need advice on the usage of .indexOf(); [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 some code:
firstWord = sentance.substring (0, sentance.indexOf(' '));
secondWord = sentance.substring(sentance.indexOf(' ') + 1);
the code is used for selecting the first word out of a string without the use of arrays.
However I am wondering if I can further fool-proof my code by implementing a safeguard so that if the user inputs two spaces, the code will take the group of white space and count it as one unit.
Is this possible without the use of arrays, or loops?
For example the user would input this:
"Hello 2spaces there"
the user accidentally inputted two spaces in the beginning which will mess the program up when it tries to take the second word i think.
remove multiple space with single space as :
String str="Hello world this is string";
str=str.replaceAll("\\s+", " ");
.......// do whatever you want
Your code will not take the first word out of string only if the first character of the string is a space, or before the first space is not a word, or there is no space, for example " hello" -> "", "!##! blah" -> "!##!", "asdasd" -> ""
y.indexOf(x) returns the index of the first occurrence of x in y.
Your solution is mostly foolproof, but it will fail to get the first word if there are spaces before it, or there is no whitespace in the specified string, because indexOf would return -1.
You should call the .trim() method on the string object you want to get the first word of, it will remove the whitespace around the string, and then add a single space character at the end of the string.
str = "Hello I'm your String";
String[] splited = str.split("\\s+");
You can use arrays they are not that bad.
If you really must avoid using an array, you could use sentance.replaceAll("\\s+", " "); first to collapse all sequences of consecutive whitespace into singleton spaces.
(Similarly, you would want to trim() leading and trailing whitespace as well.)
If you just want to remove trailing and leading whitespace use .trim()
str = str.trim()

How to write a regex for this expression [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 need to write a regex which only lets the (ä,ö,ü,..) int the array..the code bellow also lets white spaces in the array..example parts2[0] and parts2[1] has white spaces but supposed to be ö
String str="geliyoru mamaadsödsödösdä dädsdäsfäfsä yüwüsüdsüfsäfsfä"
String[] parts2 = str.split("[A-Za-z]+|[0-9]+|x[A-Fa-f0-9]");
StringBuilder result = new StringBuilder(str.length());
for (int i = 0; i < parts.length; i++) {
System.out.println(parts2[i]);
result.append(parts[i]);
}
You can use the Pattern.replaceAll feature of Java.
String str= "geliyoru mamaadsödsödösdä dädsdäsfäfsä yüwüsüdsüfsäfsfä";
Pattern p = Pattern.compile("[^\u00E4\u00F6\u00FC]*");
String result = p.matcher(str).replaceAll("");
In the pattern between [^ and ] you should enumerate all the characters you wish to preserve using the unicode definition. You can look them op for instance using Google/DuckDuckGo. The ^ part between the square brackets meens that you invert the matching. Thus all character not present in the group are matched. And in the last line all these characters are replaced by empty strings.
For performance, I've added * at the end of the regex, such that groups of characters are matched as one and thus replacement is done only once.
You can see a jdoodle here

Categories