Split it into an array - c#

I have this string of proxy addresses, they are separated by an space, however x400 and x500 handles spaces into their addresses. What's the best approach to split it.
e.g.
smtp:john#a-mygot.com smtp:john#b-mygot.com smtp:john#c-mygot.com X400:C=us;A= ;P=mygot;O=Exchange;S=John;G=Gleen; SMTP:john#mygot.com
Expected result:
smtp:john#a-mygot.com
smtp:john#b-mygot.com
smtp:john#c-mygot.com
X400:C=us;A= ;P=mygot;O=Exchange;S=John;G=Gleen;
SMTP:john#mygot.com
thanks,
EDIT,
string mylist = "smtp:john#a-mygot.com smtp:john#b-mygot.com smtp:john#c-mygot.com X400:C=us;A= ;P=mygot;O=Exchange;S=John;G=Gleen; SMTP:john#mygot.com X500:/o=Example/ou=USA/cn=Recipients of /cn=juser smtp:myaddress";
string[] results = Regex.Split(mylist, #" +(?=\w+:)");
foreach (string part in results)
{
Console.WriteLine(part);
}
Result
smtp:john#a-mygot.com
smtp:john#b-mygot.com
smtp:john#c-mygot.com
X400:C=us;A= ;P=mygot;O=Exchange;S=John;G=Gleen;
SMTP:john#mygot.com
X500:/o=Example/ou=USA/cn=Recipients of /cn=juser
smtp:myaddress

Here is a Regex that should match the spaces before protocols. Try plugging it into Regex.Split like so:
string[] results = Regex.Split(input, #" +(?=\w+:)");

int index = smtp.indexOf("X400") ;
string[] smtps = smtpString.SubString(0,index).Split(" ") ;
int secondIndex = smtpString.indexOf("SMTP");
string xfour = smtpString.substring(index,secondIndex);
string lastString = smtpString.indexOf(secondIndex) ;
Should work, if the string format is that way.. and if I didn't screw up the indexes.. although you might want to check if the index isn't -1

Try this:
public static string[] SplitProxy(string text)
{
var list = new List<string>();
var tokens = text.Split(new char[] { ' ' });
var currentToken = new StringBuilder();
foreach (var token in tokens)
{
if (token.ToLower().Substring(0, 4) == "smtp")
{
if (currentToken.Length > 0)
{
list.Add(currentToken.ToString());
currentToken.Clear();
}
list.Add(token);
}
else
{
currentToken.Append(token);
}
}
if (currentToken.Length > 0)
list.Add(currentToken.ToString());
return list.ToArray();
}
It splits the string by spaces into tokens then goes through them one by one. If the token starts with smtp it is added to the result array. If not, that token is concatted with the following tokens to create one entry than is added to the result array. Should work with anything that has spaces and doesn't start with smtp.

I reckon the following line should do the work
var addrlist = variable.Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries);

Related

Get first word on every new line in a long string?

I am trying to add a leaderboard in my unity app
I have a long string as below(just an example, actual string is http pipe data from my web service, not manually stored):
string str ="name1|10|junk data.....\n
name2|9|junk data.....\n
name3|8|junk data.....\n
name4|7|junk data....."
I want to get the first word (string before the first pipe '|' like name1,name2...) from every line and store it in an array and then get the numbers (10,9,8... arter the '|') and store it in an other one.
Anyone know whats the best way to do this?
Fiddle here: https://dotnetfiddle.net/utp4HK
code below, you may want to revisit the algorithm for performance, but if that is not an issue, this will do the trick;
using System;
public class Program
{
public static void Main()
{
string str ="name1|10|junk data.....\nname2|9|junk data.....\nname3|8|junkdata.....\nname4|7|junk data.....";
foreach (var line in str.Split('\n'))
{
Console.WriteLine(line.Split('|')[0]);
}
}
}
First split by new-line characters:
string[] lines = str.Split(new string[]{Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
Then you can use LINQ to get both arrays:
var data = lines.Select(l => l.Trim().Split('|')).Where(arr => arr.Length > 1);
string[] names = data.Select(arr => arr[0].Trim()).ToArray();
string[] numbers = data.Select(arr => arr[1].Trim()).ToArray();
Check out this link on splitting strings: http://msdn.microsoft.com/en-us/library/ms228388.aspx
You could first create an array of strings (one for each line) by splitting the long string with \n as the delimeter.
Then, you could split each line with | as the delimeter. The name would be the 0th index of the array and the number would be the 1st index of the array.
First of all, you can't have a multi line string without using verbatim string literal. With using verbatim string literal, you can split your string based on \r\n or Environment.NewLine like;
string str = #"name1|10|junk data.....
name2|9|junk data.....
name3|8|junk data.....
name4|7|junk data.....";
var array = str.Split(new []{Environment.NewLine},
StringSplitOptions.RemoveEmptyEntries);
foreach (var item in array)
{
Console.WriteLine(item.Split(new[]{"|"},
StringSplitOptions.RemoveEmptyEntries)[0].Trim());
}
Output will be;
name1
name2
name3
name4
Try this:
string str ="name1|10|junk data.....\n" +
"name2|9|junk data.....\n" +
"name3|8|junk data.....\n" +
"name4|7|junk data.....";
string[] tempArray1 = str.Split('\n');
string[] tempArray2 = null;
string[,] newArray = null;
for (int i = 0; i < tempArray1.Length; i++)
{
tempArray2 = tempArray1[i].Split('|');
if (newArray[0, 0].ToString().Length == 0)
{
newArray = new string[tempArray1.Length, tempArray2.Length];
}
for (int j = 0; j < tempArray2.Length; j++)
{
newArray[i,j] = tempArray2[j];
}
}

Parse for words starting with # character in a string

I have to write a program which parses a string for words starting with '#' and return the words along with the # symbol.
I have tried something like:
char[] delim = { '#' };
string[] strArr = commenttext.Split(delim);
return strArr;
But it returns all the words without '#' in an array.
I need something pretty straight forward.No LINQ like things
If the string is "abc #ert #xyz" then I should get back #ert and #xyz.
If you define "word" as "separated by spaces" then this would work:
string[] strArr = commenttext.Split(' ')
.Where(w => w.StartsWith("#"))
.ToArray();
If you need something more complex, a Regular Expression might be more appropriate.
I need something pretty straight forward.No LINQ like things>
The non-Linq equivalent would be:
var words = commenttext.Split(' ');
List<string> temp = new List<string>();
foreach(string w in words)
{
if(w.StartsWith("#"))
temp.Add(w);
}
string[] strArr = temp.ToArray();
If you're against using Linq, which you should not be unless you're required to use older .NET versions, an approach along these lines would suit your needs.
string[] words = commenttext.Split(delimiter);
for (int i = 0; i < words.Length; i++)
{
string word = words[i];
if (word.StartsWith(delimiter))
{
// save in array / list
}
}
const string test = "#Amir abcdef #Stack #C# mnop xyz";
var splited = test.Split(' ').Where(m => m.StartsWith("#")).ToList();
foreach (var b in splited)
{
Console.WriteLine(b.Substring(1, b.Length - 1));
}
Console.ReadKey();

Extract node value from xml resembling string C#

I am having strings like below
<ad nameId="\862094\"></ad>
or comma seprated like below
<ad nameId="\862593\"></ad>,<ad nameId="\862094\"></ad>,<ad nameId="\865599\"></ad>
How to extract nameId value and store in single string like below
string extractedValues ="862094";
or in case of comma seprated string above
string extractedMultipleValues ="862593,862094,865599";
This is what I have started trying with but not sure
string myString = "<ad nameId="\862593\"></ad>,<ad nameId="\862094\"></ad>,<ad
nameId="\865599\"></ad>";
string[] myStringArray = myString .Split(',');
foreach (string str in myStringArray )
{
xd.LoadXml(str);
chkStringVal = xd.SelectSingleNode("/ad/#nameId").Value;
}
Search for:
<ad nameId="\\(\d*)\\"><\/ad>
Replace with:
$1
Note that you must search globally. Example: http://www.regex101.com/r/pL2lX1
Please see code below to extract all numbers in your example:
string value = #"<ad nameId=""\862093\""></ad>,<ad nameId=""\862094\""></ad>,<ad nameId=""\865599\""></ad>";
var matches = Regex.Matches(value, #"(\\\d*\\)", RegexOptions.RightToLeft);
foreach (Group item in matches)
{
string yourMatchNumber = item.Value;
}
Try like this;
string s = #"<ad nameId=""\862094\""></ad>";
if (!(s.Contains(",")))
{
string extractedValues = s.Substring(s.IndexOf("\\") + 1, s.LastIndexOf("\\") - s.IndexOf("\\") - 1);
}
else
{
string[] array = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
string extractedMultipleValues = "";
for (int i = 0; i < array.Length; i++)
{
extractedMultipleValues += array[i].Substring(array[i].IndexOf("\\") + 1, array[i].LastIndexOf("\\") - array[i].IndexOf("\\") - 1) + ",";
}
Console.WriteLine(extractedMultipleValues.Substring(0, extractedMultipleValues.Length -1));
}
mhasan, here goes an example of what you need(well almost)
EDITED: complete code (it's a little tricky)
(Sorry for the image but i have some troubles with tags in the editor, i can send the code by email if you want :) )
A little explanation about the code, it replaces all ocurrences of parsePattern in the given string, so if the given string has multiple tags separated by "," the final result will be the numbers separated by "," stored in parse variable....
Hope it helps

Retrieve String Containing Specific substring C#

I am having an output in string format like following :
"ABCDED 0000A1.txt PQRSNT 12345"
I want to retreieve substring(s) having .txt in above string. e.g. For above it should return 0000A1.txt.
Thanks
You can either split the string at whitespace boundaries like it's already been suggested or repeatedly match the same regex like this:
var input = "ABCDED 0000A1.txt PQRSNT 12345 THE.txt FOO";
var match = Regex.Match (input, #"\b([\w\d]+\.txt)\b");
while (match.Success) {
Console.WriteLine ("TEST: {0}", match.Value);
match = match.NextMatch ();
}
Split will work if it the spaces are the seperator. if you use oter seperators you can add as needed
string input = "ABCDED 0000A1.txt PQRSNT 12345";
string filename = input.Split(' ').FirstOrDefault(f => System.IO.Path.HasExtension(f));
filname = "0000A1.txt" and this will work for any extension
You may use c#, regex and pattern, match :)
Here is the code, plug it in try. Please comment.
string test = "afdkljfljalf dkfjd.txt lkjdfjdl";
string ffile = Regex.Match(test, #"\([a-z0-9])+.txt").Groups[1].Value;
Console.WriteLine(ffile);
Reference: regexp
I did something like this:
string subString = "";
char period = '.';
char[] chArString;
int iSubStrIndex = 0;
if (myString != null)
{
chArString = new char[myString.Length];
chArString = myString.ToCharArray();
for (int i = 0; i < myString.Length; i ++)
{
if (chArString[i] == period)
iSubStrIndex = i;
}
substring = myString.Substring(iSubStrIndex);
}
Hope that helps.
First split your string in array using
char[] whitespace = new char[] { ' ', '\t' };
string[] ssizes = myStr.Split(whitespace);
Then find .txt in array...
// Find first element starting with .txt.
//
string value1 = Array.Find(array1,
element => element.Contains(".txt", StringComparison.Ordinal));
Now your value1 will have the "0000A1.txt"
Happy coding.

Split string with ' in C#

How to split this string
1014,'0,1031,1032,1034,1035,1036',0,0,1,1,0,1,0,-1,1
and get this string array as result
1014
'0,1031,1032,1034,1035,1036'
0
0
1
1
0
1
0
-1
1
in C#
I believe that this regex should give you what you are looking for:
('(?:[^']|'')*'|[^',\r\n]*)(,|\r\n?|\n)?
http://regexr.com?2vib4
EDIT:
Quick code snippet on how it might work:
var rx = new Regex("('(?:[^']|'')*'|[^',\r\n]*)(,|\r\n?|\n)?");
var text= "1014,'0,1031,1032,1034,1035,1036',0,0,1,1,0,1,0,-1,1";
var matches = rx.Matches(text);
foreach (Match match in matches)
{
System.Console.WriteLine(match.Groups[1].ToString());
}
try this,
string line ="1014,'0,1031,1032,1034,1035,1036',0,0,1,1,0,1,0,-1,1" ;
var values = Regex.Matches(line, "(?:'(?<m>[^']*)')|(?<m>[^,]+)");
foreach (Match value in values) {
Console.WriteLine(value.Groups["m"].Value);
}
This code is not pretty at all, but it works. :) (Does not work with multiple "strings" within the string.)
void Main()
{
string stuff = "1014,'0,1031,1032,1034,1035,1036',0,0,1,1,0,1,0,-1,1";
List<string> newStuff = new List<string>();
var extract = stuff.Substring(stuff.IndexOf('\''), stuff.IndexOf('\'', stuff.IndexOf('\'') + 1) - stuff.IndexOf('\'') + 1);
var oldExtract = extract;
extract = extract.Replace(',',';');
stuff = stuff.Replace(oldExtract, extract);
newStuff.AddRange(stuff.Split(new[] {','}));
var newList = newStuff;
for(var i = 0; i < newList.Count; i++)
newList[i] = newList[i].Replace(';',',');
// And newList will be in the format you specified, but in a list..
}
Firstly split a string on ' (single) quote and then after go for comma (,).
You don't need a parser, you don't need Regex. Here's a pretty simple version that works perfectly:
var splits = input
.Split('\'')
.SelectMany(
(s,i) => (i%2==0)
? s.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries)
: new[]{ "'" + s + "'"}
);
This is exactly what #AVD + #Rawling described ... Split on ', and split only "even" results, then combine.
using System;
using System.IO;
using Microsoft.VisualBasic.FileIO; //Microsoft.VisualBasic.dll
public class Sample {
static void Main(){
string data = "1014,'0,1031,1032,1034,1035,1036',0,0,1,1,0,1,0,-1,1";
string[] fields = null;
data = data.Replace('\'', '"');
using(var csvReader = new TextFieldParser(new StringReader(data))){
csvReader.SetDelimiters(new string[] {","});
csvReader.HasFieldsEnclosedInQuotes = true;
fields = csvReader.ReadFields();
}
foreach(var item in fields){
Console.WriteLine("{0}",item);
}
}
}

Categories