473,413 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

How to see if string contains two Ks and remove the last one.

I am writing a parse system, and I want to go in and check a string for multiple Ks to be exact a K at the beginning and one at the end, if it does have a K at the end of the string I want to remove that K. I don't have an exact size of my string. I know how to remove the last K but not how to check the string for the multiple K's.

This is what I have:

Expand|Select|Wrap|Line Numbers
  1.             if (poNumber.Contains("K...K"))
  2.                 poNumber.Remove(poNumber.Length - 1,1);
I know that the first part will not work the way I want it to.
Oct 11 '12 #1
6 2108
I figured this out if anyone wanted to see how here are my results

Expand|Select|Wrap|Line Numbers
  1.    if (poNumber.EndsWith("K"))
  2.    {
  3.       poNumber = poNumber.Remove(poNumber.Length - 1,1);
  4.       this.textBox10.Text = poNumber;
  5.    }
  6.    else
  7.       this.textBox10.Text = poNumber;
Oct 11 '12 #2
weaknessforcats
9,208 Expert Mod 8TB
Scan the string and look at each character and if it is a K then increment a counter.
Oct 11 '12 #3
weaknessforcats

Scan the string and look at each character and if it is a K then increment a counter.

That won't work weakness, I am reading through a PDF417 barcode, it has over 90 characters and multiple ones are K, the minor string I parsed out the poNumber will have at least one K but if the last character is the K I have to remove it. Otherwise I end up storing false data from my barcode.
Oct 11 '12 #4
I don't want to mess with any of the other Ks in the poNumber string just that last one.
Oct 11 '12 #5
weaknessforcats
9,208 Expert Mod 8TB
I was trying to detect mutiple K's. If the counter ends up with a numer other than 1 you have multiple K's. This says nothing about the position of the K's.

What kind of string is this? A C string with a \0 termminator or a C++ Standard Library string object?

Obviously, you can tell if the first character is a K
The last character being is the one before the terminator. You remove it by a) storing a \0 on top of it if youhave a C string or b) delete the character at size()-1 if it's a C++ Standard Library string object.
Oct 11 '12 #6
I am using C# language, my method worked, I have only been using C# for a month and a couple days now, and have never used it before so some of the language specifics I don't know. For example I didn't know about the EndsWith() function.
Oct 11 '12 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: booksnore | last post by:
I am writing some code to search for strings that contain every letter of the alphabet. At the moment I am using the method below to check to see if a string contains every letter of the alphabet....
2
by: lekshminair | last post by:
dears can u help me remove last letter in a string. eg: arises in this string remove only last 's' ouput:arise
13
by: nishit.gupta | last post by:
Is their any fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" Thnx
2
by: shapper | last post by:
Hello, I am trying to remove the last letter from a string: names.Remove(names.Remove(names.Length - 1, 1)) I get index out of range. What am I doing wrong? Thanks, Miguel
13
by: SuvarnaChaudhari | last post by:
Hi,I am new to .net. As per ur guidance I tried myString = myString.SubString(0,myString.Length-1); I also used str.lastindexof n str.remove,but by using this I m gettin string as ' (B193 '' H13 ''...
7
by: Karch | last post by:
I need to find the fastest way in terms of storage and searching to determine if a given string contains one of a member of a list of strings. So, think of it in terms of this: I have a string such...
4
by: Jeff | last post by:
Hey ..NET 3.5 I'm trying to search a string to determine if the string contains </table>, but string.Contains don't find it. I've used WebRequest/WebReponse to retrieve the html from a...
1
by: Jon Skeet [C# MVP] | last post by:
On Apr 30, 3:56 pm, Raja <rajesh.mad...@gmail.comwrote: Well, in fact it's *not* working fine - it's not behaving the same on SQL server as it would be in normal code. String.Contains is case-...
8
by: SMJT | last post by:
Does anyone know why the string contains function always returns true if the token is an empty string? I expected it to return false. "AnyOldText".Contains("") or...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.