How to split a string and save the 2 characters that I split with?

in python, a string can be split on a delimiter.
string split
python split string by character
split string python
python split string on multiple delimiters without regex
string split c#
split string into 3 parts python
python split on multiple delimiters

I am trying to split a given string using the java split method while the string should be devided by two different characters (+ and -) and I am willing to save the characters inside the array aswell in the same index the string has been saven.

for example :

input : String s = "4x^2+3x-2"

output : arr[0] = 4x^2 arr[1] = +3x arr[2] = -2

I know how to get the + or - characters in a different index between the numbers but it is not helping me, any suggestions please?

You can face this problem in many ways. I´m sure there are clever and fancy ways to split this expression. I will show you the simplest problem-solving process that can help you.

State the problem you need to solve, the input and output

Problem: Split a math expression into subexpressions at + and - signals 
Input: 4x^2+3x-2
Output: 4x^2,+3x,-2

Create a pseudo code with some logic you might think works

Given an expression string
Create an empty list of expressions
Create a subExpression string
For each character in the expression
Check if the character is + ou - then
add the subExpression in the list and create a new empty subexpression
otherwise, append the character in the subExpression
In the end, add the left subexpression in the list

Implement the pseudo-code in the programming language of your choice

    String expression = "4x^2+3x-2";
    List<String> expressions = new ArrayList();
    StringBuilder subExpression = new StringBuilder();
    for (int i = 0; i < expression.length(); i++) {
        char character = expression.charAt(i);
        if (character == '-' || character == '+') {
            expressions.add(subExpression.toString());
            subExpression = new StringBuilder(String.valueOf(character));
        } else {
            subExpression.append(String.valueOf(character));
        }
    }
    expressions.add(subExpression.toString());
    System.out.println(expressions);

Output

[4x^2, +3x, -2]

You will end with one algorithm that works for your problem. You can start to improve it.

Python, Splitting characters in String data) tells Python to split the variable data on the characters: , or _ or – or ! This is a bit more arcane form but saves time. \W, Any character that is not a letter, numeric digit, or the underscore character. There are many ways to split a string in Java. The most common way is using the split() method which is used to split a string into an array of sub-strings and returns the new array. 1. Using String.split ()¶ The string split() method breaks a given string around matches of the given regular expression. There are two variants of split() method

Try this code:

String s = "4x^2+3x-2";

s = s.replace("+", "#+");
s = s.replace("-", "#-");
String[] ss = s.split("#");

for (int i = 0; i < ss.length; i++) {
    Log.e("XOP",ss[i]);
}

This code replaces + and - with #+ and #- respectively and then splits the string with #. That way the + and - operators are not lost in the result.

If you require # as input character then you can use any other Unicode character instead of #.

Split the string into substrings using delimiter, function to split string into substrings on the. // basis of delimiter and return the substrings. // after split. vector<string> splitStrings(string str, char dl). {. string word � The String.Split() method splits a string into an array of strings separated by the split delimeters. The split delimiters can be a character or an array of characters or an array of strings. The code examples in this article discuss various forms of String.Split method and how to split strings using different delimiters in C# and .NET.

Try this one:

String s = "4x^2+3x-2";
String[] arr = s.split("[\\+-]");
for(int i=0;i<arr.length;i++){
 System.out.println(arr[i]);
}

C# Split String Examples, Part 2: This program splits on a single character. The result value from Split is a string array—it contains 4 elements. Char. Part 3: The foreach-loop iterates over� The Regex.Split method is almost identical to String.Split, except that it splits a string based on a regular expression pattern instead of a fixed character set.For example, the following example uses the Regex.Split method to split a string that contains substrings delimited by various combinations of hyphens and other characters.

Personally I like it better to have positive matches of patterns, especially if the split pattern itself is empty.

So for instance you could use a Pattern and Matcher like this:

Pattern p = Pattern.compile("(^|[+-])([^+-]*)");
Matcher m = p.matcher("4x^2+3x-2");
while (m.find()) {
    System.out.printf("%s or %s %s%n", m.group(), m.group(1), m.group(2));
}

This matches the start of the string or a plus or minus: ^|[+-], followed by any amount of characters that are not a plus or minus: [^+-]*.

Do note that the ^ first matches the start of the string, and is then used to negate a character class when used between brackets. Regular expressions are tricky like that.

Bonus: you can also use the two groups (within the parenthesis in the pattern) to match the operators - if any.

All this is presuming that you want to use/test regular expressions; generally things like this require a parser rather than a regular expression.


A one-liner for persons thinking that this is too complex:

var expressions = Pattern.compile("^|[+-][^+-]*")
        .matcher("4x^2+3x-2")
        .results()
        .map(r -> r.group())
        .collect(Collectors.toList());

Split strings at delimiters - MATLAB split, Split names at whitespace characters, making it a 3-by-2 string array. names = split(names). names = 3x2 string "Mary" "Butler"� The string split() method breaks a given string around matches of the given regular expression. For Example: Input String: 016-78967 Regular Expression: - Output : {"016", "78967"} Following are the two variants of split() method in Java: 1. Public String [ ] split ( String regex, int limit )

How to split a string at every nth character in Python, Splitting a string at every nth character returns a list of strings that are of add that previously described string to a list. a_string = "abcde". split_strings = []. n = 2 . Given a string, write a Python program to split the characters of the given string into a list. Examples: Input : geeks Output : ['g', 'e', Save. Recommended Posts:

How to use C# string Split - CSharp, C# Split() handles splitting upon given string and character delimiters. It returns an array of String containing the substrings delimited by the given System.Char� Output: Geek Geeks 2. Split(Char[], Int32, StringSplitOptions) Method. This method is used to splits a string into a maximum number of substrings based on the characters in an array.

Java String split() Method with examples, Java String split method explained with the help of examples: Splitting based on word, multiple characters, special characters, whitespace etc. 2. String[] split( String regex, int limit) : This Java String split method is used when� It is tested on both a long string and a short string. Version 2: Uses the string.Split method, but with the first argument being a char array. Two chars are in the char array. Version 3: Uses string.Split as well, but with a string array argument. The 3 versions are compared.

Comments
  • Does (?<=[^+-])(?=[+-]) work?
  • This answer uses a parser solution rather than a regular expression, which is - in the end - probably what you are looking for.
  • also you can't lose + or - from your array
  • thanks you @MaartenBodewes for your suggestion appreciate it
  • thanks for help @MaartenBodewes now my answer look like some experienced write it thank you so much brother
  • This both matches an earlier answer and it has the same problem of not matching the output requirements.