Split a string contain dash and minus sign - java

I have to split a string which contain dash character and minus sign.
I tried to split based on the unicode character (https://en.wikipedia.org/wiki/Hyphen#Unicode), still it considering minus sign same as dash character. How van I solve it?
Expected output
(coun)
(US)
-1
Actual output
(coun)
(US)
// actually blank line will print here but SO editor squeezing the blank line
1
public static void main(String[] args) {
char dash = '\u002D';
int i = -1;
String a = "(country)" + dash + "(US)" + dash + i;
Pattern p = Pattern.compile("\u002D", Pattern.LITERAL);
String[] m = p.split(a);
for (String s : m) {
System.out.println(s);
}
}
I guess some conversion happens during the string concatenation but not sure.
Any suggestion to solve this issue is welcome

The operation dash + i is evaluated as numeric addition.
I think your string should be
String a = "(country)" + dash + "(US)" + dash + "" + i;
to produce the output you described.

#anubhava was partially right, I was using the wrong unicode.
I should have used "\u2010". Now everything working as expected.
public static void main(String[] args) {
char dash = '\u2010';
int i = -1;
char dashesd = '-';
String a = "(coun)"+dash+"(US)"+dash+i;
System.out.println(a);
Pattern p = Pattern.compile("\u2010", Pattern.LITERAL);
String [] m= p.split(a);
for (String s : m) {
System.out.println(s);
}

Related

How to add spaces to left and right of chars in a string?

I'm trying to print out a string with spaces on either side of each char in the string
so if I have
String s = "abcde"
it would create something like this
a b c d e
with a space before the first char and three between each char.
I just haven't been able to find a way to do this with my knowledge.
Update
Updated requirement:
I failed to realize that I need something that add one place in front
of the first term and then 3 spaces between each term.
_0___0___0___0___0_ for example.
For the updated requirement, you can use yet another cool thing, String#join.
public class Main {
public static void main(String[] args) {
String s = "abcde";
String result = "_" + String.join("___", s.split("")) + "_";
System.out.println(result);
}
}
Output:
_a___b___c___d___e_
Original answer
There can be so many ways to do it. I find it easier to do it using Regex:
public class Main {
public static void main(String[] args) {
String s = "abcde";
String result = s.replaceAll(".", " $0 ");
System.out.println(result);
}
}
Output:
a b c d e
The Regex, . matches a single character and $0 replaces this match with space + match + space.
Another cool way is by using Stream API.
import java.util.Arrays;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String s = "abcde";
String result = Arrays.stream(s.split(""))
.map(str -> " " + str + " ")
.collect(Collectors.joining());
System.out.println(result);
}
}
Output:
a b c d e
A super simple example, that doesn't handle a multitude of potential input scenarios.
public static void main(String[] args)
{
String s = "abcde";
for (int i = 0; i < s.length(); ++i) {
System.out.print("_" + s.charAt(i));
}
System.out.println("_");
}
NOTE: used an underscore rather than a space in order to allow visual check of the output.
Sample output:
_a_b_c_d_e_
Rather than direct output, one could use a StringBuilder and .append to a builder instead, for example.
Using StringBuilder:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); ++i) {
sb.append('_').append(s.charAt(i));
}
sb.append('_');
System.out.println(sb.toString());
Based on a comment where the desired output is slightly different (two internal spaces, one leading and trailing space), this suggests an alternative approach:
public static String addSpace(String inp) {
StringBuilder sB = new StringBuilder();
String string = inp.trim();
String div = "___"; // spaces, or whatever
sB.append('_'); // add leading space
for(int index = 0; index < string.length(); ++index) {
sB.append(string.charAt(index))
.append(div); // two spaces
}
sB.setLength(sB.length() - (div.length() - 1) );
return (sB.toString());
}
NOTE: again using an underscore to allow for easier debugging.
Output when div is set to 3 underscores (or spaces):
_0___0___0___1___0___1___1___0_
You can define an empty string : result = “”;
Then go through the string you want to print with foreach loop With the function toCharArray()
(char character : str.toCharArray())
And inside this loop do ->
result += “ “ + character;
String result = s.chars().mapToObj(
Character::toString
).collect(Collectors.joining(" "));
Similar to the loop versions, but uses a Stream.
Another one liner to achieve this, by splitting the String into String[] of characters and joining them by space:
public class Main {
public static void main(String[] args) {
String s = "abcde";
System.out.println(" " + String.join(" ", s.split("")) + " ");
}
}
Output:
a b c d e
Edit:
The above code won't work for strings with Unicode codepoints like "👦ab😊", so instead of splitting on empty string, the split should be performed on regex: "(?<=.)".
public class Main {
public static void main(String[] args) {
String s = "abcde";
System.out.println(" " + String.join(" ", s.split("(?<=.)")) + " ");
}
}
Thanks to #saka1029 for pointing this out.
You can use Collectors.joining(delimiter,prefix,suffix) method with three parameters:
String s1 = "abcde";
String s2 = Arrays.stream(s1.split(""))
.collect(Collectors.joining("_+_", "-{", "}-"));
System.out.println(s2); // -{a_+_b_+_c_+_d_+_e}-
See also: How to get all possible combinations from two arrays?

