473,811 Members | 3,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String.Substrin g result when non-alphanumeric character?

I just ran into a situation where string data from a mainframe contained a
couple of non-alphanumeric characters (hex CC and C8). I was parsing a field
that occurred after these unexpected characters and it appears the Substring
method was thrown off and returned a field two bytes off.

Does this data cause a problem with the Substring method?
Nov 16 '05 #1
3 2458
Bill <nf*@nospam.com > wrote:
I just ran into a situation where string data from a mainframe contained a
couple of non-alphanumeric characters (hex CC and C8). I was parsing a field
that occurred after these unexpected characters and it appears the Substring
method was thrown off and returned a field two bytes off.

Does this data cause a problem with the Substring method?


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Bill <nf*@nospam.com > wrote:
This test now appears to be an issue with the StreamReader dropping these
unexpected characters.


Ah - and that's almost certainly just because you haven't given it the
right encoding. You haven't specified an encoding, so it's using UTF-8,
which I don't believe is what you really wanted.

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information about encodings and Unicode.

You probably want to use Encoding.Defaul t in this case.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Bill <nf*@nospam.com > wrote:
This test now appears to be an issue with the StreamReader dropping these unexpected characters.


Ah - and that's almost certainly just because you haven't given it the
right encoding. You haven't specified an encoding, so it's using UTF-8,
which I don't believe is what you really wanted.

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information about encodings and Unicode.

You probably want to use Encoding.Defaul t in this case.


Yes... Encoding.Defaul t solved the problem.

The following modifications to my original test example works:

class Class1
{
static void Main(string[] args)
{
string str;

using (StreamWriter sro = new StreamWriter("P robDataOut.txt" ))
{
using (StreamReader sri = new StreamReader("P robDataIn.txt",
Encoding.Defaul t))
{
Console.WriteLi ne("---- TESTING FILE WITH PROBLEM CHARACTERS
HEX 'CC' and 'C8' ----");
while ( (str = sri.ReadLine()) != null )
{
Console.WriteLi ne("Length: " + str.Length);
Console.WriteLi ne("Output: " + str.Substring(1 84,1));
sro.WriteLine(s tr);
}
}
}
}
}
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4359
by: Sebek | last post by:
Hello, I'm transforming a XML document in XHTML but I have problems using sub-strings, it will be clearer with an exemple: What I have: <form href="identification.php?PHPSESSID=134134&page=2&param=3" > </form> what I want:
14
1502
by: SnakeS | last post by:
Hi, I want modify a string as "112233445566" in "11:22:33:44:55:66" which is the best method? Using a RegExp? and if yes how? Thanks in advance
8
5422
by: Jami Bradley | last post by:
Hi, I'm looking for an efficient way to do this, because I know it will be heavily used :-) I have a fixed width string and I need to substitute a substring of characters with new values. I can do this with 2 substring calls, but it will need to rebuild the string just to write a few characters. Here is the simple, but inefficient, version: string s = "0123456789";
32
2392
by: Tubs | last post by:
Am i missing something or does the .Net Framework have a quirk in the way methods work on an object. In C++ MFC, if i have a CString and i use the format method, i format the string i am using. In dotnet it always asks me to pass it the string. Why can't i just say "stringvariable.Format("0.00") and have it know what i mean. Is there a way to achieve this? What am i doing wrong
11
5363
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not (strIn.Substring(410, 10).Trim = "") Then 'Something processed Else 'Something processed
2
2199
by: Digital Fart | last post by:
following code would split a string "a != b" into 2 strings "a" and "b". but is there a way to know what seperator was used? string charSeparators = { "=", ">=", "<=" , "!=" }; string s1 = "field != value" result = s1.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
29
51757
by: Ajay | last post by:
Hi all,Could anybody tell me the most efficient method to find a substr in a string.
2
3182
by: Badass Scotsman | last post by:
Hello, Using VB and ASP,NET I would like to be able to search a STRING for a smaller STRING within, based on the characters which appear before and after. For example: String1 = " That was a tasty burger"
94
4788
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring that I found inside of a string. Any ideas?
25
12957
by: John Salerno | last post by:
Forgive my excitement, especially if you are already aware of this, but this seems like the kind of feature that is easily overlooked (yet could be very useful): Both 8-bit and Unicode strings have new partition(sep) and rpartition(sep) methods that simplify a common use case. The find(S) method is often used to get an index which is then used to slice the string and obtain the pieces that are before and after the separator....
0
9731
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10651
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10136
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7671
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5556
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5697
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3871
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.