Get a specific data values from a string in Java (String without comma)

how to add comma separated values in string in java
java split string by comma and space
convert integer list to comma separated string java 8
java list to comma separated string with quotes
how to add comma in string in java
how to read comma separated values in java
regex for comma separated string java
convert comma separated string to map java 8

I want to get the value from a string.

I have a string value like this:

String myData= "Number: 34678 Type: Internal Qty: 34";

How can I get the Number, Type, Qty values separately?

Give me any suggestion on this.

Input:

String myData= "Number: 34678 Type: Internal Qty: 34";

Output:

Number value is 34678 
Type values is Internal 
Qty value is 34

Here is one way to do it. It looks for a word following by a colon followed by zero or more spaces followed by another word. This works regardless of the order or names of the fields.

      String myData = "Number: 34678 Type: Internal Qty: 34";
      Matcher m = Pattern.compile("(\\S+):\\s*(\\S+)").matcher(myData);
      while (m.find()) {
         System.out.println(m.group(1) + " value is " + m.group(2));
      }

Convert a Set of String to a comma separated String in Java , Given a Set of String, the task is to convert the Set to a comma separated String in Java. Start learning Data Sructures & Algorithms with the help of the most trusted DSA Self Please Improve this article if you find anything incorrect by clicking on the "Improve first_page Remove extra delimiter from a String in Java. Next. Form a comma separated String from the Set of String using join() method by passing comma ‘, ‘ and the set as parameters. Print the String. Below is the implementation of the above approach:

You can use regex to do this cleanly:

Pattern p = Pattern.compile("Number: (\\d*) Type: (.*) Qty: (\\d*)");
Matcher m = p.matcher(myData);
m.find()

So you'll get the number with m.group(1), the Type m.group(2) and the Qty m.group(3).

I assume you accept a limited number of types. So you can change the regex to match only if the type is correct, for eg. either Internal or External: "Number: (\\d*) Type: (Internal|External) Qty: (\\d*)"

Here's a nice explanation of how this works

Convert a List to a Comma-Separated String in Java 8, Agile · AI · Big Data · Cloud · Database · DevOps · Integration · IoT · Java · Microservices Converting a List<String> to a String with all the values of the List comma For old times' sake, let's have a look at the Java 7 implementation: and easier to make mistakes like forgetting to remove the last comma. Java split string by comma example shows how to split string by comma in Java. The example also shows how to handle CSV records having a comma between double quotes or parentheses. How to split String by comma in Java? You can split string by comma in Java using the split method of the String class.

If you just want to print them with fixed pattern of input data, a simplest way is shown as follows: (Just for fun!)

System.out.print(myData.replace(" Type", "\nType")
            .replace(" Qty", "\nQty")
            .replace(":", " value is"));

split - Manual, Return Values ¶. Returns an array of strings, each of which is a substring of string formed by For example, if there is no occurrence of pattern , an array with only one For split(), when using a backslash as the delimiter, you have to *double It seemed to split on a comma even though it was between a pair of quotes. Since splitting a String is a very common functionality, Java designers have provided a couple of split() method on java.lang.String class itself. These split() function takes a regular expression and split the String accordingly. In order to parse a comma delimited String, you can just provide a "," as a delimiter and it will return an array of String containing

I suppose the string is always formatted like that. I.e., n attribute names each followed by a value that does not contain spaces. In other words, the 2n entities are separated from each other by 1 or more spaces.

If so, try this:

String[] parts;
int      limit;
int      counter;
String   name;
String   value;
parts = myData.split("[ ]+");
limit = (parts.length / 2) * 2; // Make sure an even number of elements is considered
for (counter = 0; counter < limit; counter += 2)
{
  name = parts[counter].replace(":", "");
  value = parts[counter + 1];
  System.out.println(name + " value is " + value);
}

Comma-separated values, A comma-separated values (CSV) file is a delimited text file that uses a comma to separate A delimiter that is not present in the field data (such as tab) keeps the format If a particular CSV file's variations fall outside what a particular receiving commas or spaces for delimiters, so unquoted character strings could not  How to create a thread without implementing the Runnable interface in Java? How to read the data from a properties file in Java? How to instantiate a static inner class with reflection in Java? Will a finally block execute after a return statement in a method in Java? C program to find the length of a string?

This Should work

    String replace = val.replace(": ", "|");

    StringBuilder number = new StringBuilder();
    StringBuilder type = new StringBuilder();
    StringBuilder qty = new StringBuilder();
    String[] getValues = replace.split(" ");
    int i=0;
    while(i<getValues.length-1){
        String[] splitNumebr = getValues[i].split("\\|");
        number.append(splitNumebr[1]);
        String[] splitType = getValues[i+=1].split("\\|");
        type.append(splitType[1]);
        String[] splitQty = getValues[i+=1].split("\\|");
        qty.append(splitQty[1]);
    }
    System.out.println(String.format("Number value is %s",number.toString()));
    System.out.println(String.format("Type value is %s",type.toString()));
    System.out.println(String.format("Qty value is %s",qty.toString()));

}

Output Number value is 34678 Type value is Internal Qty value is 34

Parsing Strings with split, parsing: dividing a string into tokens based on the given delimiters; token: one when reading in data from a file on a line-by-line basis), then we will need to parse Strings in Java can be parsed using the split method of the String class. In this case, we have just one delimiter (comma) and consecutive delimiters (​i.e.,  Joe Harry yes, I have to check whether the Info array values are in Compare_val. I have to iterate the Compare_val over the Info array and find what values are matching. Ahmed Bin S : 3) you have a pretty dog and many cats, and not a pretty dogs and many cat - the third statement must have pretty 'dogs' and many cat and thats why it should

Scala split string and get last, For the times we want to format numbers/values into our string, we can make use of such as a string I get from a comma-separated value (CSV) or pipe-delimited file. Given a balanced string s split it in the maximum amount of balanced strings. Scala makes use of java strings without creating a separate string class​. This tutorial shows several ways for converting a comma-separated String to a List in Java.. 1- Java 7. With Java 7 and older versions, the typical way of converting a comma-separated String to a List is through splitting the String by the comma “,” delimiter and then generating a List using Arrays.asList() as the following:

How to Convert Python List to String with Examples, Check out the most efficient methods to convert Python List to String. It could be a list of strings, characters, and numbers string_token => It is also a string such as a space ' ' or comma "," etc. Let's say we have a list of month names. See the example given below: It is not possible to use the join() function on this list. Using String.substring() method. Approach: Get the Strings and the index. Create a new String; Insert the substring from 0 to the specified (index + 1) using substring(0, index+1) method.

Converting JavaScript Arrays to Strings, The join method converts the elements of an array to strings, concatenates them, a comma will be placed between the values in the resulting string, as shown here: You can include an empty string separator if you do not want any separation contained only string values, but arrays can contain a variety of data types. In this post, we will see how to convert comma-separated String to List in Java. 1. Arrays.asList + split(). The idea is to split the string using split() function and pass the resultant array into Arrays.asList() method which returns a fixed-size List backed by an array.

Comments
  • You could use a regex to parse the string.
  • I don't have any idea about regex. Do you give any example for regex?
  • Thanks your code is helpful. But I am facing some errors arrayindexofbound. My input is, String mydata = "Number: 332W3314-64 Type: Internal Qty: 3 Exact
  • Updated the answer so it considers only the actual pairs of name/value