Read string format and fetch required irregular data

I have a string format like this which is output of
readAllBytes(new String(Files.readAllBytes(Paths.get(data))
from a file
a+2 b+3 c+33 d+88 ......
My scenario is I want to get the data after c+" ". The position of c is not constant but c occurs only once. It may occur anywhere. My required value will always be after c+ only. The required size of value 33.....is also not constant. Can someone help me with the optimal code please? I think collections need to be used here.
You can use this regex which will let you capture the data you want,
c\+(\d+)
Explanation:
c+ matches a literal c character immediately followed by a + char
(\d+) captures the next digit(s) which you are interested in capturing.
Demo, https://regex101.com/r/jfYUPG/1
Here is a java code for demonstrating same,
public static void main(String args[]) {
String s = "a+2 b+3 c+33 d+88 ";
Pattern p = Pattern.compile("c\\+(\\d+)");
Matcher m = p.matcher(s);
if (m.find()) {
System.out.println("Data: " + m.group(1));
} else {
System.out.println("Input data doesn't match the regex");
}
}
This gives following output,
Data: 33
This code is extracting the value right after c+ up to the next space, or to the end of the string if there is no space:
String str = "a+2 b+3 c+33 d+88 ";
String find = "c+";
int index = str.indexOf(" ", str.indexOf(find) + 2);
if (index == -1)
index = str.length();
String result = str.substring(str.indexOf(find) + 2, index);
System.out.println(result);
prints
33
or in a method:
public static String getValue(String str, String find) {
int index = str.indexOf(find) + 2;
int indexSpace = str.indexOf(" ", index);
if (indexSpace == -1)
indexSpace = str.length();
return str.substring(index, indexSpace);
}
public static void main(String[] args) {
String str = "a+2 b+3 c+33 d+88 ";
String find = "c+";
System.out.println(getValue(str, find));
}

Parse words separate by |

I have a string #JSGF V1.0;grammar numbers;public <accion> = (one| two| three);
I want the numbers: one, two and three.
I did this String answer = res.substring(res.indexOf("(")+1,res.indexOf(")")); and obtain one| two| three, but Im having trouble in this part.
Ideas?
You can get the numbers as array using
String numbers[] = answer.split("\\s*\\|\\s*");
\\s*\\|\\s*: 0 or more spaces then | symbol and 0 or more spaces
split the answer on non-word characters:
public static void main(String[] args) {
String res = "JSGF V1.0;grammar numbers;public <accion> = (one| two| three);";
String answer = res.substring(res.indexOf("(") + 1, res.indexOf(")"));
String[] numbers = answer.split("[^\\w]+"); // split on non-word character
for (String number : numbers) {
System.out.println(number);
}
}
output:
one
two
three
String res = "(one| two| three);";
String answer = res.substring(res.indexOf("(")+1,res.indexOf(")"));
for(String str : answer.split("\\s*\\|\\s*")) {
System.out.println(str);
}

How to take a String of LastName, FirstName and return FirstInitial.LastName

Expected Input: Doe, John
Expected Output: J. Doe
public static void main(String[] args) {
String z = "Doe, John";
System.out.println(z);
String y = formatName(name);
System.out.println(y);
}
public static String formatName(String name) {
String str[] = name.split(",");
StringBuilder sb = new StringBuilder();
sb.append(str[1].charAt(0));
sb.append(". ");
sb.append(str[0]);
return sb.toString();
}
My output is not as expected.
Match (Optional) White Space with String.split Regular Expression
You have a space after the comma in your input, you could modify your regular expression in split from
String str[] = name.split(",");
to
String str[] = name.split(",\\s*");
to match and remove optional white-space. After I made the above change I ran your code, and got the (expected) output
Doe, John
J. Doe
Trim the Leading White Space
Alternatively, you could trim str[1] before getting the first character like
sb.append(str[1].trim().charAt(0)); //<-- will also remove leading space
Regular Expression With a Compiled Pattern
Another possible option is compiling a regex Pattern and using a Matcher like
// Match (and group) one more characters followed by a "," and
// optional whitespace. Then match (and group) one character followed
// any number of optional characters.
private static Pattern pattern = Pattern.compile("(.+),\\s*(.).*");
public static String formatName(String name) {
Matcher m = pattern.matcher(name);
if (m.matches()) {
return String.format("%s. %s", m.group(2), m.group(1));
}
return name;
}
Another simple way to get FirstInitial.LastName
Other than using split, you can use substring and based on the position of the comma ,, manipulate the name to get the output:
String s = "Doe, John";
s = s.replace(" ", ""); //remove spaces
int i = s.indexOf(","); //get pos of comma
String name = s.charAt(i+1) + ". " + s.substring(0, i); //create name
Output:
J. Doe
sb.append(str[1].charAt(0)); , index for charAt() should be 1 not 0 .
String str[] = name.split(","); will return [Doe, John], notice the space before second element.
better yet use split(", ")
I tried this based on what i understood. Use for loop and trim the items
public static String formatName(String name) {
String str[] = name.split(",");
for(int i = 0 ; i < str.length ; i++){
str[i] = str[i].trim();
//System.out.println("+"+str[i]+"+");
}
StringBuilder sb = new StringBuilder();
sb.append(str[1].charAt(0));
sb.append(".");
sb.append(str[0]);
sb.append(".");
return sb.toString().trim();
}

markdown algorithm java; converter problems

public class Md2html {
public static void main(String[] args) throws IOException {
String stringToConvert = new Scanner(System.in).nextLine();
System.out.println(convert(stringToConvert));
}
public static String convert(String str) {
if (str.equals("# "))
System.out.println(" ");
Pattern pattern = Pattern.compile("(#+[^#]+)");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
String str1 = matcher.group(1);
if(str1.replaceFirst("#+", "").length() == 0 ||
str1.replaceFirst("#+", "").matches("[\\s]+"))
continue;
int n = str1.length() - str1.replaceFirst("#+", "").length();
System.out.println("<h" + n + ">" + str1.substring(n) +
"</h" + n + ">");
double carac;
carac = str.charAt(0);
if(carac>65 & carac <=90) {
System.out.print("<p>");
System.out.println(str);
System.out.println("<p>");
}
}
return ("");
}
}
Ok, so now I have an algorithm that converts # to < h1> < h2> depending on the number of #...I'm now trying to add < p> at the beginning of a paragraph and < /p> at the end of it. For some reason,the second part of the converter, which is supposed to add < p> at the beginning and < /p> at the end of a paragraph doesnt seem to work (it's the code starting with double carac). Can someone tell me what I'm doing wrong???
You are printing two opening tags for a paragraph if the string starts with an uppercase letter and no closing tag. Replace
System.out.print("<p>");
System.out.println(str);
System.out.println("<p>");
with
System.out.print("<p>");
System.out.println(str);
System.out.println("</p>"); //<--here
Also, you should use a logical AND && instead of a bitwise AND & for boolean operations.
Also, String#charAt(int) returns a char, not a double. You are declaring carac as a double. Declare as a char instead.

Categories