string - C# : IndexOf storing 2 values at a time -
i trying break single string 3 strings having problem using indexof
when input string e.g 15,m,true i-e use 2 commas in input
console.write("enter pyrimid slot number ; block number ; whether or not block should lit or not ?"); string pyrimidslot = console.readline(); int commanumber = pyrimidslot.indexof(","); string pyrimidslotnumber = pyrimidslot.substring(0, commanumber); console.writeline("your block number : " + pyrimidslotnumber); the code works fine till here
string blocknumber = pyrimidslot.substring(commanumber + 1, commanumber +1 ); console.writeline("your block number : " + blocknumber); but when try separate "block number" input string using above code output your block number : m,t works fine when change code
string blocknumber = pyrimidslot.substring(commanumber + 1, commanumber -1 ); why not storing value of first index ? commanumber + 1 starting index , commanumber - 1 ending index does noy make sense ?
this done string.split
string[] parts = pyrimidslot.split(','); console.writeline(parts[0]); console.writeline(parts[1]); console.writeline(parts[2]); instead indexof, need split string yourself, , remember second parameter of string.substring length, meaning need pass number of characters extract starting position.
int firstcomma = pyrimidslot.indexof(','); string slot = pyrimidslot.substring(0, firstcomma); int secondcomma = pyrimidslot.indexof(",", firstcomma + 1); string block = pyrimidslot.substring(firstcomma + 1, secondcomma - firstcomma - 1); string hi = pyrimidslot.substring(secondcomma +1); console.writeline(slot); console.writeline(block); console.writeline(hi); of course, assumes input string contains 2 commas separated @ least 1 character
Comments
Post a Comment