Jump to content

Welcome to Obsidian Forum Community
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. If you already have an account, login here - otherwise create an account for free today!
Photo

C# Split String Is Not Working


  • Please log in to reply
21 replies to this topic

#1
Xarzu

Xarzu

    (1) Prestidigitator

  • Members
  • 20 posts
C# Split String Is Not Working
======================

Please Help. I wrote a C# program that reads code one line at a time. Each line has data seperated by tabs.

Here is what my C# code is supposed to do. The line is supposed to be split into a string array by using the "Split" string methodIt is supposed to be spit read in text one line at a time and I use the "Split" method to group the line according to the tab character.

But, as this screen shot shows, it does not work. Any idea why?

Posted Image

#2
mkreku

mkreku

    Arch-Mage

  • Members
  • 8092 posts
  • Location:Uppsala, Sweden
It may be a syntax problem.

Maybe try something like:

string[] arr = line.Split(new char [] {'\t'});


#3
Nightshape

Nightshape

    (12) Mage

  • Members
  • 1851 posts
  • Location:Warsaw, Poland
Go go mkreku, I thought you couldn't code.
  • Ailove likes this

#4
Malcador

Malcador

    Arch-Mage

  • Members
  • 5395 posts
  • Location:Someplace in Canada
  • Xbox Gamertag:Pft, consoles.
  • Silver Backer
  • Kickstarter Backer
Are you sure it's tabs in the string and not spaces ?

#5
Nightshape

Nightshape

    (12) Mage

  • Members
  • 1851 posts
  • Location:Warsaw, Poland

Are you sure it's tabs in the string and not spaces ?


It's a matter of being certain what the delimiter should be, t, or n or a symbol. I tend to use a symbol so I can be explicit.

#6
Humodour

Humodour

    Arch-Mage

  • Members.
  • 3563 posts

Please Help. I wrote a C# program


I found your problem.

#7
Prosper

Prosper

    (4) Theurgist

  • Members
  • 329 posts
Show us line. All in your editor see if you can set a view of hex for the line string so we can be sure of all the characters.

#8
Prosper

Prosper

    (4) Theurgist

  • Members
  • 329 posts
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace LineSplit
{
    public partial class Form1 : Form
    {
	    public Form1()
	    {
		    InitializeComponent();
	    }
	    private void button1_Click(object sender, EventArgs e)
	    {
		    richTextBox1.Clear();
		    string inputLine = textBox1.Text;
		    string[] lines = inputLine.Split('\t');
		   
		    foreach(string line in lines) {
			    richTextBox1.AppendText("\n"); // skips a line
			    richTextBox1.AppendText(line);
		    }
		   
	    }
	    private void button2_Click(object sender, EventArgs e)
	    {
		    textBox1.Text += '\t';
	    }
    }
}

Posted Image
http://i.imgur.com/fBhiu.png

Works for me. Are you using mono?

#9
Nightshape

Nightshape

    (12) Mage

  • Members
  • 1851 posts
  • Location:Warsaw, Poland

Please Help. I wrote a C# program


I found your problem.


You're not still evangelising Python, hell next you'll tell me you think Javascript is awesome.

C++ or fail dude.

#10
Malcador

Malcador

    Arch-Mage

  • Members
  • 5395 posts
  • Location:Someplace in Canada
  • Xbox Gamertag:Pft, consoles.
  • Silver Backer
  • Kickstarter Backer
Language snobbery is so undergrad. Still though, really should get back practicing my C++ and C#.
  • Khorde likes this

#11
Humodour

Humodour

    Arch-Mage

  • Members.
  • 3563 posts


Please Help. I wrote a C# program


I found your problem.


You're not still evangelising Python, hell next you'll tell me you think Javascript is awesome.

C++ or fail dude.


Eh, go away. Nobody takes you seriously anyway. Still, you accidentally stumbled upon a grain of wisdom. "C++ or fail" indeed. In this case the OP chose fail.

