i have a string like 123Prefix1pics.zip
i want to split it into 123 Prefix1 pics.zip and store them in different variables
i m trying to do it in c#,.net
jst litle bit confused on how to use split method
splitArray = Regex.Split(subjectString, #"(?<=\p{N})(?=\p{L})");
will work in C# to split in positions between a number (\p{N}) and a letter (\p{L}).
If you also want to split between a letter and a number, use
splitArray = Regex.Split(subjectString, #"(?<=\p{L})(?=\p{N})|(?<=\p{N})(?=\p{L})");
however, that splits your example too much.
You only want to split that one string? Too easy!
string filename = "123Prefix1pics.zip"
string part1 = "123"
string part2 = "Prefix1"
string part3 = "pics.zip"
Ok this is a joke, but it gives the right answer. Unless you generalise your splitting rule, or provide further examples, we can only guess.
You may be asking to make a string break after a numeral, but again I'm only guessing.
You can start with:
string filename = "123Prefix1pics.zip"
string part1 = filename.Substring(0, 3);
string part2 = filename.Substring(3, 7);
string part3 = filename.Substring(10, 4);
You can also note String.Split() needs a separator argument, like an ; or ,. As you don't have any separator, you can try two approaches:
Make sure all your filenames have same format; this way, you can to use Substring() to break your strings
You can identify a more general pattern, as "numbers, 7 characters plus 4 more characters" and to use a regular expression. This is a more advanced solution and can lead to maintenance problems;
I recommend you to stick with first option.
You can split it like this:
your ip in a string variable like this:
create a char vector
then a string vector
Code:
string theIP="this is string";
char[] separator={' '}; //you can put multiple separators
string[] result = theIP.Split(separator,StringSplitOptions.None);
this means result[0] is "this", result[1] is "is", and so on.
You can find a good tutorial about string splitting here:
http://goldwin-advertising.ro/index.php?option=com_content&view=article&id=10:splitstring&catid=3:howto&Itemid=5
Good luck!
Look like you want to split by fixed size.
So use yourString.Substring(0, 3);
Related
I have a string that is like the following:
string str = hello_16_0_2016;
What I want is to extract hello from the string. As in my program the string part can occur anywhere as it is autogenerated, so I cannot fix the position of my string.
For example: I can take the first five string from above and store it in a new variable.
But as occurring of letters is random and I want to extract only letters from the string above, so can someone guide me to the correct procedure to do this?
Could you just use a simple regular expression to pull out only alphabetic characters, assuming you only need a-z?
using System.Text.RegularExpressions;
var str = "hello_16_0_2016";
var onlyLetters = Regex.Replace(str, #"[^a-zA-Z]", "");
// onlyLetters = "hello"
I'd use something like this (uses Linq):
var str = "hello_16_0_2016";
var result = string.Concat(str.Where(char.IsLetter));
Check it out
Or, if performance is a concern (because you have to do this on a tight loop, or have to convert hundreds of thousands of strings), it'd probably be faster to do:
var result = new string(str.Where(char.IsLetter).ToArray());
Check it too
But as occurring of letters is random and I want to extract only
letters from the string above, so can someone guide me to the correct
procedure to do this?
The following will extract the first text, without numbers anywhere in the string:
Console.WriteLine( Regex.Match("hello_16_0_2016", #"[A-Za-z]+").Value ); // "hello"
I want to extract only those words between two commas.
So, if the string is Ab Java, DE, 78801 The answer must be DE
I have tried this code but it is not working
string search = "Ab Java, DE, 78801 ";
int index = search.IndexOf(",");
string result = search.Substring(search.IndexOf(",") ,index);
MessageBox.Show(result);
If your string has always 2 commas, you can use String.Split for it like;
string search = "Ab Java, DE, 78801 ";
Console.WriteLine(search.Split(',')[1]); // DE
Remember, this will you generate DE with an extra white space before it.
If you don't want that white space, you can use TrimStart() to remove it.
Console.WriteLine(search.Split(',')[1].TrimStart()); //DE
Your start and end in your Substring resolve to the same value.
Try using split and getting the second item, of course this assumes that your input always follows the pattern in your example. Otherwise you'll need to do something more complicated.
string[] searchItems = search.Split(',');
string result = searchItems[1].Trim(); // will output DE
try this
string[] splitedStr=search.Split(',');
string NewStr=splitedStr[1].ToString();
Assuming your string always has just two commas, then:
search.Split(", ")[1]
will give you the desired text.
Try this...
String str = "Ab Java, DE, 78801 ";
String[] myStrings = str.split(",");
String marco = myStrings[1];
Try This. It May work...
string[] arrayStr = search.Split(',');
int len = arrayStr.Length;
for(int i =1;i<=len-2;i++)
{
MessageBox.Show(result);
}
That is something I would have solved using regular expressions. It might be slower than the String.Split solutions but it is way easier to read - in particular if the pattern is going to evolve over time.
The solution would than look like this:
string search = "Ab Java, DE, 78801 ";
Regex r = new Regex(", *(.*?) *,", RegexOptions.Compiled);
Console.WriteLine(r.Match(search).Groups[1].ToString());
This writes DE without surrounding spaces. The regex instance should be a static member of your class, since I assume this is happening within a loop ...
I have the following strings:
string a = "1. testdata";
string b = "12. testdata xxx";
What I would like is to be able to extract the number into one string and the characters following the number into another. I tried using .IndexOf(".") and then remove, trim and
substrings. If possible I would like to find something simpler as I have this to do in a
lot of parts of my code.
if the format is always the same you could do:
a.Split('.');
Proposed solutions so far are not correct.
First, after Split('.') or Split(".") you will have space in the beginning of second substring.
Second, if you have more than one dot - you'll have to do something yet after the split.
More robust solution is below:
string a = "11. Test string. With dots.";
var res = a.Split(new[] {". "}, 2, StringSplitOptions.None);
string number = res[0];
string val = res[1];
Argument 2 specifies maximum number of strings to return. Thus when you have several dots - it will make a split only at the first.
string[]list = a.Split(".");
string numbers = list[0];
string chars = list[1];
i have strings in the form [abc].[some other string].[can.also.contain.periods].[our match]
i now want to match the string "our match" (i.e. without the brackets), so i played around with lookarounds and whatnot. i now get the correct match, but i don't think this is a clean solution.
(?<=\.?\[) starts with '[' or '.['
([^\[]*) our match, i couldn't find a way to not use a negated character group
`.*?` non-greedy did not work as expected with lookarounds,
it would still match from the first match
(matches might contain escaped brackets)
(?=\]$) string ends with an ]
language is .net/c#. if there is an easier solution not involving a regex i'd be also happy to know
what really irritates me is the fact, that i cannot use (.*?) to capture the string, as it seems non-greedy does not work with lookbehinds.
i also tried: Regex.Split(str, #"\]\.\[").Last().TrimEnd(']');, but i'm not really pround of this solution either
The following should do the trick. Assuming the string ends after the last match.
string input = "[abc].[some other string].[can.also.contain.periods].[our match]";
var search = new Regex("\\.\\[(.*?)\\]$", RegexOptions.RightToLeft);
string ourMatch = search.Match(input).Groups[1]);
Assuming you can guarantee the input format, and it's just the last entry you want, LastIndexOf could be used:
string input = "[abc].[some other string].[can.also.contain.periods].[our match]";
int lastBracket = input.LastIndexOf("[");
string result = input.Substring(lastBracket + 1, input.Length - lastBracket - 2);
With String.Split():
string input = "[abc].[some other string].[can.also.contain.periods].[our match]";
char[] seps = {'[',']','\\'};
string[] splitted = input.Split(seps,StringSplitOptions.RemoveEmptyEntries);
you get "out match" in splitted[7] and can.also.contain.periods is left as one string (splitted[4])
Edit: the array will have the string inside [] and then . and so on, so if you have a variable number of groups, you can use that to get the value you want (or remove the strings that are just '.')
Edited to add the backslash to the separator to treat cases like '\[abc\]'
Edit2: for nested []:
string input = #"[abc].[some other string].[can.also.contain.periods].[our [the] match]";
string[] seps2 = { "].["};
string[] splitted = input.Split(seps2, StringSplitOptions.RemoveEmptyEntries);
you our [the] match] in the last element (index 3) and you'd have to remove the extra ]
You have several options:
RegexOptions.RightToLeft - yes, .NET regex can do this! Use it!
Match the whole thing with greedy prefix, use brackets to capture the suffix that you're interested in
So generally, pattern becomes .*(pattern)
In this case, .*\[([^\]]*)\], then extract what \1 captures (see this on rubular.com)
References
regular-expressions.info/Grouping with brackets
string temp_constraint = row["Constraint_Name"].ToString();
string split_string = "FK_"+tableName+"_";
string[] words = Regex.Split(temp_constraint, split_string);
I am trying to split a string using another string.
temp_constraint = FK_ss_foo_ss_fee
split_string = FK_ss_foo_
but it returns a single dimension array with the same string as in temp_constraint
Please help
Your split operation works fine for me:
string temp_constraint = "FK_ss_foo_ss_fee";
string split_string = "FK_ss_foo_";
string[] words = Regex.Split(temp_constraint, split_string);
foreach (string word in words)
{
Console.WriteLine(">{0}<", word);
}
Output:
><
>ss_fee<
I think the problem is that your variables are not set to what you think they are. You will need to debug to find the error elsewhere in your program.
I would also avoid using Split for this (both Regex and String.Split). You aren't really splitting the input - you are removing a string from the start. Split might not always do what you want. Imagine if you have a foreign key like the following:
FK_ss_foo_ss_fee_FK_ss_foo_ss_bee
You want to get ss_fee_FK_ss_foo_ss_bee but split would give you ss_fee_ and ss_bee. This is a contrived example, but it does demonstrate that what you are doing is not a split.
You should use String.Split instead
string[] words =
temp_constraint.Split(new []{split_string}, StringSplitOptions.None);
string split uses a character array to split text and does the split by each character which is not often ideal.
The following article shows how to split text by an entire word
http://www.bytechaser.com/en/functions/ufgr7wkpwf/split-text-by-words-and-not-character-arrays.aspx