How to split get utf string - c#

if (cmd == "card_request") {
Debug.Log("FOund cards");
ISFSObject responseParams = (SFSObject)evt.Params["params"];
Debug.Log(responseParams.GetClass("cards").ToString());
SFSArray data = (SFSArray)responseParams.GetSFSArray("cards");
Debug.Log(data.GetUtfString(0));
//for (int i = 0; i < data.GetUtfString(0).IndexOf("value"); i++) {
firstSplit = data.GetUtfString(0).Split(';');
Debug.Log(firstSplit);
//}
for (int i = 0; i < firstSplit[0].IndexOf("value"); i++) {
secondSplit = firstSplit[0].Split(':');
Debug.Log(secondSplit);
}
for (int i = 0; i < secondSplit[0].IndexOf("value"); i++) {
thirdSplit = secondSplit[0].Split(',');
Debug.Log(thirdSplit);
}
}
the data is coming fine at this line Debug.Log(data.GetUtfString(0)); But when I try to split it it gives errors. Somebody can please suggest me the effective way to split UTF string. Null exceptions occurs at secondSplit and thirdSplit

Related

How to remove an element from an array and resize the array

I want to remove a specific object from an array, put it in a smaller array without getting out of range. This is what I've tried but it won't work.
Skateboard[] newSkateboard = new Skateboard[_skateboards.Length - 1];
for (int i = 0; i < _skateboards.Length; i++)
{
if (skateboard.Code != _skateboards[i].Code)
{
newSkateboard[i] = _skateboards[i];
}
}
Sure.
var j = 0;
for (int i = 0; i < _skateboards.Length; i++)
{
if (skateboard.Code != _skateboards[i].Code)
{
newSkateboard[j] = _skateboards[i];
j = j + 1;
}
}

Code execution stops after for loop?

So i'm trying to make a program that uses the League of Legends API in C#.
I found a NuGet package that makes using the API a lot easier.
Everything works fine so far except the code execution stops after the
first for loop i use.
Here's the code: (of course i took out the api key)
string[] summnames;
long[] champids;
long[] teamids;
long[] champs;
CreepScoreAPI.ParticipantLive[] enemy;
CreepScoreAPI.ParticipantLive[] ally;
CreepScoreAPI.ParticipantLive centsumm;
CreepScoreAPI.ParticipantLive[] champsss;
CreepScoreAPI.ChampionStatic[] champions;
CreepScoreAPI.Summoner[] sumners;
CreepScoreAPI.League[] leaguesz;
Dictionary<string, List<CreepScoreAPI.League>>[] leagues;
int[] champidsint;
string[] champnames;
int s;
int se;
public async Task<string> game(string summname)
{
string data;
CreepScoreAPI.CreepScore cs = new CreepScoreAPI.CreepScore("api key");
var summoner = await cs.RetrieveSummoner(CreepScoreAPI.CreepScore.Region.EUNE, summname);
long summid = summoner.id;
var thegame = await summoner.RetrieveCurrentGameInfo();
CreepScoreAPI.ParticipantLive[] participants = thegame.participants.ToArray();
for (int i = 0; i <= 9; i++) { summnames[i] = participants[i].summonerName;}
for (int i = 0; i <= 9; i++) { champids[i] = participants[i].championId;}
for (int i = 0; i <= 9; i++) { teamids[i] = participants[i].teamIdLong;}
for (int i = 0; i <= 9; i++) { champids[i] = participants[i].championId;}
for (int i = 0; i <= 9; i++) { champidsint[i] = Convert.ToInt32(champids[i]);}
for (int i = 0; i <= 9; i++) { champions[i] = await cs.RetrieveChampionData( CreepScoreAPI.CreepScore.Region.EUNE, champidsint[i], CreepScoreAPI.Constants.StaticDataConstants.ChampData.All, "en_US", "7.1.1",false ); }
for (int i = 0; i <= 9; i++) { champnames[i] = champions[i].name; }
for (int i = 0; i <= 9; i++) { sumners[i] = await cs.RetrieveSummoner(CreepScoreAPI.CreepScore.Region.EUNE, summnames[i]); }
for (int i = 0; i <= 9; i++) { leagues[i] = await sumners[i].RetrieveLeague(); }
/* teamsorter */
foreach (CreepScoreAPI.ParticipantLive p in participants)
{
if (p.summonerId == summid)
{
centsumm = p;
}
if (p.teamIdLong == centsumm.teamIdLong)
{
ally[s] = p;
s++;
}
if (p.teamIdLong != centsumm.teamIdLong)
{
enemy[se] = p;
se++;
}
}
data = " I'LL FORMAT A STRING THAT OUTPUTS ALL THE DATA I NEED HERE";
return data;
}
When I call the game function and input the name of the summoner i gets to the first for loop and doesn't even populate the summnames[] array and stops execution with no error code.
What I'm trying to do with all the loops is populate the variables I made before the function so I can use them later for other purposes.
I think you should assign all the arrays and set their length to at least 10.
This could solve the problem.