P.S. Javascript is pretty annoying, but maybe if you try Coffeescript you'll still be a douchebag, but at least you'll be a douchebag using a nice language. What, are you going to tell me people should code interactive websites in C++? lol

Language snobbery is so undergrad. Still though, really should get back practicing my C++ and C#.


And yet it's what Ph.Ds and industry veterans will often spend their time writing papers on.

In this case, Microsoft themselves have rapidly ditched C# in favour of a return C++ for the cases where they were using it (which were cases where they used C++ originally anyway - turns out C# isn't a suitable replacement). This is poignant because Microsoft are the ones who designed C# in the first place, and for this main purpose (to replace C++ internally).

Anybody still using C# in light of this is just unaware of better languages for their needs - C++ and Python for instance, depending on the solution required.

Edited by Krezack, 26 August 2012 - 11:53 PM.


#12
Malcador

Malcador

    Arch-Mage

  • Members
  • 5395 posts
  • Location:Someplace in Canada
  • Xbox Gamertag:Pft, consoles.
  • Silver Backer
  • Kickstarter Backer
They spend their time sneering at other languages ? :p

Wonder if the guy fixed his problem though, which was getting this function to work as intended.

#13
Janmanden

Janmanden

    (6) Magician

  • Members
  • 718 posts
  • Location:Харьков
Lots of ideas here. That's what he asked for. Maybe the reason why the split doesn't work is because those four options are getting along so well, that they just don't want to be split.. Maybe they are currently discussing what ruleset they should use for their next adventure.. I suggest making a check for tabs before trying to split a string based on that. Make a regular expression and check to see if there any tabs in there or possible multiple spaces and apply the split based on what the result is. Could be tabs, could be spaces.. could it be love?
  • Humodour likes this

#14
J.Burke

J.Burke

    Programmer

  • Developers
  • 156 posts
  • PSN Portable ID:Cadius
  • Steam:Cadius
Oh hi, code talk.

#15
Roby Atadero

Roby Atadero

    Programmer

  • Developers
  • 64 posts
  • Backer
  • Kickstarter Backer
False alarm Burke, everyone left... :(

#16
Humodour

Humodour

    Arch-Mage

  • Members.
  • 3563 posts
What languages does Obsidian use? Just LUA and C++?

#17
DABhand

DABhand

    (0) Nub

  • Members
  • 3 posts


Please Help. I wrote a C# program


I found your problem.


You're not still evangelising Python, hell next you'll tell me you think Javascript is awesome.

C++ or fail dude.


>Insinuating C++ is the king of all languages
>2012

Yeah I know no green text here.

I could equally say to you

ASM or fail dude.

But I am sure that you may lie about knowing ASM fully just like your claim to invent the crate 3.0 technology which is car engine tech, which wasn't invented by a Swedish man, just sayin' :p

#18
Roby Atadero

Roby Atadero

    Programmer

  • Developers
  • 64 posts
  • Backer
  • Kickstarter Backer

What languages does Obsidian use? Just LUA and C++?


The games that have used Onyx consist of this: The engine is C++. Our tools are a combination of C++ and C# (all of our development is done using Windows machines). And our UI utilizes Actionscript (Flash).

I cannot say exactly what other languages besides C++ the projects before DS3 used but, the primary language of all of our released games has been C++.

#19
J.Burke

J.Burke

    Programmer

  • Developers
  • 156 posts
  • PSN Portable ID:Cadius
  • Steam:Cadius
Alpha protocol was made using Unreal, so we used C++ and unreal script.
Fallout New Vegas was made using Gambryo -- programmers worked almost exclusively in C++ (including tools work), though some XML work was required for UI.
Roby already went into Onyx projects.

#20
lingmaaki

lingmaaki

    (0) Nub

  • Members
  • 1 posts
Check this one

string str = null;
string[] strArr = null;
int count = 0;
str = "CSharp split test";
char[] splitchar = { ' ' };
strArr = str.Split(splitchar);
for (count = 0; count < = strArr.Length - 1; count++)
{
MessageBox.Show(strArr[count]);
}

http://csharp.net-in...tring-split.htm

maaki.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users