Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to parse strings better?

I'm wondering if there's a built in way in .NET to parse bits of a string.

Take for example I have the following string:

"bsarbirthd0692" 

made up of the following parts that will be cross referenced to data later:

Indexes   Purpose 0-3       (name) 4-9       (description) 10-13     (date mm-yy) 

I'm hoping for something native like:

string name, desc, date; string.ParseFormat("{0:4}{1:5}{2:4}", "bsarbirthd0692", out name, out desc, out date); 

Is there a native way to do this in .NET or a popular library?

like image 379
Sarah Bailey Avatar asked Apr 15 '15 15:04

Sarah Bailey


People also ask

Is there a parse for string?

Parsing String is the process of getting information that is needed in the String format. String parsing in java can be done by using a wrapper class. Using the Split method, a String can be converted to an array by passing the delimiter to the split method. The split method is one of the methods of the wrapper class.

Can you parse a string Java?

Strings in Java can be parsed using the split method of the String class. ( StringTokenizer can also be used to parse a string; we won't be covering it here).

What does it mean to parse a string?

To parse, in computer science, is where a string of commands – usually a program – is separated into more easily processed components, which are analyzed for correct syntax and then attached to tags that define each component.

How do you parse a string in Python?

Parse String to List With the str.split() function to split the string on the basis of each , . The str. split() function takes a delimiter/separator as an input parameter, splits the calling string based on the delimiter, and returns a list of substrings.


1 Answers

Since a format is known, and shouldn't change Substring should work for you

string data = "bsarbirthd0692"; string name, desc, date; name = data.Substring(0, 4); desc = data.Substring(4, 6); date = data.SubString(10); 

EDIT

There's also extension methods you can create to do what ever you want. This is obviously more complex than previous suggestion

public static class StringExtension {     /// <summary>     /// Returns a string array of the original string broken apart by the parameters     /// </summary>     /// <param name="str">The original string</param>     /// <param name="obj">Integer array of how long each broken piece will be</param>     /// <returns>A string array of the original string broken apart</returns>     public static string[] ParseFormat(this string str, params int[] obj)     {         int startIndex = 0;         string[] pieces = new string[obj.Length];         for (int i = 0; i < obj.Length; i++)         {             if (startIndex + obj[i] < str.Length)             {                 pieces[i] = str.Substring(startIndex, obj[i]);                 startIndex += obj[i];             }             else if (startIndex + obj[i] >= str.Length && startIndex < str.Length)             {                 // Parse the remaining characters of the string                 pieces[i] = str.Substring(startIndex);                 startIndex += str.Length + startIndex;             }              // Remaining indexes, in pieces if they're are any, will be null         }          return pieces;     } } 

Usage 1:

string d = "bsarbirthd0692"; string[] pieces = d.ParseFormat(4,6,4); 

Result:

enter image description here

Usage 2:

string d = "bsarbirthd0692"; string[] pieces = d.ParseFormat(4,6,4,1,2,3); 

Results:

enter image description here

like image 101
Shar1er80 Avatar answered Oct 11 '22 22:10

Shar1er80