using WPF can I do auto word replacement when a user is typing in richtextbox

public void overallTextReplace(RichTextBox[] rtb) {
string[] keyword = { "FCI", "CNG", "DCR", "EZR", "VASC", "CND" };
string[] newString = { "Forecourt Controller","Case Number Declined" ,"Case Number Given", "Dispenser Card reader", "Enhanced Zone Router", "Verifone Authorized Service Contractor" };
TextRange[] text = new TextRange[rtb.Length];
for (int I = 0; I < rtb.Length; I++) {
text[I] = new TextRange(rtb[I].Document.ContentStart, rtb[I].Document.ContentEnd);
}
for (int I = 0; I < text.Length; I++) {
for (int K = 0; K < keyword.Length; K++) {
TextPointer current = text[I].Start.GetInsertionPosition(LogicalDirection.Forward);
string textInRun = current.GetTextInRun(LogicalDirection.Forward);
if (!string.IsNullOrEmpty(textInRun)) {
int index = textInRun.IndexOf(keyword[K]);
if (index != -1) {
TextPointer selectionStart = current.GetPositionAtOffset(index, LogicalDirection.Forward);
TextPointer selectionEnd = selectionStart.GetPositionAtOffset(keyword.Length, LogicalDirection.Forward);
TextRange selection = new TextRange(selectionStart, selectionEnd);
selection.Text = newString[K];
selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Regular);
rtb[I].Selection.Select(selection.Start, selection.End);
rtb[I].Focus();
}
}
current = current.GetNextInsertionPosition(LogicalDirection.Forward);
}
}
}
okay so this code will look through all RichTextBoxs in the WPF form when passed in to the function, it will then look for the keywords listed and replace them with the newString. the problem im having is that the program only looks on one line of text from start to finish. If it detects a newline it will not look past it for example: line1: the FCI is a fuel controller. it replaces it just fine but if i have more on line 2 it will not make the replace. if it makes any difference their are 6 richTextBoxes being passed in to this function.
just found an error, but it has nothing to do with my first issue. so it seems that having 6 array indexes prevents the code from running and throws a null ref on TextRange selection = new Textrange(selectionStart, selectionEnd);
but if i use VASC as the word to be replaced their is no exception. I am not sure why.
For winforms:
Try this(Though I haven't run this code but logically it should work) :
public void overallTextReplace(RichTextBox[] rtb)
{
string[] keyword = { "FCI", "CNG", "DCR", "EZR", "VASC", "CND" };
string[] newString = { "Forecourt Controller","Case Number Declined" ,"Case Number Given", "Dispenser Card reader", "Enhanced Zone Router", "Verifone Authorized Service Contractor" };
for (int i = 0; i < rtb.Length; i++)
{
for (int j = 0; j < 6; j++)
{
rtb[i].Rtf=rtb[i].Rtf.Replace(keyword[j],newString[j]);
}
}
}
For wpf:
for (int i = 0; i < rtb.Length; i++)
{
RichTextBox rtb_wording= rtb[i];
var textRange = new TextRange(rtb_wording.Document.ContentStart, rtb_wording.Document.ContentEnd);
string rtf;
using (var memoryStream = new MemoryStream())
{
textRange.Save(memoryStream, DataFormats.Rtf);
rtf = ASCIIEncoding.Default.GetString(memoryStream.ToArray());
}
for (int j = 0; j < 6; j++)
{
rtf =rtf.Replace(keyword[j],newString[j]);
}
MemoryStream stream = new MemoryStream (ASCIIEncoding.Default.GetBytes(rtf));
rtb_wording.SelectAll();
rtb_wording.Selection.Load(stream, DataFormats.Rtf);
}

Getting position in a list and comparasion

I am trying to get positions in a list of some values to compare them with another list.
for (int i = 0; i <= commands.ToArray().Length; i++)
{
levensheteinvalues_commands.Add(commands.ToArray()[i].ToString());
levensheteinvalues_numbers.Add(
Program.ComputeLevenshteinDistance(args[0],
commands.ToArray()[i].ToString()));
}
for (int i = 0; i <= commands.ToArray().Length; i++)
{
if (smallestlevensheteinvalue == 0)
{
smallestlevensheteinvalue = levensheteinvalues_numbers[i];
}
else if (smallestlevensheteinvalue > levensheteinvalues_numbers[i])
{
smallestlevensheteinvalue = levensheteinvalues_numbers[i];
}
}
var indexes = levensheteinvalues_numbers.GetIndexes(smallestlevensheteinvalue);
Why doesn't
var indexes = levensheteinvalues_numbers.GetIndexes(smallestlevensheteinvalue);
work? And when I get the value how can I compare it to another list?
The code you have posted have some serious problems. This May solve your problem since your code as well as your approach is very unclear and ambiguous. I have blindly edited the code to fix the serious problems.
for (int i = 0; i < commands.Count(); i++) {
levensheteinvalues_commands.Add(commands.ElementAt(i).ToString());
Program.ComputeLevenshteinDistance(args[0], commands.ElementAt(i).ToString()));
}
for (int i = 0; i < commands.Count(); i++) {
if (smallestlevensheteinvalue == 0)
{
smallestlevensheteinvalue = levensheteinvalues_numbers[i];
}
else if (smallestlevensheteinvalue > levensheteinvalues_numbers[i])
{
smallestlevensheteinvalue = levensheteinvalues_numbers[i];
}
}
int index = levensheteinvalues_numbers.IndexOf(levensheteinvalues_numbers.Min());

