I have a string in which each value is separated using "|"as following:
698301 | 48380.80 | sam | aass@gmail.com | 5675767 | 3 | 40602.80 | 7778
I want to split this string at once without using array or split function in c# and want to store each splitted value in different variables so that i can use these variables for my code later for example:
a= 69801, b= 48380.80, c= sam, d= aass@gmail.com, e= 5675767, f= 3, g=40602.80, h=7778
i have tried "indexOf"
function but it is not working as expected.
code which i have used:
var hyphenIndex = str.IndexOf("|");
var a = str.Substring(0, hyphenIndex);
MessageBox.Show(a.ToString());
var b = str.Substring(1, hyphenIndex);
MessageBox.Show(b.ToString());
var c = str.Substring(2, hyphenIndex);
MessageBox.Show(c.ToString());
var d = str.Substring(3, hyphenIndex);
MessageBox.Show(d.ToString());
var e = str.Substring(4, hyphenIndex);
MessageBox.Show(e.ToString());
var f = str.Substring(5, hyphenIndex);
MessageBox.Show(f.ToString());
var g = str.Substring(6, hyphenIndex);
MessageBox.Show(g.ToString());
var h = str.Substring(7, hyphenIndex);
MessageBox.Show(h.ToString());
I would create a function that can get the value of a column by index, then reuse that as needed. Here is an example:
public string GetValue(string row, int index)
{
var start = 0;
for (var i = 0; i < index; i++)
start = row.IndexOf('|', start + 1);
var end = row.IndexOf('|', start + 1);
if (end == -1)
end = row.Length;
return row.Substring(start, end - start)
.Replace("|", string.Empty).Trim();
}
var str = "698301 | 48380.80 | sam | aass@gmail.com | 5675767 | 3 | 40602.80 | 7778";
var a = GetValue(str, 0);
var b = GetValue(str, 1);
var c = GetValue(str, 2);
var d = GetValue(str, 3);
var e = GetValue(str, 4);
var f = GetValue(str, 5);
var g = GetValue(str, 6);
var h = GetValue(str, 7);
You're very close, you just have to make sure you're cutting off the part of the string that you used, and updating your index with the new location of the first |
in the string. Changing the name to delimiterIndex
just because calling a pipe a hyphen bugs me:P
Make special note that we have to go one past the index provided by IndexOf
when doing our substring to make sure we have cut out the |
from the result string. When we provide the length for the substring, we must take this extra 1 character into account as it will reduce the overall length of the resultant string.
string s = "698301 | 48380.80 | sam | aass@gmail.com | 5675767 | 3 | 40602.80 | 7778";
string a, b, c, d, e, f, g, h;
int delimiterIndex = s.IndexOf('|');
a = s.Substring(0, delimiterIndex);
s = s.Substring(delimiterIndex + 1 , s.Length - delimiterIndex - 1);
delimiterIndex = s.IndexOf('|');
b = s.Substring(0, delimiterIndex);
s = s.Substring(delimiterIndex + 1 , s.Length - delimiterIndex - 1);
delimiterIndex = s.IndexOf('|');
c = s.Substring(0, delimiterIndex);
s = s.Substring(delimiterIndex + 1 , s.Length - delimiterIndex - 1);
delimiterIndex = s.IndexOf('|');
d = s.Substring(0, delimiterIndex);
s = s.Substring(delimiterIndex + 1 , s.Length - delimiterIndex - 1);
delimiterIndex = s.IndexOf('|');
e = s.Substring(0, delimiterIndex);
s = s.Substring(delimiterIndex + 1 , s.Length - delimiterIndex - 1);
delimiterIndex = s.IndexOf('|');
f = s.Substring(0, delimiterIndex);
s = s.Substring(delimiterIndex + 1 , s.Length - delimiterIndex - 1);
delimiterIndex = s.IndexOf('|');
g = s.Substring(0, delimiterIndex);
s = s.Substring(delimiterIndex + 1 , s.Length - delimiterIndex - 1);
h = s;
As Chris Dunaway suggested, this can be further refined to remove the substrings of the source string using the overload for IndexOf which takes a start index. This would look like the following
string s = "698301 | 48380.80 | sam | aass@gmail.com | 5675767 | 3 | 40602.80 | 7778";
string a, b, c, d, e, f, g, h;
int lastIndex = 0;
int delimiterIndex = s.IndexOf('|', 0);
a = s.Substring(lastIndex, delimiterIndex);
lastIndex = delimiterIndex;
delimiterIndex = s.IndexOf('|', delimiterIndex+1);
b = s.Substring(lastIndex + 1, delimiterIndex - lastIndex - 1);
lastIndex = delimiterIndex;
delimiterIndex = s.IndexOf('|', delimiterIndex+1);
c = s.Substring(lastIndex + 1, delimiterIndex - lastIndex - 1);
lastIndex = delimiterIndex;
delimiterIndex = s.IndexOf('|', delimiterIndex+1);
d = s.Substring(lastIndex + 1, delimiterIndex - lastIndex - 1);
lastIndex = delimiterIndex;
delimiterIndex = s.IndexOf('|', delimiterIndex+1);
e = s.Substring(lastIndex + 1, delimiterIndex - lastIndex - 1);
lastIndex = delimiterIndex;
delimiterIndex = s.IndexOf('|', delimiterIndex+1);
f = s.Substring(lastIndex + 1, delimiterIndex - lastIndex - 1);
lastIndex = delimiterIndex;
delimiterIndex = s.IndexOf('|', delimiterIndex+1);
g = s.Substring(lastIndex + 1, delimiterIndex - lastIndex - 1);
lastIndex = delimiterIndex;
h = s.Substring(lastIndex + 1);
If I am allowed to answer with a free open source Nuget package, here is a quick example using WordParser.
Nuget Package:
(.Net Framework) DataJuggler.Core.UltimateHelper
(Dot Net Core) DataJuggler.UltimateHelper.Core
// source string
string source = "698301 | 48380.80 | sam | aass@gmail.com | 5675767 | 3 | 40602.80 | 7778";
// use this char to split on
char[] delimiterChars = { '|' };
// get the words
List<Word> words = WordParser.GetWords(source, delimiterChars);
// If the words collection exists and has one or more items
if (ListHelper.HasOneOrMoreItems(words))
{
// Iterate the collection of Word objects
foreach (Word word in words)
{
// Do something with each word
Console.WriteLine(word.Text);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.