how can i fetch text box value in string array and print

this is my code 1 index print second index showing error "Index was outside the bounds of the array." please help what should I do?
string[] SName = Request.Form.GetValues("title");
string[] Email = Request.Form.GetValues("fname");
for (int i = 0; i <= SName.Length - 1; i++)
{
Response.Write(SName[i]);
Response.Write(Email[i]);
}
It is not necessary you get same length for both SName and Email string arrays.
Index is out of bound because length are not same.
Better way is to do it separately:
string[] SName = Request.Form.GetValues("title");
string[] Email = Request.Form.GetValues("fname");
for (int i = 0; i < SName.Length; i++)
Response.Write(SName[i]);
for (int i = 0; i < Email.Length; i++)
Response.Write(Email[i]);
If you want to print one name and email then use this:
string[] SName = Request.Form.GetValues("title");
string[] Email = Request.Form.GetValues("fname");
int iLength = -1;
if(SName.Length > Email.Length)
iLength = SName.Length;
else
iLength = Email.Length;
for (int i = 0; i < iLength; i++)
{
if(i < SName.Length)
Response.Write(SName[i]);
if(i < Email.Length)
Response.Write(Email[i]);
}
NOTE:
If you are not dealing with array of HTML element with same name then you don't have to use Request.Form.GetValues("title"). See following example:
string SName = Request.Form["title"];
string Email = Request.Form["fname"];
Response.Write(SName + " " + Email);
Your code should be.
if (SName != null)
for (int i = 0; i < SName.Length; i++)
Response.Write(SName[i]);
if (Email != null)
for (int i = 0; i < Email.Length; i++)
Response.Write(Email[i]);
The problem is that length of SName and Email are different.
You can use the below code, which gives the result in a single loop
if (SName != null && SName.Length > 0 && Email != null && Email.Length > 0)
{
for (int i = 0,j=0; i < SName.Length && j<Email.Length; i++,j++)
{
Response.Write(SName[i]);
Response.Write(Email[j]);
}
}

Categories